Skip to content

Instantly share code, notes, and snippets.

@bergantine
Last active July 25, 2020 15:58
Show Gist options
  • Save bergantine/4128679 to your computer and use it in GitHub Desktop.
Save bergantine/4128679 to your computer and use it in GitHub Desktop.
Django Template Tag and Filter Test File. #django #djangotemplate
{# asdf #}
{# {{ var }} {% if %} ... {% endif %} #}
{{ asdf }}
<script type="text/javascript">
fruits = new Array('apples', 'oranges', 'bananas');
for (var i=0; i < fruits.length; i++) {
alert(fruits[i]);
}
</script>
<style type="text/css" media="screen">
p {
background: #336600;
}
h1 {
color: #fff;
border-width: 1px;
border-style: solid;
background: url('/images/foo.jpg') no-repeat left top;
}
</style>
{% comment %}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
{% endcomment %}
{% if you.friends.count > 5 %}You're popular!{% endif %}
{% if country != "NZ" %}Come and visit New Zealand!{% endif %}
{% if messages|length > 3 %}...{% endif %}
{% if not movie %}
<!-- HTML comment -->
{% for date in days %}
{% ifchanged %}<h3>{{ dt|date:"F" }}</h3>{% endifchanged %}
<a href="{{ dt|date:"M/d"|lower }}/">{{ dt|date:"j" }}</a>
{% endfor %}
{% for match in matches %}
<div style="background-color:
{% ifchanged match.ballot_id %}
{% cycle "red" "blue" %}
{% else %}
grey
{% endifchanged %}
">{{ match }}</div>
{% endfor %}
<a href="{{ the_url }}">I'm linking to {{ the_url }}</a>
{% url path.to.view as the_url %}
{% if the_url %}
<a href="{{ the_url }}">Link to optional stuff</a>
{% endif %}
<img src="bar.gif" height="10" width="{% widthratio this_value max_value 100 %}" />
{% for o in some_list %}
<tr class="{% cycle rowvalue1 rowvalue2 %}">
...
</tr>
{% endfor %}
{% for o in some_list %}
<tr class="{% cycle 'row1' rowvalue2 'row3' %}">
...
</tr>
{% endfor %}
<tr class="{% cycle rowcolors %}">...</tr>
<tr class="{% cycle rowcolors %}">...</tr>
<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% empty %}
<li>Sorry, no athlete in this list!</li>
{% endfor %}
</ul>
<ul>
{% if athlete_list %}
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% endfor %}
{% else %}
<li>Sorry, no athletes in this list.</li>
{% endif %}
</ul>
<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% endfor %}
</ul>
<h1>Archive for {{ year }}</h1>
<ul>
{% for gender in gender_list %}
<li>{{ gender.grouper }}
<ul>
{% for item in gender.list %}
<li>{{ item.first_name }} {{ item.last_name }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
<p style="background-color: #222;"></p>
{% for o in some_list %}
<tr class="{% cycle 'row1' 'row2' %}">
...
</tr>
{% endfor %}
{% filter force_escape %}
{% cycle var1 var2 var3 %}
{% endfilter %}
{% cycle row1,row2,row3 %}
{% filter force_escape|lower %}
This text will be HTML-escaped, and will appear in all lowercase.
{% endfilter %}
{% if var1 %}
{{ var1|safe }}
{% else %}{% if var2 %}
{{ var2|safe }}
{% else %}{% if var3 %}
{{ var3|safe }}
{% endif %}{% endif %}{% endif %}
{% firstof var1 var2 var3 %}
{% filter force_escape %}
{% firstof var1 var2 var3 "fallback value" %}
{% endfilter %}
{% for x, y in points %}
There is a point at {{ x }},{{ y }}
{% endfor %}
{% for key, value in data.items %}
{{ key }}: {{ value }}
{% endfor %}
{% if athlete_list %}
Number of athletes: {{ athlete_list|length }}
{% else %}
No athletes.
{% endif %}
{% if athlete_list and coach_list %}
Both athletes and coaches are available.
{% endif %}
{% if not athlete_list %}
There are no athletes.
{% endif %}
{% if athlete_list or coach_list %}
There are some athletes or some coaches.
{% endif %}
{% if not athlete_list or coach_list %}
There are no athletes or there are some coaches (OK, so
writing English translations of boolean logic sounds
stupid; it's not our fault).
{% endif %}
{% if athlete_list and not coach_list %}
There are some athletes and absolutely no coaches.
{% endif %}
{% if athlete_list and coach_list or cheerleader_list %}
{% if athlete_list %}
{% if coach_list or cheerleader_list %}
We have athletes, and either coaches or cheerleaders!
{% endif %}
{% endif %}
{% if athlete_list or coach_list or parent_list or teacher_list %}
{% for day in days %}
{% ifchanged day.date %} {{ day.date }} {% endifchanged %}
{% ifchanged day.hour day.date %}
{{ day.hour }}
{% endifchanged %}
{% endfor %}
{% ifequal user.id comment.user_id %}
...
{% endifequal %}
{% ifequal user.username "adrian" %}
...
{% endifequal %}
{% include "foo/bar.html" %}
{% include template_name %}
It is {% now "jS F Y H:i" %}
It is the {% now "jS o\f F" %}
people = [
{'first_name': 'George', 'last_name': 'Bush', 'gender': 'Male'},
{'first_name': 'Bill', 'last_name': 'Clinton', 'gender': 'Male'},
{'first_name': 'Margaret', 'last_name': 'Thatcher', 'gender': 'Female'},
{'first_name': 'Condoleezza', 'last_name': 'Rice', 'gender': 'Female'},
{'first_name': 'Pat', 'last_name': 'Smith', 'gender': 'Unknown'},
]
{% regroup people by gender as gender_list %}
{% regroup people|dictsort:"gender" by gender as gender_list %}
{% spaceless %}
<p>
<a href="foo/">Foo</a>
</p>
{% endspaceless %}
{% spaceless %}
<strong>
Hello
</strong>
{% endspaceless %}
{% ssi /home/html/ljworld.com/includes/right_generic.html %}
{% url path.to.some_view arg1,arg2,name1=value1 %}
{% url path.to.view arg, arg2 as the_url %}
{% with business.employees.count as total %}
{{ total }} employee{{ total|pluralize }}
{% endwith %}
{{ value|add:"2" }}
{{ value|addslashes }}
{{ value|capfirst }}
"{{ value|center:"15" }}"
{{ value|cut:" " }}
{{ value|date:"D d M Y" }}
{{ value|date }}
{{ value|default:"nothing" }}
{{ value|default_if_none:"nothing" }}
{{ value|dictsort:"name" }}
{{ value|divisibleby:"3" }}
{{ value|escapejs }}
{{ value|filesizeformat }}
{{ value|first }}
{{ value|fix_ampersands }}
{{ value|get_digit:"2" }}
{{ value|iriencode }}
{{ value|join:" // " }}
{{ value|last }}
{{ value|length }}
{{ value|length_is:"4" }}
{{ value|linebreaks }}
{{ value|linebreaksbr }}
{{ value|linenumbers }}
"{{ value|ljust:"10" }}"
{{ value|lower }}
{{ value|make_list }}
{{ value|phone2numeric }}
You have {{ num_messages }} message{{ num_messages|pluralize }}.
You have {{ num_walruses }} walrus{{ num_walruses|pluralize:"es" }}.
{{ value|random }}
{{ value|removetags:"b span"|safe }}
"{{ value|rjust:"10" }}"
{{ some_list|safeseq|join:", " }}
{{ some_list|slice:":2" }}
{{ value|slugify }}
{{ value|stringformat:"s" }}
{{ value|striptags }}
{{ value|time:"H:i" }}
{{ value|title }}
{{ value|truncatewords:2 }}
{{ value|truncatewords_html:2 }}
{{ value|upper }}
{{ value|urlencode }}
{{ var|unordered_list }}
{{ value|urlize }}
{{ value|urlizetrunc:15 }}
{{ value|wordcount }}
{{ value|wordwrap:5 }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment