<?xml version="1.0" encoding="windows-1252"?>
<node id="312427" title="Re: HTML::Template macros" created="2003-12-04 23:51:04" updated="2005-06-19 08:25:17">
<type id="11">
note</type>
<author id="77866">
thraxil</author>
<data>
<field name="doctext">
&lt;p&gt;for populating &amp;lt;select&amp;gt;'s i use this sub:&lt;/p&gt;
&lt;code&gt;
sub selectify {
    my $values = shift;
    my $labels = shift;
    my $selected = shift;
    my %selected = map {$_ =&gt; 1} @{$selected};

    return [map {
         {
            value =&gt; $_,
            label =&gt; shift @{$labels},
            selected =&gt; $selected{$_} || "",
         }
    } @{$values}];
}
&lt;/code&gt;

&lt;p&gt;you use it in a script like:&lt;/p&gt;
&lt;code&gt;
$template-&gt;param(foo_loop =&gt; selectify(\@values,\@labels,\@selected));
&lt;/code&gt;
&lt;p&gt;and a template file like:&lt;/p&gt;
&lt;code&gt;
&lt;select name="foo"&gt;
&lt;tmpl_loop name="foo_loop"&gt;
  &lt;option value="&lt;tmpl_var name="value"&gt;"&lt;tmpl_if name="selected"&gt; selected="selected"&gt;&lt;/tmpl_if&gt;&gt;
    &lt;tmpl_var name="label"&gt;
  &lt;/option&gt;
&lt;/tmpl_loop&gt;
&lt;/select&gt;
&lt;/code&gt;

&lt;p&gt;i usually have the stuff inside the &amp;lt;tmpl_loop&amp;gt; factored out into its own file so i can just do a &amp;lt;tmpl_include&amp;gt; anytime i have a select list. ie, this is what i you actually see all through my templates:&lt;/p&gt;

&lt;code&gt;
&lt;select name="foo"&gt;
&lt;tmpl_loop name="foo_loop"&gt;
  &lt;tmpl_include name="option.tmpl"&gt;
&lt;/tmpl_loop&gt;
&lt;/select&gt;
&lt;/code&gt;

&lt;p&gt;&lt;strong&gt;update:&lt;/strong&gt; fixed some unescaped entities.&lt;/p&gt;


&lt;div class="pmsig"&gt;&lt;div class="pmsig-77866"&gt;
&lt;p align="right"&gt;&lt;a href="http://www.columbia.edu/~anders/"&gt;anders pearson&lt;/a&gt;&lt;/p&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
312367</field>
<field name="parent_node">
312367</field>
</data>
</node>
