Skip to main content

Jinja2 templates integration in Grok

Project description

Introduction

megrok.jinja makes it possible to use Jinja2 templates in Grok.

For more information about Grok and Jinja2 visit:

Requirements

  • Grok v1.0a or later. Tested with Grok v1.0a2.

  • Jinja v2.1.1 or later. Tested with Jinja v2.1.1

  • PyYAML v3.08 or later. Tested with PyYAML v3.08

  • simplejson v1.7.1 or later. Tested with simplejson v1.7.1

Installation

To use the Jinja2 templates within Grok, megrok.jinja must be first installed as an egg, and its ZCML included. After using grokproject, amend the setup.py to look like this:

install_requires=[''setuptools',
                  'grok',
                  'megrok.jinja',
                  # Add extra requirements here
                  ],

Then include megrok.jinja in your configure.zcml. It should look something like this:

<configure xmlns="http://namespaces.zope.org/zope"
           xmlns:grok="http://namespaces.zope.org/grok">
  <include package="grok" />
  <include package="megrok.jinja"/>
  <grok:grok package="." />
</configure>

Rerun buildout (bin/buildout), giving you something like:

Getting distribution for ‘megrok.jinja’. Got megrok.jinja 0.1

That’s it. You can now place Jinja2 templates (with the .jinja extension) into any template directory used within your project.

Jinja2 Environment

megrok.jinja creates an Environment using jinja2.ext.i18n extension and overrides the globals variables _ and gettext in order to use custom functions to resolve translations. It also set the global variable provider as a function to resolve the call to a content provider (viewlet manager).

For more information about Jinja2 Environment and Global variables visit:

With the extensions added you are able to use
  • zope.i18n messages factory

  • content providers like viewlets

To translate your templates, just register your translations domain and then you can write:

{% set i18n_domain='test_domain' %}

{{ _('some-msgid')}}

Also it’s possible to use the {%trans%} tag:

{% set i18n_domain='test_domain' %}

{% trans %}
Whatever you may want to translate.
{% endtrans %}

It’s important to note that, messages created in python classes won’t be translated like in Zope Page Templates.

If you write:

view.py:

from zope import i18nmessageid
_ = i18nmessageid.MessageFactory('some.domain')

class Something(grok.View):
    def update(self):
        self.msg = _('Some msg id')

view.jinja:

{{ view.msg }}

You will always get ‘Some msg id’. What you do could write is:

view.py:

class Something(grok.View):
    def update(self):
        self.msg = 'Some msg id'

view.jinja:

{% set i18n_domain='some.domain' %}

{{ _(view.msg) }}

Note that megrok.jinja uses the translate function from zope.i18n.interfaces.ITranslationDomains, so using:

{{ _('msg_id') }}

you can pass all the parameters accepted by translate:

{{ _('msg_id', domain='another_domain', target_language='es') }}

If you want to call some content provider named ‘manager’, just write:

{{ provider('manager') }}

About .json extension and PyYAML

For more information about PyYAML visit: - http://pyyaml.org/wiki/PyYAML

megrok.jinja allows you to write templates with .json extension and inside it, you are able to mix the Jinja2 syntax with PyYAML.

First, megrok.jinja will try to parse the template using Jinja2 and the result it’s passed to the PyYAML loader. If PyYAML it’s able to load the string passed, the result it’s returned with simplejson.dumps

If you write this in a template with .json extension:

dicts :
   - key1 : some_text
   - key2 : {{ 'Some Jinja2 expression' }}
     {% set l = ['3','4'] %}
     {% for v in l %}
   - {{ 'key-' + v }} : whatever {{ v }}
     {% endfor %}

You will get the next JSON:

{"dicts": [{"key1": "some_text"},
           {"key2": "Some Jinja2 expression"},
           {"key-3": "whatever 3"},
           {"key-4": "whatever 4"}]}

Authors

  • Santiago Videla

Changelog

0.1 (2009-05-03)

Initial public release.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

megrok.jinja-0.1.tar.gz (12.2 kB view details)

Uploaded Source

File details

Details for the file megrok.jinja-0.1.tar.gz.

File metadata

  • Download URL: megrok.jinja-0.1.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for megrok.jinja-0.1.tar.gz
Algorithm Hash digest
SHA256 668a44a95e4fb64ea3265054b2f518b1de6f76234f4277532efcd0feb20a2101
MD5 ebacb92c8a301b40976ad87dc9feff9e
BLAKE2b-256 a64a8ec13d048e2ea7fdf423dd59b43140866d3c4a3b4430e40fb3fb6347e4af

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page