Reputation: 2798
I'm trying to pass a jinja2 variable to a function as a parameter but I am unsure of the syntax or method to do so.
I've tried including the reference in the jinja2 function call but this has not worked.
I've tested the function and it works with a simple string "test" and the value is render in the page.
HTML SECTION
...
<tbody>
{% for test in test_records %}
<tr>
<td class="trGrey"> {{ test.id }} </td>
<td class="trGrey"> {{ test.testname }} </td>
<td class="trGrey"> {{ test.created }} </td>
<td class="trGrey"> {{ test.recordtype }} </td>
<td class="trGrey"> {{ {{ test.recordtype }}|my_function }} </td>
</tr>
{% endfor %}
</tbody>
...
PYTHON FILE
from django import template
register = template.Library()
def my_function(value):
if value:
return value
return ''
register.filter('my_function', my_function)
I'd expect the input variable to be rendered to the page.
Any suggestions will be helpful thanks!
Upvotes: 2
Views: 1611
Reputation: 476557
This is not a template tag, but a template filter (note the register.filter(..)
), you just filter with a vertical bar:
{{ test.recordtype|my_function }}
This is described in the Jinja documentation on filters:
Variables can be modified by filters. Filters are separated from the variable by a pipe symbol (
|
) and may have optional arguments in parentheses. Multiple filters can be chained. The output of one filter is applied to the next.
Upvotes: 1