Feature #61
Template parser - default values
Description
The abilety to assign a default value to a template.
When the template parser finds a tag unused replace is with the default value if availible.
Default values could be described in mutible formats
inline:¶
[<tag>|default=<default value tag>]
example:
[search_result|default=<p>No search results found.</p>]
[icon|default=<img src='/icon_default.png'>]
Django style (multyline):¶
[<tag>]
<default value tag>
[END]
example:
[search_result]
<p>No search results found.</p>
[END]
[icon]
<img src='/icon_default.png'>
[END]
Related issues
Associated revisions
History
#1 Updated by Jan Klopper over 13 years ago
Maybe we can overload the function syntax.
[var|function|default]
If the var doesn't exists, see if the last function also does not exists, and if so use that string as the value.
Its a hack though, but I like the | as an OR.
Maybe || would be better though.
#2 Updated by Elmer de Looff over 13 years ago
- Status changed from New to Feedback
- Assignee set to Jan Klopper
| is already used as a pipe, pushing the result (of the previous) towards the (next) function.
We should not assign a second meaning to the pipe character. Double pipes are going to reduce readability significantly -- "is this an error, or a default value?"
The Django multiline default is an option, but then we're special casing the word END, which seems a tad hacky as well.
What I could see happen is a {default} syntax that follows the last funtion but preceeds the closing bracket:
<p>This is a demonstration of the template default. Kittens are:</p>
<ul>
<li>Cute</li>
<li>[behaviour|lolcat{freaking annoying}]</li>
</ul>
If behaviour
is specified, this would process behaviour with the lolcat
temlate function and place it. If behaviour
is left unspecified, this would put down "freaking annoying" as the default.
#3 Updated by Jan Klopper over 13 years ago
When would the default value kick in?
When the original argument cannot be resolved / is None?
Or when its an emptystring?
#4 Updated by Jan Klopper over 13 years ago
- Assignee changed from Jan Klopper to Elmer de Looff
#5 Updated by Elmer de Looff over 13 years ago
- Assignee changed from Elmer de Looff to Jan Klopper
Definitely when it cannot be resolved.
I'm not sure about boolean truthness, placing zeroes would be tricky if we rely on truthiness.
#6 Updated by Elmer de Looff over 13 years ago
- Category set to TemplateParser
#7 Updated by Elmer de Looff almost 13 years ago
- Status changed from Feedback to Resolved
- % Done changed from 0 to 70
This can now be achieved using {{ if }} statements in the templateparser.
<p>This is a demonstration of the template default. Kittens are:</p>
<ul>
<li>Cute</li>
<li>{{ if [behaviour] }} [behaviour|lolcat] {{ else }} rather annoying {{ endif }}</li>
</ul>
This is more verbose than a quick default, but mostly the default will involve more than a single statement, and this solution quickly becomes preferable and readable.
#8 Updated by Elmer de Looff almost 13 years ago
- Status changed from Resolved to Closed
- % Done changed from 70 to 100
Seems to be good.
Added {{ if }} statements to the templateparser. This allows richer expressions in templates. if, elif and else are properly supported. This completely resolves #252 and makes obsolete issue #61.