Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6542533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:14:22+00:00 2026-05-25T11:14:22+00:00

I am looking for a way to have a Jinja macro that calls different

  • 0

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:

{% macro menuitem(obj) %}
  {% set type = obj.__class__.__name__ %}
  {% if type == "ImageMenuItem" %}
    {{ imagemenuitem(obj) }}
  {% elif type == "FoobarMenuItem" %}
    {{ foobarmenuitem(obj) }}
  {% else %}
    {{ textmenuitem(obj) }}
  {% endif %}
{% endmacro %}

In pure Python, one can muck around with the module environment, e.g. globals()[x+'menuitem'], 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.

What better ways are there to achieve what I’m seeking?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:14:22+00:00Added an answer on May 25, 2026 at 11:14 am

    I have now solved my problem similarly to how fabrizioM suggested, with one notable difference: Since the menu item presentation can (and most of the time, does) contain HTML, I don’t want mess around with HTML markup directly in the present methods. So I ended up implementing the menu definitions in Python, the presentation in Jinja, with mutual recursion bridging the gap.

    Different types of menu items are represented by different subclasses:

    class MenuItem(object):
        def present(self, macromap):
            return macromap[type(self).__name__](self, macromap)
    
    class TextLink(MenuItem):
        def __init__(self, url, text):
            self.url, self.text = url, text
    
    class Section(MenuItem):
        def __init__(self, text, items):
            self.text, self.items = text, items
    
    class ImageLink(MenuItem):
        ...
    

    The macromap referenced above is a dict mapping the type of menu item to the macro implementing its represenation. It’s all defined in Jinja:

    {% macro TextLink(l, macromap) %}
      <a class="menuitem" href="{{l.url|escape}}">
        {{ l.text|escape }}
      </a>
    {% endmacro %}
    
    {% macro Section(s, macromap) %}
      <div class="heading">{{s.text}}</div>
      <ul class="items">
        {% for item in s.items %}
          <li>{{ item.present(macromap) }}</li>
        {% endfor %}
      </ul>
    {% endmacro %}
    
    {% set default_map = {'TextLink': TextLink, 'Section': Section, ...}
    

    The actual menu definitions are cleanly expressed as trees of MenuItem subclasses:

    main_menu = section("Main Menu", [
        section("Product Line 1", [
            TextLink("/products/...", "A product"),
            ...
        ]),
        section(...),
    ])
    

    To kick off the presentation, a template has to call the top level section’s present method, passing a macro map to specify how to present the menu, e.g. main_menu.present(default_map). As can best be seen in the Section macro, menu items can then ask their children to present themselves, whose present method will call yet another Jinja macro, and so on, recursively.

    Having to explicitly pass around the macro map is not very pretty, but it grants a valuable benefit: One can now easily render different representations of the menu data without touching the menu definitions at all. For example, macro maps may be defined to render the main website menu, or a variant for mobile devices (in case CSS doesn’t suffice), or an XML sitemap, or even a plain text version. (We actually ended up using this system for the website menu and sitemap cases.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a way to have a textbox that allows users to type
I am looking for a way to have complex URL mappings that would depend
I'm looking at a way to have a subversion repository to be basically a
I'm looking for a way to have a generic local cache for any object.
I'm looking for a way to have sql server treat a different file extension
I'm looking for a way to have a javascript/jquery function that references dynamically created
I'm looking for the most optimized way to have an object in JS; since
I'm looking for a way to have a function such as: myFunction({Key, value}, {Key2,
I am looking for a way to have a Matrix variable (i.e, a bunch
I am looking for a way to have output in the same manner as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.