Feature #252
Template parser - if statements & for loops
Description
If statements and for loops in the templates.
Example if (jango style):
<body>
{% if var1 %}
<div>
Lorem Ipsum
</div>
{% elseif var2 %}
<div>
Lorem Ipsum
</div>
{% endif %}
</body>
Example for (jango style):
<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% endfor %}
</ul>
Related issues
Associated revisions
History
#1 Updated by Jan Klopper over 13 years ago
Im still not sure If we'd want this.
Yes, it would make the pagemakers less intricate. But meh, if's in templates just make it possible to have too much logic in there.
Loops are less of a problem imho, however There needs to be a pretty good and clean syntax.
#2 Updated by Elmer de Looff over 13 years ago
This would upgrade the 'dumb template' to an actual View (as in, MVC). I'm not sure this is a bad idea, actually, I think it's a good idea. Currently we have a huge amalgamation of little template snippets that aren't really super useful imo.
For loops are a 'requirement' for the future I would say. I've less strong feelings about if/else structures, but it might make sense to toss out defaults in favor of them.
#3 Updated by Elmer de Looff about 13 years ago
- Category set to TemplateParser
#4 Updated by Elmer de Looff almost 13 years ago
- Status changed from New to Resolved
- % Done changed from 0 to 70
For-loops have been added to the templateparser
in a separate branch of the Underdark repository. The merge is pending more extensive testing and a good bit of code-cleanup and thorough documentation.
Example template:
<html>
<body>
<ul>
{{ for person in [names] }}
<li>Hello [person]</li>
{{ endfor }}
</ul>
</body>
</html>
When this template is parsed with
names=('Tom', 'Dick', 'Harry')
, the output will be (barring copious whitespace differences):
<html>
<body>
<ul>
<li>Hello Tom</li>
<li>Hello Dick</li>
<li>Hello Harry</li>
</ul>
</body>
</html>
#5 Updated by Elmer de Looff almost 13 years ago
- Status changed from Resolved to Closed
- % Done changed from 70 to 100
All relevant tests and documentation has been added, and the development branch has been merged in to the production branch.
#6 Updated by Elmer de Looff almost 13 years ago
{{ if }} statement now included in templateparser. For a minor example, refer to issue #61.
Added first testcase checking the behaviour of FOR-loops: Test succeeds! Declaring v1.0 of templateparser. Lots of code cleanup still needed before we can merge this, but this resolves #252.