Reputation: 83
This is my code:
{% if {{post.author.profile.image.url}} is None %}
When I run this code I get an error
Could not parse the remainder: '{{post.author.profile.image.url}}' from '{{post.author.profile.image.url}}'
How to solve this? And how to use a template tag inside a template tag
Upvotes: 2
Views: 6206
Reputation: 376
The first question has already answered. Regarding to this one:
And how to use a template tag inside a template tag
Short answer: you can't. Long answer: you can with help of the 'block assignment' tag ( http://jinja.pocoo.org/docs/2.10/templates/#block-assignments ) :
{% set somevar %}
... any number of tags here {{ post.author.profile.image.url }} ...
{% endset %}
...
{% if somevar is None %} ... {% endif %}
You can also (since Jinja2 2.10) apply filter to the assignment:
{% set somevar | default('Empty') %}
... any number of tags here {{ post.author.profile.image.url }} ...
{% endset %}
Upvotes: 0
Reputation: 77902
abdusco's already answered the first part of your question ("how to solve this").
wrt/ the second part:
how to use a template tag inside a template tag
The simple answer is: you can't, period. Why it's not possible becomes rather obvious once you understand how the template system works, and that would be mostly useless anyway (if you find yourself trying to use a tag within a tag then you're doing it wrong and there's a better way, really).
NB: when I say you can`t nest tags, I mean that you can't do this:
{% sometag arg={% some_other_tag %} %}
Now in your example, what you're trying to do is not "using a tag inside a tag", but using a context variable within a tag, and this is of course possible (else the template language would be rather hard to use), as shown in abdusco's answer. And you can also use filter expressions (apply a filter to a context variable) here, ie {% mytag some.variable|upper %}
, at least if the template tag is correctly implemented.
Upvotes: 0
Reputation: 11091
Use the value of expression directly, you shouldn't wrap it inside braces:
{% if post.author.profile.image.url is None %}
...
{% endif %}
Upvotes: 6