I want to split name with space and tag_name with underscore. How can I split through jinja templating
Html file:
{% for i in i|split:' ' %}
<tr>
<td>
<p>{{ i }}</p>
</td>
<td>
<p>{{ j }}</p>
</td>
</tr>
{% endfor %}
custom_tags.py from django import template register = template.Library()
@register.filter(name='split')
def split(obj, sub_strings):
list1 = obj.name.split(" ")
list2 = obj.tag_name.split("_")
if "" in list1:
list1.remove("")
return [list1, list2]
Your template tag isn't splitting based on the variable that you are passing to it, but it seems that isn't necessary as you split by both ' ' and '_'. So you can remove that altogether:
{% for i in i|split %}
@register.filter(name='split')
def split(obj):
list1 = obj.name.split(" ")
list2 = obj.tag_name.split("_")
if "" in list1:
list1.remove("")
return [list1, list2]
This gives you a list of two lists, presumably you want to show both because you have {{ i }} and {{ j }}? You just need to move your for loop to be around the <td> elements and not the table row:
<tr>
{% for i in i|split:' ' %}
<td>
<p>{{ i }}</p>
</td>
{% endfor %}
</tr>