[Sputnik-list] Flat menu bar

Yuri Takhteyev yuri at sims.berkeley.edu
Tue Feb 12 06:59:10 GMT+2 2008


Thanks.

A few observations:

First, if it wasn't for setting _template, this would also work as a
function to generate a two-level list, which I imagine would be a more
common case.  It would perhaps make sense to rename to "get_full_menu"
or something like that and add a flag that determines whether
_template is set or not.

Second, your function uses two pairs of loops, as does mine.  But I am
wondering if it would be possible to do the same essentially in one
loop (inside cosmo.fill).  The reason I had two double loops is
because I needed to loop through all the items to check if any of the
2nd-level items matched node.category, in which case I would also know
to make the corresponding 1st level item highlighted.  Wondering if
it's possible to avoid this.  Not a big issue, though.

Third, perhaps the two functions (get_full_menu and get_nav_bar) can
be merged into one, since the code is mostly the same.  One way to do
that would be to have the cosmo table have do_subsections for each
section _and_ also at the top level.  I.e., something like this:

   local do_subsections = function(section)
               for i, subsection in ipairs(section or {}) do
                  cosmo.yield {
                     class = util.choose(subsection.is_active, "front", "back"),
                     link = sputnik:make_link(subsection.id),
                     label = subsection.title
                  }
   end

   return cosmo.f(node.templates.NAV_BAR){
            do_sections = function()
               for i, section in ipairs(nav) do
                  cosmo.yield {
                     class = util.choose(section.is_active, "front", "back"),
                     id    = section.id,
                     link  = sputnik:make_link(section.id),
                     label = section.title,
                     do_subsections = function() return
do_subsections(section) end
                  }
               end
            end,
            do_subsections = function() return
do_subsections(nav.current_section) end
            end,
         }

Note that you can then get either sidebar depending on the template.
But also, didn't we agree to merge NAV_BAR into the MAIN template?
I.e.:


function wrappers.default(node, request, sputnik)
   ...
   return cosmo.f(node.templates.MAIN){
      site_title       = sputnik.config.SITE_TITLE or "",
      ...
      do_menu_sections = function () ...


>  [1] I don't know if there's a convention to create sequences of
>  elements with a separator string like the standard Lua function
>  table.concat. I decided to use "subtemplates alternatives" body/tail,
>  but other solution exist (alternatives head/body, head/body/tail,
>  subtemplates $if_first/$if_last, etc.). It would ease sharing of
>  templates if we could agree on the best syntax for these (rather
>  common) cases.

I prefer the alternative template syntax myself.  I used $if_first /
$if_last style before because I hadn't actually added this feature to
cosmo at that point.  I also prefer to use a different template for
the first element vs. everything else, though simply because it is
usually easier to test whether the element is first than whether it's
last.  It seems to me that  "i==1 and 1 or 2" is just a bit easier on
the eyes than "i==#nav and 2 or nil" .  Note that you could also do
min(i,2) in this case.

Finally, should we find some place on the wiki to put alternative
templates like this one?  Perhaps there should be a page called
"Templates" that could host examples like this?  Also, maybe I should
promote "Customization" to a section?  (And perhaps demote "Demos" to
a subsection in the first tab?  Or perhaps even rename the sections to
have all of them be verbs: "Install", "Customize", "Track" (or
"Follow"?), "Talk", "Hack". :)  Not sure what the first one would be
called though.

  - yuri



More information about the Sputnik-list mailing list