Using view templates in cells

Cell states are mapped to methods. They may return a string as view. However, if you want to use a real template as state view, just return nothing. Recall our Hello World! example.

app/cells/hello_world_cell.rb

1
2
3
4
5
6
7
class HelloWorldCell < Apotomo::StatefulWidget
  def shout_hello
    @my_object = "Me? I said nothing!"
 
    nil
  end
...

When the state shout_hello is invoked, it won’t return a string but nil. This tells Apotomo to look for a template file called shout_hello.[rhtml|haml|...]. Instance variables assigned in the method can be used within the view file - just as you’re used to from controller actions!

A view might look like this:

app/cells/hello_world/shout_hello.rhtml

<h1>Hey, hello world!</h1>
What did you say? <%= @my_object %>

This gives you all the power and functionality known from the traditional rails controllers approach, along with Apotomo’s component flexibility.

But what if you dont want to name your state view after the method? Well, keep cool. Use state_view to command Apotomo to look for a view with a different name.

app/cells/hello_world_cell.rb

8
9
10
11
  def yell_hello
    # do something senseful...
    state_view :another_view
  end

This will result in the view another_view.[rhtml|haml|...] being rendered.

Tags:

2 Responses to “Using view templates in cells”

  1. cp Says:

    for great diagrams checkout visual paradigm uml suite, sadly not open source, but 30 days free, damn nice code/db/process modelling tool written in java.

    and regi is easy, you’ll see what i mean ;) ;)

  2. cp Says:

    wahh, how did my comment got here?? was for the hangman game question about a good modelling tool…

Leave a Reply