[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cgiapp] Navigation Templates and Run Mode


On 10/20/05, Brett Sanger <suppressed> wrote:
> That's not quite how I see it.  The templates isn't supposed to CARE
> about your current runmode, it's simply showing a set of options.  The
> app calls the appropriate template based on the runmode.  The app should
> make that decision, not the template.

I think you are looking at this the wrong way.  The template isn't
deciding which runmode method is being executed.  All it is doing is
using a piece of information to decide which menu item to highlight. 
It doesn't matter that by coincidence this is the same piece of
information that the module uses to decide which method to execute to
generate the response page.

> Now, in practical terms, it's not a big deal.  It's just a violation of
> mental boundries that sets off a red flag, like having code in your
> template, or doing formatting in the application.

I think that people often take these concepts too far.  To me, saying
that there should be no code in the templates is a fairly silly
statement that is repeated all the time.  Of course you need code in
the templates, otherwise it would be a plain html file.  You need
loops, and you need if statements, and you need variables.  That is
code!

What is important to remember is that you need to limit this code to
'display' code only.  Figuring out which menu item to highlight is a
display task.  Your CGIApp module couldn't care less which menu item
is highlighted -- it shouldn't even care that there is a menu.  But
your templates do care, so some code to figure out what menu item to
highlight is totally appropriate (in my opinion).

> > In navigationmenu.tt2, I'd hack something like this:
> > <ul id="navigation">
> >    <li[% (rm == 'rm1') ? ' id="current"' : '' %]>
> >       <a href="/app/rm1">Runmode 1</a>
>
> That's much what I have.  It's looking like that's what everyone else
> does too, so I guess I'm looking for extra simplicity where there is none.

I have a suggestion here that takes a page from the programmers
notebook, and applies it to template design.  It doesn't take away the
complexity, but hides it away, so it only needs to be done once, and
can be reused across multiple applications.  Just write a function to
do the dirty work for you (or in the case of template toolkit, write a
MACRO).

---------------------

[%# Stick this (and only this) into every page that needs the 'mainmenu' -%]
[% INCLUDE 'mainmenu' highlight='rm1' %]

---------------------

[%# create one menu include file for each app, with this code
      in it, which configures your menu items.  If you have more
      than one menu, create multiple blocks (with unique names),
      for each menu -%]
[% BLOCK mainmenu -%]
[% build_menu( 'mainmenu', highlight, 'color: red', [
    [ 'rm1', '?rm=rm1', 'Runmode 1' ],
    [ 'rm2', '?rm=rm2', 'Runmode 2' ],
    [ 'google', 'http://google.com/', 'Google' ],
] ) -%]
[% END -%]

---------------------

[%# Hide this macro into an include file that is autoincluded
      on every template.  I know this MACRO is not pretty,
      but this menu building code can be reused across many
      apps, so you only have to look at it once :). -%]
[% MACRO build_menu(menuid, highlight, css, items) BLOCK -%]
[% IF highlight -%]
<style>
#[% menuid %]_[% highlight %] {
    [% css %]
}
</style>
[% END -%]
<ul>
[%   FOREACH item IN items -%]
  <li class="[% menuid %]" id="[% menuid %]_[% item.0 %]" />
    <a href="[% item.1 %]">[% item.2 %]</a>
  </li>
[%   END -%]
</ul>
[% END -%]


So the complexity is still there, but we are hiding it where you
rarely need to look at it.  The MACRO is the most complex, but it can
be reused for all your apps.  The BLOCK is also a little bit complex,
because the template designer needs to be able to build a data
structure, but with a little bit of cut and paste that shouldn't be a
problem.  And the INCLUDE directive is as simple as it gets, and this
is also happens to be the most often used piece, since it will appear
on every page that needs this menu.

I'm sure some of you are screaming 'heretic' after seeing that, but to
me, that is all display logic.  I don't expect my template designers
to be able to put together the MACRO, but they don't have to, as I
just did it for them.  I do expect my template designers to be able to
handle the menu BLOCK, and the INCLUDE directive...  And they still
have full control over the stylesheet to make this menu look like
anything they want.

Anyway, this is something I just cooked up in response to this
message, so it probably needs some tweaks, but hopefully it
illustrates the point.

Cheers,

Cees

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.