-
-
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
Make Menu.get_template neutral to template engine #190
Merged
ababic
merged 5 commits into
jazzband:master
from
AgriConnect:feature/neutral-tpl-engine
Nov 15, 2017
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
985fd29
Make Menu.get_template neutral to template engine
hongquan 9ee9945
Add me to CONTRIBUTORS
hongquan 40a5b83
Add documentation about using with Jinja2 template.
hongquan 41b361c
More explanation in documentation about Jinja2
hongquan f0aed87
Revert "Add documentation about using with Jinja2 template."
hongquan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ Rendering menus | |
|
||
template_tag_reference | ||
custom_templates | ||
use_with_jinja2 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
.. _use_with_jinja2: | ||
|
||
=============================== | ||
Use with Jinja2 template engine | ||
=============================== | ||
|
||
|
||
If your project uses `Jinja2 <http://jinja.pocoo.org>`_ for template, the template for *wagtailmenus* has also be written in Jinja2. Assume that your source folder tree is like this:: | ||
|
||
+ project | ||
| +-- settings.py | ||
| +-- urls.py | ||
+ templates | ||
| +-- base.jinja | ||
| +-- menus | ||
+ manage.py | ||
|
||
|
||
Here is how to make *wagtailmenus* work with Jinja2: | ||
|
||
1. Create *jinja.py* file: | ||
|
||
.. code-block:: python | ||
|
||
import jinja2 | ||
from jinja2 import Environment | ||
from jinja2.ext import Extension | ||
from django.conf import settings | ||
from wagtailmenus.templatetags.menu_tags import main_menu, sub_menu | ||
|
||
|
||
def environment(**options): | ||
env = Environment(**options) | ||
env.globals.update({ | ||
'settings': settings, | ||
}) | ||
return env | ||
|
||
|
||
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), | ||
}) | ||
|
||
|
||
2. Setup ``TEMPLATES`` in *settings.py* (not complete): | ||
|
||
.. code-block:: python | ||
|
||
from django_jinja.builtins import DEFAULT_EXTENSIONS | ||
|
||
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 wagtailmenus | ||
'project.jinja.MenuExtension', | ||
], | ||
} | ||
}, | ||
) | ||
|
||
|
||
Here in my example, I use `django-jinja <https://github.com/niwinz/django-jinja>`_ for integrating Jinja2 with Django. My convention is that template files are named with ".jinja" and put in *templates* folder. | ||
|
||
3. Write your Jinja2 template for menu and change ``WAGTAILMENUS_DEFAULT_MAIN_MENU_TEMPLATE`` setting. | ||
|
||
.. code-block:: python | ||
|
||
WAGTAILMENUS_DEFAULT_MAIN_MENU_TEMPLATE = 'menus/main_menu.jinja' | ||
WAGTAILMENUS_DEFAULT_SUB_MENU_TEMPLATE = 'menus/sub_menu.jinja' | ||
|
||
You can copy original template of *wagtailmenus*, written in Django template syntax, and convert it to Jinja2. | ||
|
||
|
||
4. In your template content, insert menu like this: | ||
|
||
.. code-block:: jinja | ||
|
||
<div class="collapse navbar-collapse" id="myNavbar"> | ||
{{ main_menu() }} | ||
</div> | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this file for now - We'll work on Jinja-specific stuff as a separate piece of work :) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this for now - We'll work on Jinja-specific stuff as a separate piece of work :)