[Nitro] Dynamic Elements

Riku Räisänen riku.raisanen at walkingwoods.com
Mon Feb 6 19:28:04 EST 2006


First of all, I've managed not to post to ML for this long. I'm Riku Räisänen 
(Rayman), a Ruby user from Finland.

Now about the idea of dynamic elements:

I like the idea of elements. Classes callable in HTML with XHTML. What I don't 
like, is the fact that the Elements are rendered in compile time ie. they're 
static. Good for performance, but limits their usage a lot.

Currently, elements are meant to - as far as I can tell - to stuff the 
repeated static HTML parts aside and make them easy to render.

Example:

<Page>
        <StaticMenu />
        <% here_be_dynamic_data %>
</Page>

Now while this is cool, I'd like to make far better use of elemets.. Let's 
start with an example:

<Page>
        <StaticMenu />
        <LeftColumn>
                <DynamicMenu />
                <ShoppingCart />
        </LeftColumn>
        <Content>
                Here's the default content, overwritten if Content Element 
finds some better 
content from the Models.. umm.. yeah. Can't express myself better :f
        </Content>
</Page>

DynamicMenu and ShoppingCart classes / 'dynamic elements' would be somewhat 
stand-alone parts of the application. Plugin modules? I don't know the terms 
that well. At least my dynamic elements would fetch data from Models and 
possibly over-ride default behaviour according to instance variables set 
inside Controllers.

Other usages:

<Product id="1" />
<Menu items="@items" />

This can be done with elements as they currently are by stuffing ruby code 
inside the def render of the element:

Class ShoppingCart < Nitro::Element
        def render
                %~
                <% Cart.get_contents.each { .. } %>
                <% end %>
                ~
        end
end

However, I've experienced lots of problems with variables not being in 
Elements scope.. It could be that I'm wrong about this. Since they all reside 
in Nitro module? (I'm new on most of this stuff.. which is why I'm writing 
such a long mail and maybe somewhat with a better grasp on nitro and or ruby 
can help me out).

IIRC, session is not in the scope of Element. Elements can access Models. I'm 
not sure of controllers instance variables.. I think they're accessible as 
well. IIRC, however, they're all only accessible in def render. Once again, I 
could be wrong. And probably am. :)

(I think it was something like Elements only evaling the ._text property of an 
Element, meaning the stuff inside of %~~ or any other output. Right?)

Now to the main problem: In order to use elements for dynamic content, you 
basically need to stuff all the logic/code inside of the single def render 
method. With complex logic in place, the code becomes horrid. And I'd rather 
use basic ruby syntax and not the <% %> etc.

So.. I'd like to have elements with access to session and controllers instance 
variables both OUTSIDE of def render.

This way one could stuff all the logic of, say, a shopping cart inside the 
element itself. Thus implementing and removing the shopping cart from an 
application would be very easy.

Now.. I could be totally lost here and trying to do something stupid. If this 
is the case, please point me in a right direction. Any ideas of similar 
implementation ( the shopping cart etc) are welcome.

Yours,

Riku Räisänen




More information about the Nitro-general mailing list