Skip to content

Commit 830e9db

Browse files
committed
add jinja2 support
1 parent 21e740f commit 830e9db

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Contributors
2727
* Leonardo Cavallucci
2828
* m-vdb
2929
* Marco Federighi
30+
* Maxim Filipenko
3031
* Mirat Can Bayrak
3132
* Murat Aydos
3233
* mvergerdelbove

docs/models.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ Usage
112112
</body>
113113
</html>
114114

115+
#. For Jinja2 support, configure your environment::
116+
117+
118+
from jinja2 import Environment
119+
from meta.jinja2 import MetaProxy
120+
121+
...
122+
env = Environment(**options)
123+
env.globals.update({
124+
...
125+
'meta_global': MetaProxy()
126+
})
127+
128+
129+
And put macro into your templates::
130+
131+
{% from 'meta/meta.html' import meta_macro with context %}
132+
{{ meta_macro() }}
133+
134+
Jinja2 support is experimental for now, so feel free to tell about any issues.
135+
115136
Note
116137
++++
117138

meta/jinja2.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from functools import wraps
2+
3+
from django.core.exceptions import ImproperlyConfigured
4+
5+
from .templatetags import meta
6+
7+
try:
8+
from jinja2 import Markup
9+
except ImportError:
10+
Markup = None
11+
12+
13+
class MetaProxy(object):
14+
15+
def __init__(self, *args, **kwargs):
16+
if Markup is None:
17+
raise ImproperlyConfigured('Jinja2 package is not installed')
18+
19+
def __getattr__(self, item):
20+
f = getattr(meta, item)
21+
22+
@wraps(f)
23+
def wrapped(*args, **kwargs):
24+
tag = f(*args, **kwargs)
25+
return Markup(tag)
26+
27+
return wrapped

meta/jinja2/meta/meta.html

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{% macro meta_macro() -%}
2+
{% if meta is defined %}
3+
{% if meta.description %}{{ meta_global.meta('description', meta.description) }}{% endif %}
4+
{% if meta.keywords %}{{ meta_global.meta_list('keywords', meta.keywords) }}{% endif %}
5+
{% if meta.extra_props %}{{ meta_global.meta_extras(meta.extra_props) }}{% endif %}
6+
{% if meta.extra_custom_props %}{{ meta_global.custom_meta_extras(meta.extra_custom_props) }}{% endif %}
7+
{% if meta.use_title_tag %}
8+
{% if meta.title %}{{ meta_global.title_prop(meta.title) }}{% endif %}
9+
{% endif %}
10+
{% if meta.use_og %}
11+
{% if meta.title %}{{ meta_global.og_prop('title', meta.title) }}{% endif %}
12+
{% if meta.url %}{{ meta_global.og_prop('url', meta.url) }}{% endif %}
13+
{% if meta.og_description %}{{ meta_global.og_prop('description', meta.og_description) }}
14+
{% elif meta.description %}{{ meta_global.og_prop('description', meta.description) }}{% endif %}
15+
{% if meta.image %}{{ meta_global.og_prop('image', meta.image) }}{% endif %}
16+
{% if meta.og_type %}{{ meta_global.og_prop('type', meta.og_type) }}
17+
{% elif meta.object_type %}{{ meta_global.og_prop('type', meta.object_type) }}{% endif %}
18+
{% if meta.site_name %}{{ meta_global.og_prop('site_name', meta.site_name) }}{% endif %}
19+
{% if meta.og_author_url %}{{ meta_global.generic_prop('article', 'author', meta.og_author_url) }}
20+
{% elif meta.og_author %}{{ meta_global.generic_prop('article', 'author', meta.og_author) }}{% endif %}
21+
{% if meta.published_time %}{{ meta_global.generic_prop('article', 'published_time', meta.published_time) }}{% endif %}
22+
{% if meta.modified_time %}{{ meta_global.generic_prop('article', 'modified_time', meta.modified_time) }}{% endif %}
23+
{% if meta.expiration_time %}{{ meta_global.generic_prop('article', 'expiration_time', meta.expiration_time) }}{% endif %}
24+
{% if meta.og_publisher %}{{ meta_global.generic_prop('article', 'publisher', meta.og_publisher) }}{% endif %}
25+
{% if meta.og_app_id %}{{ meta_global.facebook_prop('app_id', meta.og_app_id) }}
26+
{% elif meta.facebook_app_id %}{{ meta_global.facebook_prop('app_id', meta.facebook_app_id) }}{% endif %}
27+
{% if meta.fb_pages %}{{ meta_global.facebook_prop('pages', meta.fb_pages) }}{% endif %}
28+
{% if meta.og_profile_id %}{{ meta_global.facebook_prop('profile_id', meta.og_profile_id) }}{% endif %}
29+
{% if meta.tag %}{{ meta_global.generic_prop('article', 'tag', meta.tag) }}{% endif %}
30+
{% if meta.locale %}{{ meta_global.og_prop('locale', meta.locale) }}{% endif %}
31+
{% endif %}
32+
{% if meta.use_twitter %}
33+
{{ meta_global.twitter_prop('domain', meta.get_domain) }}
34+
{% if meta.twitter_type %}{{ meta_global.twitter_prop('card', meta.twitter_type) }}
35+
{% elif meta.twitter_card %}{{ meta_global.twitter_prop('card', meta.twitter_card) }}{% endif %}
36+
{% if meta.title %}{{ meta_global.twitter_prop('title', meta.title) }}{% endif %}
37+
{% if meta.url %}{{ meta_global.twitter_prop('url', meta.url) }}{% endif %}
38+
{% if meta.twitter_description %}{{ meta_global.twitter_prop('description', meta.twitter_description) }}
39+
{% elif meta.description %}{{ meta_global.twitter_prop('description', meta.description) }}{% endif %}
40+
{% if meta.image %}{{ meta_global.twitter_prop('image', meta.image) }}{% endif %}
41+
{% if meta.twitter_creator %}{{ meta_global.twitter_prop('creator', meta.twitter_creator) }}{% endif %}
42+
{% if meta.twitter_site %}{{ meta_global.twitter_prop('site', meta.twitter_site) }}{% endif %}
43+
{% endif %}
44+
{% if meta.use_googleplus %}
45+
{% if meta.gplus_author %}<link rel="author" href="https://plus.google.com/{{ meta.gplus_author }}"/>{% endif %}
46+
{% if meta.gplus_publisher %}<link rel="publisher" href="https://plus.google.com/{{ meta.gplus_publisher }}"/>{% endif %}
47+
{% if meta.title %}{{ meta_global.googleplus_prop('name', meta.title) }}{% endif %}
48+
{% if meta.published_time %}{{ meta_global.googleplus_prop('datePublished', meta.published_time) }}{% endif %}
49+
{% if meta.modified_time %}{{ meta_global.googleplus_prop('dateModified', meta.modified_time) }}{% endif %}
50+
{% if meta.url %}{{ meta_global.googleplus_prop('url', meta.url) }}{% endif %}
51+
{% if meta.gplus_description %}{{ meta_global.googleplus_prop('description', meta.gplus_description) }}
52+
{% elif meta.description %}{{ meta_global.googleplus_prop('description', meta.description) }}{% endif %}
53+
{% if meta.image %}{{ meta_global.googleplus_prop('image', meta.image) }}{% endif %}
54+
{% endif %}
55+
{% endif %}
56+
{%- endmacro %}

0 commit comments

Comments
 (0)