title: Render returned string in django template
body: <p>I have been working a lot with Django's template language lately, and it works well in most cases, but recently I've come across a small problem it hasn't been able to solve.<br>
  I'm iterating over a bunch of database objects, and building a button in the template from each of them. Something like this:</p>
  
  <pre><code>{% for item in items %}
      &lt;button id="{{ item.action.id }}"&gt;
          {{ item.action.text }}
      &lt;/button&gt;
  {% endfor %}
  </code></pre>
  
  <p>This works fine, but my <code>Action</code> class is inheritable, and may have a different button structure. Say I wanted to sometimes have a javascript function attached as well. My first though was <em>well, let's just create a render method in my class</em>, and then call that in my template.</p>
  
  <pre><code>class ScriptAction(Action):
  
      def render(self):
          return '''
              &lt;button id="{}" onclick={}&gt;
                  {}
              &lt;/button&gt;'''.format(self.id, self.func, self.text)
  </code></pre>
  
  <p>Because no the template is much more elegant, and doesn't rely on a fixed button structure:</p>
  
  <pre><code>{% for item in items %}
      {{ item.action.render|safe }}
  {% endfor %}
  </code></pre>
  
  <p>And now for my actual problem: <strong>what if this rendered string needs further processing by django?</strong><br>
  Maybe render method returns a string like</p>
  
  <pre><code>&lt;button id="action-button-id" onclick="console.log('clicked')"&gt;
      {{ item.name }}
  &lt;/button&gt;
  </code></pre>
  
  <p>Currently, <code>{{ item.name }}</code> will not be rendered in the template loop. Is there a way of getting around this?  </p>
  
  <p>Perhaps I making this more complicated than it should be, but I've looked through the Django built-in tags and filters, even looked at <a href="https://docs.djangoproject.com/en/2.0/howto/custom-template-tags/#advanced-custom-template-tags" rel="nofollow noreferrer">writing you own parser</a>.  But at this point something inside me told me that I was being obsessive. This is actually just a small problem, and would require a fair amount of time, so -</p>
  
  <ul>
  <li>is this more effort than it is worth?</li>
  <li>is writing complex parsers like this secure?</li>
  <li>is the Django template language even able to do something like this?</li>
  </ul>
  
  <p>If you could hone in on some of these points, I would really appreciate it. This problem is really bugging me.<br>
  Thanks in advance!</p>