I am trying to understand how Jinja2 passes variables from my flask app with flask- login to a jinja2 template and how it interacts with the User class variable is_authenticated. Currently in my app this conditional statement seem to fully work as intented:
{% if current_user.is_authenticated %}
<h1>Hi {{ current_user.username }}!</h1>
{% endif %}
But if I try to be more explicit in my jinja2 statement for once, nothing seem to work out, among others that I have tried:
{% if current_user.is_authenticated is sameas true %}
{% if current_user.is_authenticated == "True" %}
{% if current_user.is_authenticated == true %}
{% if current_user.is_authenticated == "1" %}
...plus a whole battery of things that seem not to be working. When I print out the variable user.is_authenticated is is:
<bound method User.is_authenticated of <User 1>>
How can I explicitly check this one for being true? Why is jinja2 accepting {% if current_user.is_authenticated %} but not a more explicit statement like {% if current_user.is_authenticated == true %} ? What am I missing?
Related
I am passing a query to the context of a normal Django template. Within the template I want to do this:
{% for item in sample %}
{% if item.status != "Close" %}
Show something
{% endif %}
{% endfor %}
However, the if condition is never evaluated to True.
I can perfectly access the field for each item in the template like {{item.status}} but the if part is not working.
Is there a special way to write the if statement so the template expands item.status to its value before evaluating the comparison?
for com in applications:
for number in range(len(appliers)):
if connector[number] == com.id:
print(appliers[number].name)
I am having problem with parsing this python code to django template language... keep getting error saying that it can't parse. please can anyone parse it for me...
You can create a filter for getting a range in template:
#register.filter(name='times')
def times(number):
return range(number)
And then in template you can do:
{% for number in appliers|length|times %}
{% if connector.number == commission.id %}
{{appliers.number.name}}
{% endif %}
{% endfor %}
Alternatively you can pass range(len(appliers)) as a context variable to your template from a view.
{% extends "../../../views/"{{variable}}"/templates/one-column.volt" %}
or
{% extends "../../../views/"~variable~"/templates/one-column.volt" %}
or any other way to make that work.
how to make it work like above. anyone can help me?
framework phalcon
If you are using variable inside the extend or include you have to omit the template extension. In your case it is .volt.
Try like this:
{% extends "../../../views/"~ variable ~"/templates/one-column" %}
Another example:
// When using variable inside the include
{% set folderVar = 'partials' %}
{% include "_layouts/"~ folderVar ~"/header" %}
// Without using variable
{% include "_layouts/partials/header.phtml" %}
Explanation of this behaviour: https://docs.phalconphp.com/en/latest/reference/volt.html#include
The template file i created contains this:
{% if type({'a':1,'b':2}) is dict %}
print "Oh Yes!!"
{% else %}
print "Oh No!!!"
{% endif %}
Then Jinja2 responds by saying:
TemplateAssertionError: no test named 'dict'
I am completely new to Jinja2 and Flask
You are looking for the mapping test:
{% if {'a': 1, 'b': 2} is mapping %}
"Oh Yes!"
{% else %}
"Oh No!"
{% endif %}
Jinja is not Python though, so you don't have access to all the builtins (type and print do not exist, for example, unless you add them to the context. In Flask, you do this with the context_processor decorator).
You don't actually need print at all. By default everything is output (unless you are in a child template that extends a parent, in which case you can do interesting things like the NULL Master fallback because only blocks with names available in the master template are output).
How about:
{% if {'a':1,'b':2} is mapping %}
print "Oh Yes!!"
{% else %}
print "Oh No!!!"
{% endif %}
see List of Builtin Tests for reference.
In case you want to get a custom type you can access field name like in this example:
{% if 'RelationField' in field.__class__.__name__ %}
<div class="col-md-1">
Manage object
</div>
{% endif %}
I'm not able to access template variable in TWIG macro.
Here is a simplified example:
{% set myname = "Ligio" %}
{{ _self.pagedurl(1) }}
{% macro pagedurl(page) %}
Hi {{ _self.myname }}! This is Page Num {{ page }}
{% endmacro %}
How can I access the variable myname without passing it to the macro?
You can not.
As stated in the documentation:
As PHP functions, macros don't have access to the current template
variables.
Your only solution is to pass the parameter to the macro:
{% import _self as flow %}
{{ flow.pagedurl(1, "Ligio") }}
{% macro pagedurl(page, myname) %}
Hi {{ myname }}! This is Page Num {{ page }}
{% endmacro %}
IMPORTANT NOTE:
You may have noticed in my example, I call {% import _self as flow %}.
This is something you MUST do:
When you define a macro in the template where you are going to use it,
you might be tempted to call the macro directly via _self.input()
instead of importing it; even if seems to work, this is just a
side-effect of the current implementation and it won't work anymore in
Twig 2.x.
http://twig.sensiolabs.org/doc/tags/macro.html
If you need to pass more than one global variable into the macro, you might find the _context variable useful:
{% macro mymacro(globalvars) %}
Value of the global variable pi is {{ globalvars.pi }}
{% endmacro %}
{% set pi = 3.14159 %}
{{ _self.mymacro(_context) }}
Ref: this or this answer.
You can set a global variable and access it anywhere in the template
$loader = new \Twig_Loader_Filesystem('path/to/templates');
$twig = new \Twig_Environment($loader);
$twig->addGlobal('V_Name', 'V_Value');