Skip to content

Commit

Permalink
add jinja2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
prokaktus committed Jun 30, 2017
1 parent 21e740f commit 830e9db
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Contributors
* Leonardo Cavallucci
* m-vdb
* Marco Federighi
* Maxim Filipenko
* Mirat Can Bayrak
* Murat Aydos
* mvergerdelbove
Expand Down
21 changes: 21 additions & 0 deletions docs/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ Usage
</body>
</html>

#. For Jinja2 support, configure your environment::


from jinja2 import Environment
from meta.jinja2 import MetaProxy

...
env = Environment(**options)
env.globals.update({
...
'meta_global': MetaProxy()
})


And put macro into your templates::

{% from 'meta/meta.html' import meta_macro with context %}
{{ meta_macro() }}

Jinja2 support is experimental for now, so feel free to tell about any issues.

Note
++++

Expand Down
27 changes: 27 additions & 0 deletions meta/jinja2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from functools import wraps

from django.core.exceptions import ImproperlyConfigured

from .templatetags import meta

try:
from jinja2 import Markup
except ImportError:
Markup = None


class MetaProxy(object):

def __init__(self, *args, **kwargs):
if Markup is None:
raise ImproperlyConfigured('Jinja2 package is not installed')

def __getattr__(self, item):
f = getattr(meta, item)

@wraps(f)
def wrapped(*args, **kwargs):
tag = f(*args, **kwargs)
return Markup(tag)

return wrapped
56 changes: 56 additions & 0 deletions meta/jinja2/meta/meta.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{% macro meta_macro() -%}
{% if meta is defined %}
{% if meta.description %}{{ meta_global.meta('description', meta.description) }}{% endif %}
{% if meta.keywords %}{{ meta_global.meta_list('keywords', meta.keywords) }}{% endif %}
{% if meta.extra_props %}{{ meta_global.meta_extras(meta.extra_props) }}{% endif %}
{% if meta.extra_custom_props %}{{ meta_global.custom_meta_extras(meta.extra_custom_props) }}{% endif %}
{% if meta.use_title_tag %}
{% if meta.title %}{{ meta_global.title_prop(meta.title) }}{% endif %}
{% endif %}
{% if meta.use_og %}
{% if meta.title %}{{ meta_global.og_prop('title', meta.title) }}{% endif %}
{% if meta.url %}{{ meta_global.og_prop('url', meta.url) }}{% endif %}
{% if meta.og_description %}{{ meta_global.og_prop('description', meta.og_description) }}
{% elif meta.description %}{{ meta_global.og_prop('description', meta.description) }}{% endif %}
{% if meta.image %}{{ meta_global.og_prop('image', meta.image) }}{% endif %}
{% if meta.og_type %}{{ meta_global.og_prop('type', meta.og_type) }}
{% elif meta.object_type %}{{ meta_global.og_prop('type', meta.object_type) }}{% endif %}
{% if meta.site_name %}{{ meta_global.og_prop('site_name', meta.site_name) }}{% endif %}
{% if meta.og_author_url %}{{ meta_global.generic_prop('article', 'author', meta.og_author_url) }}
{% elif meta.og_author %}{{ meta_global.generic_prop('article', 'author', meta.og_author) }}{% endif %}
{% if meta.published_time %}{{ meta_global.generic_prop('article', 'published_time', meta.published_time) }}{% endif %}
{% if meta.modified_time %}{{ meta_global.generic_prop('article', 'modified_time', meta.modified_time) }}{% endif %}
{% if meta.expiration_time %}{{ meta_global.generic_prop('article', 'expiration_time', meta.expiration_time) }}{% endif %}
{% if meta.og_publisher %}{{ meta_global.generic_prop('article', 'publisher', meta.og_publisher) }}{% endif %}
{% if meta.og_app_id %}{{ meta_global.facebook_prop('app_id', meta.og_app_id) }}
{% elif meta.facebook_app_id %}{{ meta_global.facebook_prop('app_id', meta.facebook_app_id) }}{% endif %}
{% if meta.fb_pages %}{{ meta_global.facebook_prop('pages', meta.fb_pages) }}{% endif %}
{% if meta.og_profile_id %}{{ meta_global.facebook_prop('profile_id', meta.og_profile_id) }}{% endif %}
{% if meta.tag %}{{ meta_global.generic_prop('article', 'tag', meta.tag) }}{% endif %}
{% if meta.locale %}{{ meta_global.og_prop('locale', meta.locale) }}{% endif %}
{% endif %}
{% if meta.use_twitter %}
{{ meta_global.twitter_prop('domain', meta.get_domain) }}
{% if meta.twitter_type %}{{ meta_global.twitter_prop('card', meta.twitter_type) }}
{% elif meta.twitter_card %}{{ meta_global.twitter_prop('card', meta.twitter_card) }}{% endif %}
{% if meta.title %}{{ meta_global.twitter_prop('title', meta.title) }}{% endif %}
{% if meta.url %}{{ meta_global.twitter_prop('url', meta.url) }}{% endif %}
{% if meta.twitter_description %}{{ meta_global.twitter_prop('description', meta.twitter_description) }}
{% elif meta.description %}{{ meta_global.twitter_prop('description', meta.description) }}{% endif %}
{% if meta.image %}{{ meta_global.twitter_prop('image', meta.image) }}{% endif %}
{% if meta.twitter_creator %}{{ meta_global.twitter_prop('creator', meta.twitter_creator) }}{% endif %}
{% if meta.twitter_site %}{{ meta_global.twitter_prop('site', meta.twitter_site) }}{% endif %}
{% endif %}
{% if meta.use_googleplus %}
{% if meta.gplus_author %}<link rel="author" href="https://plus.google.com/{{ meta.gplus_author }}"/>{% endif %}
{% if meta.gplus_publisher %}<link rel="publisher" href="https://plus.google.com/{{ meta.gplus_publisher }}"/>{% endif %}
{% if meta.title %}{{ meta_global.googleplus_prop('name', meta.title) }}{% endif %}
{% if meta.published_time %}{{ meta_global.googleplus_prop('datePublished', meta.published_time) }}{% endif %}
{% if meta.modified_time %}{{ meta_global.googleplus_prop('dateModified', meta.modified_time) }}{% endif %}
{% if meta.url %}{{ meta_global.googleplus_prop('url', meta.url) }}{% endif %}
{% if meta.gplus_description %}{{ meta_global.googleplus_prop('description', meta.gplus_description) }}
{% elif meta.description %}{{ meta_global.googleplus_prop('description', meta.description) }}{% endif %}
{% if meta.image %}{{ meta_global.googleplus_prop('image', meta.image) }}{% endif %}
{% endif %}
{% endif %}
{%- endmacro %}

0 comments on commit 830e9db

Please sign in to comment.