Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: HTML - sharing design and logic

by muba (Priest)
on Jul 01, 2004 at 13:00 UTC ( [id://371066]=note: print w/replies, xml ) Need Help??


in reply to Re: HTML - sharing design and logic
in thread HTML - separating design and logic

Well, I'd say you could do this by placing the select tag within the template, then use a loop to place all the options and use the template's IF tag to insert the 'selected' keyword.

Replies are listed 'Best First'.
Re^3: HTML - sharing design and logic
by hardburn (Abbot) on Jul 01, 2004 at 13:13 UTC

    Yes, you can do it that way, and many people do. It makes the templates really, really ugly. If there is a clean solution to this, I haven't seen it yet. Ironically, some of the cleanest ones I've seen involve creating the HTML on the Perl side (though usually with a backend module doing the actual HTML generation).

    ----
    send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

      It makes the templates really, really ugly.

      Depends on how you do it. Myself, I use Template Toolkit and right off the bat I created a widgets/ directory in my base template directory. This widgets directory contains a different template for each of the form fields (text inputs, textareas, radio buttons, select fields, etc). My select field template is exactly what is below. I'm not even sure now if the SET option.selected = '' line is needed. I think I had to add it because of some weird complication (perhaps the option.selected value wasn't being reset in each iteration of the loop?).

      [% USE HTML %] [% DEFAULT size = 1 %] <select name="[% HTML.escape(name) %]" size="[% size %]" class="frmSel +ect"> [% FOREACH option = options %] [% SET option.selected = ''; SET option.selected = ' selected="selected"' IF option.value = += selected; DEFAULT option.label = option.value; %] <option value="[% HTML.escape(option.value) %]"[% option.selected +%]>[% HTML.escape(option.label) %]</option> [% END %] </select>

      Now within any html template that wants to include a select field, all it takes it this simple include:

      [% INCLUDE widgets/select.tmpl name="fav_flavour" selected="$fav_flavour" options=[ { value => 'Chocolate', label => 'brown stuff' }, { value => 'Vanilla', label => 'plain stuff' }, { value => 'Strawberry', label => 'red stuff' } ] %]

        Myself, I use Template Toolkit . . .

        Increasing the complexity of the template isn't a real solution, because at that point you're no longer doing seperation of data and code, which is the whole point.

        ----
        send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

      Perhaps our standards of beauty differ, but:
      <select> <tmpl_loop name='options'> <option value='<tmpl_var val>' <tmpl_if selected > selected </ +tmpl_if> </option> </tmpl_loop> </select>
      Is that really so ugly?

        I was thinking of the situation where the HTML designer wanted to include the list statically:

        <select> <option value="1" <TMPL_IF option1>selected</TMPL_IF>>1</option> <option value="2" <TMPL_IF option2>selected</TMPL_IF>>2</option> <option value="3" <TMPL_IF option3>selected</TMPL_IF>>3</option> . . . </select>

        Belch!

        ----
        send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://371066]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 09:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found