The templatetag {% include %} in Django

Loads a template and renders it with the current context.

# 會把 template context 也帶進去
{% include 'new_style/_bottom_action_btns' %}

{% for repost in recommend_creatives %}
    # 會把 repost 這個參數也帶進去!
    {% include 'new_style/creative_small.html' %}
{% endfor %}

# 但是你也可以額外指定要傳進去的 context
{% include 'new_style/_bottom_action_btns' with item=item repost=None %}

# 或是用 only 來限制只傳「顯式地指定的參數」進去
{% include 'new_style/_bottom_action_btns' with item=item only %}

ref:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#std:templatetag-include

How to set a variable in Django template?

How to set a variable in Django template?

Creating a custom template tag for assigning variables in Django template.

in app/templatetags/common.py

from django import template
register = template.Library()

class SetVarNode(template.Node):

    def __init__(self, var_name, var_value):
        self.var_name = var_name
        self.var_value = var_value

    def render(self, context):
        try:
            value = template.Variable(self.var_value).resolve(context)
        except template.VariableDoesNotExist:
            value = ""
        context[self.var_name] = value

        return u""

@register.tag(name='set')
def set_var(parser, token):
    """
    {% set some_var = '123' %}
    """
    parts = token.split_contents()
    if len(parts) < 4:
        raise template.TemplateSyntaxError("'set' tag must be of the form: {% set <var_name> = <var_value> %}")

    return SetVarNode(parts[1], parts[3])

in app/templates/xxx.html

{% load common %}

{% if work and is_public_work %}
    {% ifequal link_class 'info_box pull-right' %}
        {% set ga_label = 'card' %}
    {% else %}
        {% set ga_label = ga_label_detail %}
    {% endifequal %}
    <a class="{{ ga_label }}">123</a>
{% endif %}

ref:
http://www.yihaomen.com/article/python/341.htm
http://www.soyoucode.com/2011/set-variable-django-template