22

I am already trying to concatenate like this:

{% for choice in choice_dict %}
    {% if choice =='2' %}
        {% with "mod"|add:forloop.counter|add:".html" as template %}
            {% include template %}
        {% endwith %}                   
    {% endif %}
{% endfor %}    

but for some reason I am only getting "mod.html" and not the forloop.counter number. Does anyone have any idea what is going on and what I can do to fix this issue? Thanks alot!

gurney alex
  • 13,247
  • 4
  • 43
  • 57
Ethan
  • 251
  • 1
  • 2
  • 8

3 Answers3

51

Your problem is that the forloop.counter is an integer and you are using the add template filter which will behave properly if you pass it all strings or all integers, but not a mix.

One way to work around this is:

{% for x in some_list %}
    {% with y=forloop.counter|stringformat:"s" %}
    {% with template="mod"|add:y|add:".html" %}
        <p>{{ template }}</p>
    {% endwith %}
    {% endwith %}
{% endfor %}

which results in:

<p>mod1.html</p>
<p>mod2.html</p>
<p>mod3.html</p>
<p>mod4.html</p>
<p>mod5.html</p>
<p>mod6.html</p>
...

The second with tag is required because stringformat tag is implemented with an automatically prepended %. To get around this you can create a custom filter. I use something similar to this:

http://djangosnippets.org/snippets/393/

save the snipped as some_app/templatetags/some_name.py

from django import template

register = template.Library()

def format(value, arg):
    """
    Alters default filter "stringformat" to not add the % at the front,
    so the variable can be placed anywhere in the string.
    """
    try:
        if value:
            return (unicode(arg)) % value
        else:
            return u''
    except (ValueError, TypeError):
        return u''
register.filter('format', format)

in template:

{% load some_name.py %}

{% for x in some_list %}
    {% with template=forloop.counter|format:"mod%s.html" %}
        <p>{{ template }}</p>
    {% endwith %}
{% endfor %}
dting
  • 38,604
  • 10
  • 95
  • 114
  • @Ethan: this seems to answer your question doesn't it? – Edward Newell Jun 30 '14 at 00:49
  • @dting - This works great, unless you call it with `forloop.counter0`, in which case the `if value` line will return "False" for "0". To get around this, just change that line to `if value is not None`. – trubliphone Mar 24 '16 at 03:29
  • Worth noting that you need a __init__.py in the templatetags directory, and to restart the server, for this to register, as per: https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/#code-layout – Rikki Mar 13 '17 at 13:50
3

Try without using the block "with"

{% for choice in choice_dict %}
    {% if choice =='2' %}
       {% include "mod"|add:forloop.counter|add:".html" %}                   
    {% endif %}
{% endfor %} 
matuuar
  • 399
  • 4
  • 9
3

You probably don't want to do this in your templates, this seems more like a views job: (use of if within a for loop).

chosen_templates=[]
for choice in choice_dict:
  if choice =='2':
    {% with "mod"|add:forloop.counter|add:".html" as template %}
    template_name = "mod%i.html" %index
    chosen_templates.append(template_name)

Then pass chosen_templates to your template where you will have only

{% for template in chosen_templates %}
  {% load template %}
{% endfor %}

Also, I don't quite understand why you are using a dict to select the template with a number that is not in the dictionnary. for key,value in dict.items() may be what you are looking for.

user716625
  • 31
  • 2