One solution: put your options in an array rather than hard coding the html... then iterate through the array comparing the values with the placeholder, and add a ’selected="selected"’ attribute to the html if they match. Kinda like this:
<select name="role" id="role" />
[[!roleOptions? &selected=`[[+role]]`]]
</select>
roleOptions is a snippet that reads a PHP array and outputs <option> tags:
<?php
$opts = array(1=>'User',2=>'Reviewer'); // values for the <option> array
$return = ''; // the return chunk
foreach ($opts as $key=>$val) {
// add 'selected' attribute if the array key matches the placeholder
$sel = (isset($selected) && $key == $selected) ? 'selected="selected"' : '';
$return .= "<option $sel value=\"$key\">$v</option>\n"; // append html to output string
}
return $return;
?>
That should work. You can make it more usable by creating a single snippet that gets the options array from an argument (rather than needing a separate snippet for each set of options). [[!opts? &array=`role` &selected=`[[+role]]`]]. I keep my arrays in external include files (you could also generate them from a database table of all possible values).
I’ve been thinking of writing a tutorial on this. Not sure how other people are handling this sort of thing in MODx; maybe there are more elegant solutions.