title: NamedTuples in Jinja2 template macros
body: <p>The saga continues, extended from the <a href="https://stackoverflow.com/questions/14016356/jinja2-macro-selecting-macro-or-dynamic-macro-calls">original thread</a>.</p>
  
  <p>So, I have a something to make macros within python code:</p>
  
  <pre><code>from flask import get_template_attribute
  from jinja2 import Template
  
      class InternalMacro(object):
          """
          Creates a macro given a name, internal macro text, and content to fill(as namedtuple(t.var), dict(k,v), list(i), or other)
          """
          def __init__(self, name = None,
                             text = None,
                             content_is = None):
              self.name = name
              self.macro_name = "{}_template".format(self.name)
              self.macro_var = "{}_macro".format(self.name)
              self.text = text
              self.content_is = content_is
              self.macro_txt = self.format_text
  
          @property
          def is_tuple(self):
              return "{{% macro {0}(t) %}}{1}{{% endmacro %}}".format(self.macro_var, self.text)
  
          @property
          def is_dict(self):
              return "{{% macro {0}(items) %}}{{% for k,v in items.iteritems() %}}{1}{{% endfor %}}{{% endmacro %}}".format(self.macro_var, self.text)
  
          @property
          def is_list(self):
              return "{{% macro {0}(items) %}}{{% for i in items %}}{1}{{% endfor %}}{{% endmacro %}}".format(self.macro_var, self.text)
  
          @property
          def format_text(self):
              return getattr(self, self.content_is)
  
          @property
          def return_template(self):
              return Template(self.macro_txt)
  
          @property
          def return_callable(self):
              return get_template_attribute(self.return_template, self.macro_var)
  </code></pre>
  
  <p>Which I pass namedtuples singly, as lists, or as dicts. This works when passing a list (haven't fully tested as dict, yet) but does not work when passing a single namedtuple. No matter what, so far, the namedtuple gets escaped as unicode. </p>
  
  <p>So given:</p>
  
  <pre><code>test_macro = InternalMacro('test', '{{ t }} &lt;div id="divvy"&gt;{{ t.var }}&lt;/div&gt;', 'is_tuple')
  
  test_macro.return_callable(Anamedtuple)
  </code></pre>
  
  <p>returns:</p>
  
  <pre><code>u'Anamedtuple(var="A VAR VALUE") &lt;div id="divvy"&gt;&lt;/div&gt;'
  </code></pre>
  
  <p>not:</p>
  
  <pre><code>u'Anamedtuple(var="A VAR VALUE")' &lt;div id="divvy"&gt;A VAR VALUE&lt;/div&gt;
  </code></pre>
  
  <p>If I do this as list, .var get called normally.</p>
  
  <p>What is going on that I'm missing and how do I circumvent this? The single namedtuple gets escaped, but a list does not. I could do the single one as a list and just pop the first, maybe seems unclean to me. Any suggestions on improving this appreciated as well.</p>
  
  <p>EDIT: </p>
  
  <p>Simple solution was to just reduce everything to a passed in list, eliminate single and dict options, just pass in a list of 1. Still I'd like to figure out what is going on there exactly.</p>
  
  <p>EDIT2: </p>
  
  <p>A deeper explore showed that the way I output the namedtuple generated the results I was seeing ie -</p>
  
  <pre><code>test_macro = InternalMacro('test', '{{ t }} &lt;div id="divvy"&gt;{{ t.var }}&lt;/div&gt;', 'is_tuple')
  </code></pre>
  
  <p>results in:</p>
  
  <pre><code>u'Anamedtuple(var="A VAR VALUE") &lt;div id="divvy"&gt;&lt;/div&gt;'
  </code></pre>
  
  <p>whereas:</p>
  
  <pre><code>test_macro = InternalMacro('test', '&lt;div id="divvy"&gt;{{ t.var }}&lt;/div&gt;', 'is_tuple')
  </code></pre>
  
  <p>results in </p>
  
  <pre><code>'&lt;div id="divvy"&gt;A VAR VALUE&lt;/div&gt;'
  </code></pre>
  
  <p>I guess the namedtuples get read once or....well any detailed explanation appreciated.</p>