views:

110

answers:

1

Hi,

Coming from PHP world, I used to create select box like this:

<select>
<?php foreach($arrField as $idx=>$val){?>
   <option <?php echo ($fieldVal == $idx ? "selected='selected'" : ''); ?>><?php echo $val; ?></option>
<?php } ?>
</select>

However, I can't do that in python. Here's my snippet:

<select name='type'>
   <option value='normal' {% if id = 'normal' %} selected="selected"{% endif %}>1-Normal</option>
   <option value='image' {% if id = 'image' %} selected="selected"{% endif %}>2-Image</option>
</select>

I got this error:

TemplateSyntaxError: 'if' statement improperly formatted

Is there a way to do this?

+2  A: 

Hi,

You will need to use (if you are using Django):

{% ifequal id "something"%}selected='selected'{% endifequal %}

You will also need to make sure "id" is a variable you pass into templates.Render()

P

Kinlan
On another note, the {% if %} syntax cant have an "=" in, it can only be used to compare against a value of "true" or false. http://docs.djangoproject.com/en/dev/ref/templates/builtins/ has a good set of the filters and tags you can use with associated values.
Kinlan