title: Is there a Django template tag that lets me set a context variable?
body: <p>I want to be able to set variables in a template to string values.  I wrote a tag, but it doesn't seem to change the context.  The intended use is:</p>
  
  <pre><code>{% define "a string" as my_var %}
  </code></pre>
  
  <p><strong>Update (solved):</strong> </p>
  
  <pre><code>class DefineNode(Node):
      def __init__(self, var, name):
          self.var = var
          self.name = name
  
      def __repr__(self):
          return "&lt;DefineNode&gt;"
  
      def render(self, context):
          context[self.name] = self.var
          return ''
  
  @register.tag
  def define(parser, token):
      """
      Adds a name to the context for referencing an arbitrarily defined string.
  
      For example:
  
          {% define "my_string" as my_string %}
  
      Now anywhere in the template:
  
          {{ my_string }}
      """
      bits = list(token.split_contents())
      if (len(bits) != 4 or bits[2] != "as") or \
          not (bits[1][0] in ('"', "'") and bits[1][-1] == bits[1][0]):
          raise TemplateSyntaxError("%r expected format is '\"string\" as name'" % bits[0])
      else:
          value = bits[1][1:-1]
      name = bits[3]
      return DefineNode(value, name)
  </code></pre>