[Nitro] how to initialize @color and make it visible into the template?

Jonathan Buch john at oxyliquit.de
Tue Jul 10 02:32:38 EDT 2007


(late, my internet died at home..)

> I don't see how to mix static and dynamic aspect of nitro in an easy way.
> I would like to use some nitro functionalities like in tc_element.rb:
> class Box < Raw::Element
>   def open
>     %|<div style="color: #@color">|
>   end
> end
> but Nitro seems to ignore @color wherever you declare it (controller,
> template, <?r ?>, %{}, etc.). Any advice ?

I'm unsure what %|| means, but I'm guessing that it means "" (double
quotes).  "#{@color}" in this context means, the instance variable
@color from the _Box_ instance.  Depending on where you defined that
variable (probably in your controller), you have to get the instance
variable from the _Controller_ instance.

So, what you want is to get the @color variable resolved in the
controller scope (which is also the template scope).

You do that by escaping.   "\#{@color}", this saves the string
"#{@color}" back to the template, which then gets normally executed
like you had written #{@color} in the template.

I's just a matter of scope here.

> I don't want to be careless. Nitro looks nice but it is finally far more
> complicated to use as it looks like at first, mostly because there
> aren't usable examples available to beginners. Please, don't make it to
> elitist :( I'm really feeling i'm using the hard way, not to mention
> nitro is using facets too.

Well, we aren't actually working on making Nitro elitist.  ;)  It's just
that very few people are working on making it less elitist (meaning docs,
tutorials, examples).
This is a cycle, no people no docs, no docs no people.  Which also means,
anybody can help break that cycle, simply by writing down your own
experience, like a small tutorial on what you find important to know
as a beginner.
The second is, fast development cycle means docs are out of date fast,
so, few people to keep them up to date, which means bad docs, which also
creates problems for beginners.  So, we need more stable people who
constantly update docs.  Hard, very.
I'm far beyond the beginner levels sadly and can't really forsee what
beginners need, but at least I try to help.  :)

> @George: It is like deciphering the Rosetta stone to understand Egyptian
> hieroglyphs writing. Easy for one who can read Greek but harder to the
> other ;)
> Also, nobody react to my proposal to redefine the @_children instance
> variable into a class variable. I was able to access a child element
> like that. Was it so silly ?

Well, class variables are like $globals, just with a different scope.
That means, those are shared over all instances, if you change it in
one, it changes for all.  This might work when there are no threads
involved, but will fail with more than one concurrent thread.

Note, I don't know anything about the @_children instance variable,
so I can't say something about that.

Everything in Ruby or any other programming language is a matter
of scope, try to think 'where does this variable appear, where
am I now, can I access this variable from the current scope?'.
If the variable can't be accessed from the current scope, there's
two possibilities, bring the foreign scope to the current one,
or bring the 'execution' of the variable to the foreign scope.

Hope that helps,


Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

More information about the Nitro-general mailing list