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

George Moschovitis george.moschovitis at gmail.com
Tue Jul 10 01:33:37 EDT 2007


>
> class Box < Raw::Element
>   def open
>     %|<div style="color: #@color">|
>   end
>
>   def close
>     "</div>"
>   end
> end
>


Elements are evaluated at "compile time" ie when the page is loaded for the
first time. However, you can  achieve dynamic effects, let me give you an
example:

class Box < Raw::Element
   def render
      %{
      <div style="color: \#{@color}">
        #{content}
      </div>
      }
   end
end

then use it like this:

<?r
colors = [ "#ff0", "#f00", "#00f", #"0ff" ]
?>
<h1>Dynamic boxes</h1>

<Box color="colors[rand(colors.size)]">
   This box is randomly colored ;-)
</Box>


-g.

-- 
http://phidz.com
http://blog.gmosx.com
http://cull.gr
http://www.joy.gr
http://nitroproject.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/nitro-general/attachments/20070710/254efba8/attachment.html 


More information about the Nitro-general mailing list