1
Fork 0

Fix common instance variable

This commit is contained in:
Jake Howard 2017-01-13 17:35:32 +00:00
parent 4a32c62598
commit 1b7f3070fb
6 changed files with 61 additions and 5 deletions

View file

@ -101,5 +101,8 @@ JINJA_FILTERS = {
JINJA_ENVIRONMENT = {
'trim_blocks': True,
'lstrip_blocks': True,
'extensions': {}
'extensions': [
'jinja2.ext.with_',
'plugins.include_with.IncludeWith'
]
}

53
plugins/include_with.py Normal file
View file

@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
"""
# Jinja-IncludeWith
A Jinja2 preprocessor extension that let you update the `include`
context like this:
{% include "something.html" with foo=bar %}
{% include "something.html" with a=3, b=2+2, c='yes' %}
You **must** also include 'jinja2.ext.with_' in the extensions list.
:copyright: [Juan-Pablo Scaletti] (http://jpscaletti.com).
:license: [MIT] (http://www.opensource.org/licenses/mit-license.php).
Copied from https://github.com/jpscaletti/jinja-includewith due to not pip-installable
"""
import re
from jinja2.ext import Extension
__version__ = '0.1'
rx = re.compile(r'\{\%\s*include\s+(?P<tmpl>[^\s]+)\s+with\s+(?P<context>.*?)\s*\%\}',
re.IGNORECASE)
class IncludeWith(Extension):
def preprocess(self, source, name, filename=None):
lastpos = 0
while 1:
m = rx.search(source, lastpos)
if not m:
break
lastpos = m.end()
d = m.groupdict()
context = d['context'].strip()
if context == 'context':
continue
source = ''.join([
source[:m.start()],
'{% with ', context, ' %}',
'{% include ', d['tmpl'].strip(), ' %}',
'{% endwith %}',
source[m.end():]
])
return source

View file

@ -9,7 +9,7 @@
{% endblock %}
{% block content %}
{% include 'extras/header.html' with context %}
{% include 'extras/header.html' with instance=article %}
<section>
<div class="container">
{{ article.content }}

View file

@ -9,7 +9,7 @@
{% endblock %}
{% block content %}
{% include 'extras/header.html' with context %}
{% include 'extras/header.html' with instance=article %}
<section>
<div class="container">
<p class="text-right small">

View file

@ -10,7 +10,7 @@
{% block content %}
{% if not page.no_container %}
{% include 'extras/header.html' with context %}
{% include 'extras/header.html' with instance=page %}
<section>
<div class="container">
{{ page.content }}

View file

@ -9,7 +9,7 @@
{% endblock %}
{% block content %}
{% include 'extras/header.html' with context %}
{% include 'extras/header.html' with instance=article %}
<section>
<div class="container">
<p class="text-right small">