Rails integration

When Malline is installed, it registers itself with all view files named *.mn.

Malline files work in partials, layouts and normal view files for action. If you want, you could also use inline renderer Malline::Base.render { xhtml ... } that returns string.

On this page

Partials

You can combine Malline, ERB and other template systems in partials. .rhtml-files can be rendered from .mn-files and vice versa.

Contents of RAILS_ROOT/app/views/examples/_one.mn:

1
span.one 'This is the first'

Contents of RAILS_ROOT/app/views/examples/_two.mn:

1
img :src => image_path('second'), :alt => 'This is the second Malline partial'

Contents of RAILS_ROOT/app/views/examples/_three.rhtml:

1
2
Blop, <%= 'this is rhtml'.upcase %>.
<%= render :partial => 'examples/two' %>

Partials

1
2
3
4
5
div do
    _render :partial => 'examples/one'
    br
    _render :partial => 'examples/three'
end
1
2
3
4
5
<div>
    <span class="one">This is the first</span><br/>
    Blop, THIS IS RHTML.
    <img src="/images/second.png" alt="This is the second Malline partial"/>
</div>

Layouts

When using Malline files in layout, use the @content_for_layout -instance variable instead of yield.

Contents of RAILS_ROOT/app/views/examples/action.mn:

1
2
3
4
@title = 'Data'
ul.data! do
    3.times {|i| li "Data #{i}" }
end

Layout example

1
2
3
4
5
6
7
8
9
xhtml do
    head do
        title @title
        _stylesheet_link_tag('style.css')
    end
    body do
        self << @content_for_layout
    end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <title>Data</title>
        <link href="/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />
        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
    </head>
    <body>
        <ul id="data">
            <li>Data 0</li>
            <li>Data 1</li>
            <li>Data 2</li>
        </ul>
    </body>
</html>

You can combine all different template systems in layouts too. That is, you can have ERB layout with Malline views or the other way around.

Configuration options

:strict

Strict mode means, that only tags defined in current language plugin are used. Nonstrict mode allows every tag with method_missing. default => true

:xhtml

Load buildin XHTML plugin, that defines all XHTML tags, special xhtml-shortcut, knows all self-closing tags etc. default => true

:encoding

Default encoding. This is currently used only within XHTML plugin. default => "UTF-8"

:lang

Default language, this also is only used in XHTML plugin. default => "en"

:form_for_proxy

Do we use our own form_for-proxy between Rails and Malline. default => true

:xhtml_dtd

XHTML DTD to use in XHTML plugin. default => "Transitional"

Some options can be changed on fly, all can be changed with Malline::Base.setopt in controller. Like Malline::Base.setopt :encoding => "ISO-8859-1", :lang => "fi"

How to change options in view

1
2
3
4
5
6
7
8
9
10
# Options can be changed directly in view
malline :strict => false
foo { bar }

malline :strict => true
begin
    foo { bar }
rescue NameError => n
    div "strict mode is on (#{truncate n.to_s})"
end
1
2
3
4
<foo>
    <bar/>
</foo>
<div>strict mode is on (undefined method `foo' for ...)</div>

No comments
Write a one

Write a new comment on this page