Conditional¶
Processor name: conditional
Danger
Conditional blocks require an understanding of Python logical operators and expressions to function properly. The use of this tag requires co-ordination between authors and developers, as the variables used in the condition are expected when the result is rendered in a template engine.
You can include an conditional using the following text tag:
{conditional if condition="version == 'teacher'"}
This is text that only teachers should see.
{conditional end}
Tag Parameters¶
Conditional tags function slightly differently to other block text tags. The first conditional text tag must contain the if
flag, followed by optional conditional text tags containing the elif
flag, followed by the optional text tag containing the else
flag.
Note
Any strings within your Python expression need to be wrapped with single quotes ('
) or escaped double quotes (\"
).
Examples:
{conditional if="version == 'teacher'"}
{conditional if="version == \"teacher\""}
To create a set of conditional text tags, follow the following steps:
- First if block
- Contains
if
flag - Specifies that the conditonal is an opening if statement.- Contains
condition
parameter - A Python expression to evaluate if true. If true, the enclosed content will be displayed.
- Then optional else if blocks. Multiple of these blocks can be listed after one another.
- Contains
elif
flag - Specifies that the conditonal is an else if following an opening if statement.- Contains
condition
parameter - A Python expression to evaluate if true. If true, the enclosed content will be displayed.
- Then optional else block
- Contains
else
flag - Specifies that the conditional is an else following an opening if statement, and optional elif statements.
- Lastly conditional end block
Here is a more complicated example:
{conditional if condition="version == 'teacher'"}
This is text that only teachers should see.
{conditional elif condition="version == 'instructor'"}
This is text that only instuctors should see.
{conditional elif condition="version == 'coordinator'"}
This is text that only coordinators should see.
{conditional else}
This is text that everyone else should see.
{conditional end}
Example
The default HTML for a conditional is:
<remove>
{% autoescape false -%}
{{ "{% if " }}{{ if_expression }}{{ " %}" }}
{{ if_content }}
{% for elif_expression, elif_content in elifs.items() -%}
{{ "{% elif " }}{{ elif_expression }}{{ " %}" }}
{{ elif_content }}
{% endfor -%}
{% if has_else -%}
{{ "{% else %}" }}
{{ else_content }}
{% endif -%}
{{ "{% endif %}" }}
{% endautoescape -%}
</remove>
Using the following example tag:
{conditional if condition="version == 'teacher'"}
This is text that only teachers should see.
{conditional end}
The resulting HTML would be:
{% if version == 'teacher' %}
<p>This is text that only teachers should see.</p>
{% endif %}
Overriding HTML for Conditional¶
When overriding the HTML for conditionals, the following Jinja2 placeholders are available:
{{ if_expression }}
- The expression from the conditional argument.{{ if_content }}
- The content contained within the if expression.{{ elifs }}
- An ordered dicitionary of elif expressions to content.{{ has_else }}
- Whether an else was used in the full if statement.{{ else_content }}
- The expression from the conditional argument.
Example
For example, if you wanted to output a mako template you would providing the following HTML:
<remove>
{% autoescape false -%}
{{ "% if " }}{{ if_expression }}{{ ":" }}
{{ if_content }}
{% for elif_expression, elif_content in elifs.items() -%}
{{ "% elif " }}{{ elif_expression }}{{ ":" }}
{{ elif_content }}
{% endfor -%}
{% if has_else -%}
{{ "% else:" }}
{{ else_content }}
{% endif -%}
{{ "% endif" }}
{% endautoescape -%}
</remove>
with the following tag:
{conditional if condition="version == 'teacher'"}
This is text that only teachers should see.
{conditional elif condition="version == 'instructor'"}
This is text that only instuctors should see.
{conditional elif condition="version == 'coordinator'"}
This is text that only coordinators should see.
{conditional end}
would result in:
% if version == 'teacher':
<p>This is text that only teachers should see.</p>
% elif version == 'instructor':
<p>This is text that only instuctors should see.</p>
% elif version == 'coordinator':
<p>This is text that only coordinators should see.</p>
% endif