[Nitro] Passing data from a controller to a template

James Britt james_b at neurogami.com
Fri Jun 2 20:50:41 EDT 2006


Jonathan Buch wrote:
> Hi,
> 
> 
>>>Then, in foo.xhtml, refer to @title and @content.
>>
>>Is there a way to do this using accessors instead of instance variables?
> 
> 
> You are aJavaFan(String newTitle), aren't you ;D

:)

Don't worry; we see this a lot in RubyLand  (I mean ruby_land).

> 
> No seriously, using instance variables is the correct way here (calling
> methods should be done in the Controller anyway) but you could sure
> create extra functions (accessors) for all your instance variables.
> 
> class MyController
>    attr :foo
> 
>    # public stuff 'n actions goes here
> 
>    private
> 
>    def setFoo(f); @foo = f; end
>    def getFoo; @foo; end
> end
> 
> <html><head><title>#{getFoo}
> 
> But... aint that ugly?

Yeah, but why do it like that?  Why not make the methods look like 
attribute accessors?

     def foo=(f); @foo = f; end
     def foo; @foo; end

> 
> Basically, the foo.xhtml IS the Controller instance, so you can do
> everyting you can do in the controller, no limits here, nitro is all
> about freedom ;)

Could not one just use the controller class (i.e, some_controller.rb) to 
define data accesor methods, and omit the specific action methods?

So that, for example, MainController would not define an 'index' method; 
index.xhtml would reference assorted methods defined in MainController 
and render the results.


(This means that any business logic associated with the call to index 
would either be in index.xhtml, or would emerge from the calls made by 
index.xhtml to the various data accessor methods of MainController.)



-- 
James Britt

http://www.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - The Journal By & For Rubyists
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.30secondrule.com   - Building Better Tools



More information about the Nitro-general mailing list