-
-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jinja2 extension #89
Comments
@dperetti No, no plans I'm afraid. I'm not familiar with Jinja2 at all. I wouldn't know where to start. |
It's really worth it. When you start using it, you don't want to go back ! Very powerful and very clean... and wagtail supports it ! |
@dperetti If you'd be happy to add tests and documentation to go along with it, a pull-request would be welcome, thanks. |
My temporary solution is to copy
class MenuExtension(Extension):
def __init__(self, environment):
super().__init__(environment)
environment.globals.update({
'main_menu': jinja2.contextfunction(main_menu),
'sub_menu': jinja2.contextfunction(sub_menu),
})
c = copy(context)
c.update({
'menu_items': {}
}) in Jinja2, you must do c = context.parent
ctx = {'menu_items': {})
c.update(ctx) |
@hongquan I can't seem to get your solution to work; the |
@jorenham |
Is anyone willing to work on a pull-request to add this functionality? I've just merged some changes into So, i'm hoping it should just be a case of adding the Extension somewhere, and adding some docs/tests? |
I wanted to work on it, but currently I shift my focus away my web app which uses wagtailmenus. I have working code, but only cover some cases (no modification for |
Thanks for replying @hongquan. I'd really like to avoid having an entirely separate set of tags to maintain if possible, as it will make further development difficult. I'm hoping we can get away with changing just enough in the original tags so that they work for both engines, then just define the Extension itself in a separate file. Is this realistic/possible? I'm guessing we'll also need to add jinja-compatible versions of all included menu templates too? This I can live with, as template changes are incredibly rare. What I'm most unsure about is how to extend the test suite to ensure Jinja compatibility remains intact once it's in place. But I suppose that can be tackled last of all. Even if you didn't do the work yourself, any advice you (or anybody else) can share on the above topics would be much appreciated. |
I think it is possible, like the upstream wagtail does. |
I have pending PR #190 which is hoped to solve this. With that PR being merged, you can setup like this to use Jinja2 template:
import jinja2
from jinja2.ext import Extension
from wagtailmenus.templatetags.menu_tags import main_menu, sub_menu
class MenuExtension(Extension):
def __init__(self, environment):
super().__init__(environment)
environment.globals.update({
'main_menu': jinja2.contextfunction(main_menu),
'sub_menu': jinja2.contextfunction(sub_menu),
})
TEMPLATES = (
{
'NAME': 'django-jinja',
'BACKEND': 'django_jinja.backend.Jinja2',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'environment': 'project.jinja.environment',
'match_extension': '.jinja',
'extensions': DEFAULT_EXTENSIONS + [
'wagtail.wagtailcore.jinja2tags.core',
'wagtail.wagtailadmin.jinja2tags.userbar',
'wagtail.wagtailimages.jinja2tags.images',
# My extension for wagtailmenu
'project.jinja.MenuExtension',
],
}
},
)
My convention is that Jinja2 template files are named with .jinja extension and saved in templates folder. |
Hi @hongquan. My apologies. I've had to revert the merge of your PR (#190) for now, because I'm not 100% sure about the reasoning for some of the changes (I should have flagged this before merging, but I wasn't really thinking clearly this past week, due to other pressures). There are quite a few backwards-incompatible changes in the pull that I need to explain in release notes etc, so I really need to understand things a little better before I merge again. The main thing i'm unsure about is why the switch to Django's
I took this to mean that if the Jinja template engine (or any other engine) is being used to render a Or, is the problem not that the right engine is used to find the template, but that the If anyone can help explain, that would be much appreciated :) Thanks. |
No, the But if you are using Jinja2 template, the That is why I have to use |
Okay, thanks for confirming @hongquan. In this case then, I think I need to do a little more work to make the changes backwards compatible. I'll remerge your work again as it is, and work on the backwards compatibility changes as a new PR. |
When I use
with the jinja2 extension @hongquan posted, I get the error
I see that this is because of function call that is deprecated and will be removed in 2.8, but how can I work around this for now? wagtailmenus 2.6.0 |
Hi @jorenham, The release notes should tell you everything you need to know (http://wagtailmenus.readthedocs.io/en/stable/releases/2.6.0.html). It sounds like you just need to add |
@ababic Yup, that was it. Thanx! |
The problem of the solution of @hongquan is that #main_menu.html
{% for item in menu_items %}
<a href="{{ item.href }}">{{ item.text }}</a><br>
{% if item.has_children_in_menu %}
{# evaluates to `true` the first time only #}
{% if sub_menu.__class__.__name__ == 'function' %}
{{ sub_menu(item, template='pages/menus/sub_menu.html') | safe }}
{% else %}
:(<br>{{ sub_menu.render_to_template()|safe }}
{% endif %}
{% endif %}
{% endfor %} 😕 |
Thank for reporting. I will try to fix. |
Any progress? |
Sorry, these days I am occupied by some other projects, not coming back to work on Wagtail yet. |
If anyone is still struggling to use the |
Anyone working on this? For me it looks, like multiple In Django these template tags are really tags, but in Jinja2 they are functions(), so they lives in context. But If I look at class SectionMenu(DefinesSubMenuTemplatesMixin, MenuFromPage):
menu_short_name = 'section' # used to find templates
menu_instance_context_name = 'section_menu'
related_templatetag_name = 'section_menu'
...
class ChildrenMenu(DefinesSubMenuTemplatesMixin, MenuFromPage):
menu_short_name = 'children' # used to find templates
menu_instance_context_name = 'children_menu'
related_templatetag_name = 'children_menu'
...
class SubMenu(MenuFromPage):
menu_short_name = 'sub' # used to find templates
menu_instance_context_name = 'sub_menu'
related_templatetag_name = 'sub_menu'
...
class AbstractMainMenu(DefinesSubMenuTemplatesMixin, MenuWithMenuItems):
menu_short_name = 'main' # used to find templates
menu_instance_context_name = 'main_menu'
related_templatetag_name = 'main_menu'
content_panels = panels.main_menu_content_panels
menu_items_relation_setting_name = 'MAIN_MENU_ITEMS_RELATED_NAME'
...
class AbstractFlatMenu(DefinesSubMenuTemplatesMixin, MenuWithMenuItems):
menu_short_name = 'flat' # used to find templates
menu_instance_context_name = 'flat_menu'
related_templatetag_name = 'flat_menu'
base_form_class = forms.FlatMenuAdminForm
content_panels = panels.flat_menu_content_panels
menu_items_relation_setting_name = 'FLAT_MENU_ITEMS_RELATED_NAME'
... In my opinion, in Django it is fine, tag Please can anyone else double check my idea? Thank you. So I see two possible solutions:
As a workaround I use this code: import jinja2
from jinja2.ext import Extension
from wagtailmenus.templatetags.menu_tags import (
main_menu,
flat_menu,
section_menu,
children_menu,
sub_menu,
)
class WagtailmenusExtension(Extension):
def __init__(self, environment):
super().__init__(environment)
self.environment.globals.update({
'main_menu_jinja2': jinja2.pass_context(main_menu),
'flat_menu_jinja2': jinja2.pass_context(flat_menu),
'section_menu_jinja2': jinja2.pass_context(section_menu),
'children_menu_jinja2': jinja2.pass_context(children_menu),
'sub_menu_jinja2': jinja2.pass_context(sub_menu),
})
# Nicer import names
menu_tags = WagtailmenusExtension and in my templates I use |
Looks like Jinja2 uses context differently. I realized, that in Jinja2 But this in Jinja2 never get reset to Hope this report make sense. |
I feel like this should be explained in detail in the docs. This issue is pretty much the only place jinja2 is mentioned - apart from the release notes that says it's supported. |
Got wagtailmenus 3.1.9 working with jinja2 by doing as described here. Everything works nicely, however the active_class is always empty.
Anything I may have missed? |
Any plan to support wagtailmenus' tags through a jinja2 extension ? That would be awesome !
The text was updated successfully, but these errors were encountered: