title: Polymorphic macros in Jinja
body: <p>I am looking for a way to have a Jinja macro that calls different implementations depending on the type of object that is being passed. Basically, standard Python method polymorphism. Right now, I'm using an ugly workaround similar to this:</p>
  
  <pre><code>{% macro menuitem(obj) %}
    {% set type = obj.__class__.__name__ %}
    {% if type == "ImageMenuItem" %}
      {{ imagemenuitem(obj) }}
    {% elif type == "FoobarMenuItem" %}
      {{ foobarmenuitem(obj) }}
    {% else %}
      {{ textmenuitem(obj) }}
    {% endif %}
  {% endmacro %}
  </code></pre>
  
  <p>In pure Python, one can muck around with the module environment, e.g. <code>globals()[x+'menuitem']</code>, which isn't pretty but works very well. I've tried something similar using the Jinja context, but the latter doesn't seem to contain the macro definitions.</p>
  
  <p>What better ways are there to achieve what I'm seeking?</p>