Variable not expanded inside an included selmer template - clojure

I have a top-level template where I want to use a "fragment" template inside a for cycle but I'm not able to pass in the variable value:
{% for item in coll %}
{% include "fragment.html" with name="slack" item=item %}
{% endfor %}
item and name is then used in the fragment.html template:
<div>
<label>
<input
title="{{item.id}}"
id="{{name}_{{item.id}}_active"
name="{{name}}-{{item.id}}_active"
...
/>
While the name parameter is expanded properly (its value is hardcoded in the parent template), the item parameter is not (its value is passed in as is).
Do I need to use a different syntax for that or it's just not supported?

The include tag splices in the included template. This means that any variables within scope of the parent template will be available to the included template. The with operator allows you to supply default values, which are not interpreted. Saying item=item is effectively saying item|default:"item", which is to say that item is redefined as "item".
See https://github.com/yogthos/Selmer#including-templates

Related

Django. How to kind of "spread" object when passing as with parameter to include?

I have a component which i include like that:
{% include "components/item_link.html" with target="_blank" href=link.href icon=link.icon text=link.text %}
There are lots of repetitions of link.. Is there a way to "spread" object and include it like that?
{% include "components/item_link.html" with target="_blank" ...link %}
Assign the link variable directly like:
{% include .... link=link %}
Then in the included template use their single values as:
link.href
link.icon
link.text
If you can't modify the included template code, you can create an intermediate template which assigns the above variables and call the original include with their respective href=link.href icon=link.icon text=link.text so you avoid to repeat everything every time you use it.
Update
Add a link parameter, you can still use the parameters but then in the included template you can also detect if link is set when the parameter isn't set.
For example:
{% include "components/item_link.html" with target="_blank" link=link %}
in components/item_link.html:
<a href="{%if href%}{{href}}{%elif link%}{{link.href}}{%endif%}> ...

`{% include "blah.html" with x="..." only %}` with default value for x

In a template, I am using an include like this:
{% include "blah.html" with x="..." only %}
and I can use x in blah.html.
Now, I would like blah.html to set a default value for x if it is not passed by the "parent" template:
{% include "blah.html" only %}
Since there is apparently no builtin template tag to set a variable in a template, I don't know how this could be achieved...

Django templates: optional variable in a with

This might be a pretty simple question; but I can't seem to find the 'correct' approach.
I have a template, in which I include a different template three times. I pass quite a few variables along with the include. The problem is that one of them is optional.
{% with
title="Add object"
type="function"
function_type="FUNCTION"
inputs=function_create_form
project_id=project.id
parent_function_id=root_function.id
{% if root_function.type == 'OBJECT' %}
parent_function_id=root_function.id
{% endif %}
%}
{% include "./popup-form.html" %}
{% endwith %}
(I'm aware that this should be on one line in the template, for some reason. But this is way more readable.)
The gist of it is that I only want to pass the variable in the if statement, if it is of the correct type. It will always have a value, but not always the correct one. Sadly though, Django doesn't seem to tolerate if statements in with's.
To clarify, the variable should only be passed to the template if it is of the correct type. The included template is a popup form used to add objects. These objects have different types; ROOM, OBJECT, and FUNCTION. These objects can in turn have a parent object, which is exactly what I want to pass. But this parent should only be passed to the popup form if the user is viewing the correct object type. This is determined by the if statement
I also tried to do this, same result:
{% include "./popup-form.html" with
title="Add object"
type="function"
function_type="OBJECT"
inputs=function_create_form
project_id=project.id
{% if root_function.type == 'OBJECT' %}
parent_function_id=root_function.id
{% endif %}
%}
Error: 'with' received an invalid token: '{%'
I could perhaps make an if statement outside of the with and set the value of the variable to an empty string. But there ought to be a better way, right? Doing this in the view is not an option, since the variable will always need to have a value.
The complexity of this statement should be a clue that this is the wrong approach.
If you want to pass multiple variables to an included template, you should probably be looking at an inclusion tag instead. You can then use any combination of positional and keyword arguments to render the template.

How can I access part of a tuple which is returned from a custom tag in a Django template?

I have a function that returns a non-current user's first and last name as a tuple, using the following custom tag, where foo is a username from active directory:
{% name foo %}
I would like to access the first element of the tuple, and the only syntax I could think of is:
{{ {% name foo %}.0 }}
which is incorrect. How can I accomplish this?
You can't do that. If your tag is simply returning a value, there's no way to get the first part of it.
Instead, write an assignment tag, which sets a variable in the context. Then you can do this:
{% name foo as bar %}
{{ bar.0 }}

Django: cannot pass variable to included template?

I got a problem where I want to use template including in Django.
Here is the real example:
I got 3 files:
home.html (will get the context variable passed from Views)
base.html (the skeleton template file)
and the header.html (included by base.html).
If I put the code below directly in base.html without including header.html, the {{title}} variable passed from home is correctly called. But if I include the header.html in base.html, the {{title}} variable's value cannot be called.
<title>{% block title %}{% endblock %} | {{ SITE_INFO_TITLE }}</title>
Is there any solution to this problem? Thanks.
Could you just pass in a variable within the {% include %} tag? It's documented here: https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#include
{% include "name_snippet.html" with person="Jane" greeting="Hello" %}
As far as I know blocks and variable are distinct in django.
If you want to pass title as a context variable you have to set it using a declaration in base.html such as :
{% include "header.html"%}
Which in turn contains :
{% block title %} {{title}} {%endblock%}
You can also set it in home like this.
{% block title %} Home page {%endblock%}
But I also try to set in the template context.
Without the title block.
def test_view(ctx):
xa = { "title":"Sommaire"}
return render_to_response("test.html",xa)
I think you can also see the with template tag I think it is possible to set a context variable using this tag.
You can use Inclusion Tags to render an additional template from within a Django template. You can additionally pass the 'child' template context from the 'parent' template.
It's a little involved for your use case but it solves your problem. I tend to use it when I'm looping a list to render each item with a custom template. I can then reuse that template elsewhere without duplicating the markup if I need to render another item of the same type.