[Nitro] getting dynamic in the skin

Emmanuel Piperakis epiperak at softlab.ece.ntua.gr
Fri Sep 9 23:56:18 EDT 2005


> i've got yet another little but annoying problem...
> i try to get a little bit more dynamic in the skin.rb and want to get other 
> templates to get included (using the <render>-tag)
> after playing around a bit, following example-use of a controller-variable 
> that was something like (\#{@context}) i got to the following..
>
> \#{if @context.headers["REQUEST_URI"] == 'doc/' : "<render 
> href='doc/list_classes'/>" end}
>
> so, i put this into the skin... but i cannot follow the way it goes exactly:
> i get output on the webpage wich looks like this:
>
> ^; render '/doc/list_classes' ; @out << %^
>
> my first thought was "huh?" and i still have no idea what this could mean...
> digging around a bit turned out that @out is the instance-variable wich 
> follows the @header, but that told me nothing i haven't known before...
> maybe someone else has solved this? creating dynamic menus in the skin out of 
> other templates can't be that hard...
>
> thank you in advance
> manveru
>
I paste here my skin.rb which supports dynamic submenus and titles set in 
the controller...

require 'nitro/element'

class Page < Nitro::Element
   def doctype
     %~
       <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     ~
   end

   def head
     %~
       <head>
         <title>\#{context.conf.name}</title>
         <meta name="title" content="WEEZ Online Reporting" />
         <meta name="description" content="Online Reporting" />
         <meta name="keywords" content="real estate, reporting, investment" 
/>
         <meta name="category" content="real estate" />
         <meta name="robots" content="index, follow" />
         <meta http-equiv="content-type" content="text/html; 
charset=SHIFT_JIS" />
         <base href="#(@context.host_url)/" />
         <link href="/style.css" rel="stylesheet" type="text/css" 
media="screen" />
         <link href="/print.css" rel="stylesheet" type="text/css" 
media="print" />
       </head>
     ~
   end

   def header
     %~
       <div id="header">
         <?r if session[:header_title] != nil ?>
           <h1>\#{session[:header_title]}</h1>
         <?r end ?>
       </div>
     ~
   end
   def mainnav
     %~
       <div id="mainnav">
         <table>
           <tr>
             <td><a href="home">[[home]]</a> | </td>
             <td><a href="projects">[[projects]]</a> | </td>
             <td><a href="bgroups">[[bgroups]]</a> | </td>
             <td><a href="reports">[[reports]]</a> | </td>
             <td><a href="settings">[[settings]]</a> | </td>
             <td><a href="toggle_locales">[[toggle_locales]]</a></td>
           </tr>
         </table>
       </div>
     ~
   end

   def projects
     %~
       <div id="secnav_projects">
         <table>
           <tr>
             <td><a href="officers">[[officers]]</a> | </td>
             <td><a href="investments">[[investments]] | </a></td>
             <td><a href="loans">[[loans]]</a> | </td>
             <td><a href="security">[[security]]</a> || </td>
             <?r if session[:project_name] == nil ?>
               <td>[[selected]]: <b>None</b></td>
             <?r else ?>
               <td>[[selected]]: <b>\#{session[:project_name]}</b></td>
             <?r end ?>
           </tr>
         </table>
       </div>
     ~
   end
   def bgroups
     %~
       <div id="secnav_bgroups">
         <table>
           <tr valign="bottom">
             <td><a href="areas">[[areas]] | </a></td>
             <td><a href="brokers">[[brokers]] | </a></td>
             <td><a href="owners">[[owners]]</a> | </td>
             <td><a href="buildings">[[buildings]] || </a></td>
             <?r if session[:bgroup_name] == nil ?>
               <td>[[selected]]: <b>None</b></td>
             <?r else ?>
               <td>[[selected]]: <b>\#{session[:bgroup_name]}</b></td>
             <?r end ?>
           </tr>
         </table>
       </div>
     ~
   end
   def reports
     %~
       <div id="secnav_reports">
         <table>
           <tr>
             <td><a href="balance_sheet_rep">[[balance_sheet_rep]]</a> | 
</td>
             <td><a href="profit_loss_rep">[[profit_loss_rep]]</a> | </td>
             <td><a href="journal_rep">[[journal_rep]]</a> | </td>
             <td><a href="randd_rep">[[randd_rep]]</a> | </td>
             <td><a href="rent_roll_rep">[[rent_roll_rep]]</a> | </td>
             <td><a href="management_rep">[[management_rep]]</a> | </td>
             <td><a href="repair_rep">[[repair_rep]]</a> | </td>
             <td><a href="income_rep">[[income_rep]]</a> | </td>
             <td><a href="cashflow_rep">[[cashflow_rep]]</a></td>
           </tr>
         </table>
       </div>
     ~
   end

   def footer
     %~
       <div id="footer">
         Footer
       </div>
     ~
   end

   def rendering_errors
     %~
       <div id="error">
         Error
       </div>
     ~
   end
   def render
     %~
       #{doctype}
       <html>
         #{head}
         <body>
           <div id="page">
             #{header}
             #{mainnav}
             <?r p = request.path.chop ?>
             <?r if p == 'projects' || p == 'officers' || p == 
'investments' || p == 'loans' || p == 'security' ?>
               #{projects}
             <?r elsif p == 'bgroups' || p == 'areas'  || p == 'brokers' 
|| p == 'owners'  || p == 'buildings' ?>
               #{bgroups}
             <?r elsif p == 'reports' || p == 'balance_sheet_rep' || p == 
'profit_loss_rep'  || p == 'journal_rep'  ||
                       p == 'randd_rep'  || p == 'rent_roll_rep'  || p == 
'management_rep'  || p == 'repair_rep'  ||
                       p == 'income_rep' || p == 'cashflow_rep' ?>
               #{reports}
             <?r end ?>
             <div class="main">
               #{content}
             </div>
             #{footer}
             #{rendering_errors}
           </div>
         </body>
       </html>
     ~
   end
end


Then in the controller you can do something like this... (I only paste 2 
functions)


   # wrap a localization aspect
   wrap LocalizationAspect, :pre => :localize
   @lc = Localization.get

# the main listing and new function
def index
   # title of the page
   session[:header_title] = @lc['online_reporting']
   # nil the selections for other menus
   session[:project_name] = session[:project_oid] = nil
   session[:bgroup_name]  = session[:bgroup_oid]  = nil
end
alias_action :home, :index
def bgroups
   # title of the page
   session[:header_title]  = @lc['bgroups']
   # nil the selections for other menus
   session[:project_name]  = session[:project_oid]  = nil
   # nil the selections for the sub menus
   session[:area_name]     = session[:area_oid]     = nil
   session[:broker_name]   = session[:broker_oid]   = nil
   session[:owner_name]    = session[:owner_oid]    = nil
   session[:building_name] = session[:building_oid] = nil
   # TODO: add security here
   @bgroups = Bgroup.all
   @data = @bgroups.map {|b| [b.title_en, b.title_jp, b.creation,

link_set("sel", at lc['select_tag'],"bgroup",b.oid.to_s),

link_pop("edi", at lc['edit_tag'],"bgroup",b.oid.to_s),

link_set("del", at lc['delete_tag'],"bgroup",b.oid.to_s)]}
   @header = 
[@lc['bgroup_title_en'], at lc['bgroup_title_jp'], at lc['bgroup_creation'], at lc['select'], at lc['edit'], at lc['delete']]
end


finally  the template looks something like this...

<Page>
   <div>
     <?r if @bgroups.length != 0 ?>
       #{table(:id => "bgroups_list", :headers => @header, :values => 
@data)}
     <?r else ?>
       <p>[[nothing_to_display]][[fullstop]] [[add_new]] 
[[bgroup]][[fullstop]]</p>
     <?r end ?>
   </div>
   <render href="bgroup_form" />
</Page>


Tell me if that helps


E.






Emmanouil Piperakis (epiperak at cs.ntua.gr)
{To explore is Human, to Create is Devine,
  To teach is Primal, to Rule is Sin}



More information about the Nitro-general mailing list