Pedro Gonçalves
Pedro Gonçalves

Reputation: 33

Build templates in views

In grails I do this:

<form:windowEntrada domain="serverPool">
    <form:entrada domain="serverLocation" bean="${serverPoolInstance}">
        <form:textField label="serverPool.name" name="name" />
        <form:select label="serverPool.server" name="server" list="${Servers.list()}"/>
    </form:entrada>
</form:windowEntrada>

The tag "form:textField" render something like:

<div class='field-name'>
    <div class="title"><label for="name">Name</label>(* required)</div>
    <div class="fieldcontent"><input type="text" name="name" value="smtg"></div>
    <div class="error">If thereis an error on this field, the message appears here</div>
</div>

The tag "form:windowEntrada" render something like:

<div class='container'>
    <h1>My title here</h1>
    <div class="content">${body()}</div>
</div>

The tag "form:entrada" render something like:

<form action ... >
    <....> --> messages stuff
    ${body()}
    <div class="action-buttons">
        <input type="submit" name="lala">
        <input type="submit" name="lala2">
    </div>
</div>

How can I do i rails using erb as my view tecnology?

Thank you.

Upvotes: 0

Views: 64

Answers (1)

Pedro Gon&#231;alves
Pedro Gon&#231;alves

Reputation: 33

I Have found the answer here: pathfindersoftware.com/2008/07/pretty-blocks-in-rails-views

Here's an example applying the tutorial:

In ERB:

<%= widget("mytitle", {:class => "widget-table"}) do %>
    test test test
<% end %>

in the helper:

def widget(title, options = {}, &block)
    options[:params] = params
    options[:controller_name] = controller.controller_name

    body = capture &block

    html = render "shared/form/widget",  :body => body,
                                         :title => title,
                                         :options => options

    return html 
end

and the template _widget.html.erb:

<div class="widget <%= options[:class] unless options[:class].blank? %>">
    <% unless title.blank? %>
    <div class="widget-header">
        <span class="icon-layers"></span>
        <h3><%= title %></h3>
    </div>
    <% end %>
    <div class="widget-content">
        <%= body%>
    </div>
</div>

tks

Upvotes: 1

Related Questions