1
votes

I am get this error when I have try convert php code in twig.

Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unexpected token "name" of value "my_pattern" ("end of statement block" expected) in...

Php code:

<select class="form-control" name="my_pattern">
  <?php for ($i = 1; $i <= 53; $i++) {($my_pattern == $i) ? $currentpat = 'selected' : $currentpat = ''; ?>
   <option value="<?php echo $i; ?>" <?php echo $currentpat; ?>><?php echo $i; ?></option>
  <?php } ?>
</select>

I have try to following in twig file.

<select class="form-control" name="my_pattern">
  {% for i in 1..53 my_pattern == i ? currentpat = 'selected' : currentpat = '' %}
    <option value="{{ i }}" {{ currentpat }}>{{ i }}</option>
  {% endfor %}
</select>

can you please let us know proper way for write above code in twig.

1

1 Answers

3
votes
{% for i in 1..53 my_pattern == i ? currentpat = 'selected' : currentpat = '' %}

Is not valid syntax for a for loop.

Did you mean something like the following:

{% for i in 1..53 %}
    <option value="{{ i }}" {% if my_pattern == i %}selected="selected"{% endif %}>{{ i }}</option>
{% endfor %}