[Nitro] how to initialize @color and make it visible into the template?
Arne Brasseur
arne at arnebrasseur.net
Mon Jul 9 16:36:10 EDT 2007
Kirk Haines schreef:
>> So if you want to access instance variables set in a controller, you
>> need to generate code to do that, and not do it directly.
>>
>
> I've read this a couple of times, but I think I still need it
> explained to me a bit more, because I am not quite sure I understand.
>
> Let's say I have something....an online community site of some sort.
> I want to have a little box in one corner of the page that shows the
> time and date, whether the user has any new messages in their mailbox,
> and some other stuff that may change regularly.
>
> To be nice and modular, I want to have something like this in my page template:
>
> <InfoBox foo="bar" blah="snog" whatever="snow white" />
>
> InfoBox is that thing with all that information.
>
> Now, in InfoBox's template, are you saying that all the content has to
> be generated programatically, because that template will only ever be
> rendered once, so I can't just dynamically include data generated a
> method call into it? Or do I misunderstand you?
>
I will give an example. Suppose your InfoBox depends on a user, if you
want to write this in your template:
<InfoBox user="@u" />
Then you have to do something like this to make it work
class InfoBox
def render
"<div class="infobox">Unread messages : \#{#{@user}.unread_messages}"
end
end
The first time the page gets rendered an InfoBox instance is created,
and the instance variable @user is set to the string "@u". The render
method is called yielding:
"<div class="infobox">Unread messages : #{@user.unread_messages}"
This becomes part of your compiled template, which utilizes the @user
var set in your controller. Note how the outer interpolation is escaped
: \#{
This is what I came up with, and I think it's messy, but it works.
Having talked about it a bit on IRC with Manveru and Jonathan I get the
impression this is not the primary usecase. You can also use it to
rewrite the part of your template inside the tag. Looked at it this way
you can add custom preprocessing to whatever is inside
<Element>...</Element>. Manveru called it 'Aspect oriented programming
for templates'. It 'wraps' a piece of your template, doing stuff with it.
For the InfoBox usecase, judging from the blog example I'd say G prefers
to use <?include href="infobox" ?> which just reminds me too much of
rails partials, but even worse. Elements seem so nice and clean for this
on first sight, but the fact that they're rendered only once makes it
cumbersome. Maybe we need another compiler filter for 'dynamic' elements?
This is one of the things where I'm very curious myself what their
primary intended use is. Maybe G or manv or Jo or someone else could
give more good examples of their usefulness, including what to do with
the @_children.
One idea I had was to declare 'widgets' with besides the render method
also have a css and a javascript method. the outer Page element joins
all the css and javascript together and puts it in the header, creating
reusable components (sort of).
Any other wild ideas? Insights?
(ab)
--
Arne Brasseur
http://www.arnebrasseur.net
arne at arnebrasseur.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/nitro-general/attachments/20070709/c5d761f4/attachment.html
More information about the Nitro-general
mailing list