Milano
Milano

Reputation: 18735

How to access {% url "name" %} inside 'include' tag

I can't figure out how to get value of {% url "name" %} inside another {% %} tag. In my case its include

{% include "file" with next={% url "name" %} %}

This doesn't work. Do you know what to do? I would surround the {% include.. by {% with but it would cause the same problem.

Upvotes: 0

Views: 707

Answers (1)

koniiiik
koniiiik

Reputation: 4382

Quick answer for django templates:

{% url "name" as my_url_context_var %}
{% include "file" with next=my_url_context_var %}

Lengthier explanation:

You cannot nest {% … %} or {{ … }}. The moment you open {{ … }}, you can only use context variables inside, optionally combined with template filters. Inside a {% … %}, you can only use whatever the tag you are using recognizes, but in general, that will be just a few arguments, some of which are static words (such as the as in the url above, or the with in the include), and often you can also access template variables and filters just like inside {{ … }}.

I'm not that familiar with jinja, but it seems that its include doesn't support the same kind of variable passing as django's include does, so here's an idea of what it might look like with jinja:

{% set next=url('name') %}
{% include "file" %}

Upvotes: 5

Related Questions