Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use nested template tags with arguments?

I would like to learn how I can use nested template tags where the child template tag takes an argument like below:

{% parent_tag {% child_tag arguments %} rest_of_parent_arguments %}

In the above line, I would like to use the return value of child tag as an argument to the parent tag.

like image 585
Tolga Avatar asked Oct 19 '09 23:10

Tolga


People also ask

Can templates be nested?

You can nest multiple templates to define increasingly specific layouts.

What is the purpose of creating a nested template folder structure?

Nested folders are a way to manage your codes on a more granular level, where you can quickly put subfolders within a main folder. Create subfolders within folders, move folders into existing folders to form subfolders, and create or move codes anywhere in a nested folder structure.

What does {% %} mean in Django?

The {% if %} tag evaluates a variable, and if that variable is “true” (i.e. exists, is not empty, and is not a false boolean value) the contents of the block are output. One can use various boolean operators with Django If Template tag.

Which template tag template variable is surrounded by?

A variable is a symbol within a template that outputs a value. Variable tags are surrounded by {{ and }} : My first name is {{ first_name }}. My last name is {{ last_name }}.


1 Answers

I would replace the child_tag with a custom filter. Something like:

{% parent_tag argument1|filtername:argument2 rest_of_parent_arguments %}

assuming "arguments" consists of at most 2 arguments. See here for custom filters:

http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters

like image 64
Gabriel Ross Avatar answered Oct 09 '22 23:10

Gabriel Ross