Velocimacro - Generate a HTML Select box and its Options
A quick and simple Velocimacro to generate HTML Select boxes and its Options; automatically selects the correct option based on the value given.
You can place this #macro anywhere in your Velocity templates but I used my global VM_global_library.vm file.
Code
#macro( generateSelectBox $name $options $value )
<select name='$name' value='$value'>
#foreach ( $option in $options )
#set( $selected = "" )
#if ( $option == $value )
#set( $selected = "selected='true'" )
#end
<option value='$option' $selected>$option</option>
#end
</select>
#end
Example Usage
#generateSelectBox( "enabled" ["Y","N"] $form.enabled )
The resulting HTML:
<select name='enabled' value='N'>
<option value='Y'>Y</option>
<option value='N' selected='true'>N</option>
</select>
Addendum
11th November 2005 : Thanks to MATT for pointing out the FreeMarker template engine as a promising replacement for Velocity.
About this entry
You’re currently reading “Velocimacro - Generate a HTML Select box and its Options,” an entry on Weblog of Michael Cutler
- Published:
- 5th October 2005 / 12:10pm
- Category:
- Java, Jakarta Velocity
Related Entries
- Velocity if statement is type sensitive…
26th July 2005 - Wordpress 2.0.3 ‘Bug Fix & Security Release’
1st June 2006 - WP Plugin » SpamKit Plugin 0.4 - Time-Based-Tokens to Fight Spam
6th March 2006 - Horde 3.0.8 appears to be broken
15th December 2005 - DVB Support for the Videolan Client (VLC) on Fedora Core 5
6th October 2006
1 Comment
Jump to comment form | comments rss [?] | trackback uri [?]