userx
userx

Reputation: 876

Custom templating using Jinja2

I am trying to use jinja2 as follows.

Suppose,

Following are tags:

tags: {"world":"WORLD", "c language": "Dennis Ritchie", "apple":"JOBS" }

Input:

HELLO {{ world }}, C is written by **{{ c language }}**, **}}** while **{{** java is written by {{ java }}, hola.

Output:

HELLO WORLD, C is written by Dennis Ritchie, **}}** while **{{** java is written by, hola.

So in short there are following things I have to do.

  1. delimiters - {{ & }}
  2. If there is no tag predefined, it should put empty.
  3. If there is only single delimiter {{ or }} ( I mean not pair) ,it should not consider tag else it should be printed as it it.
  4. Tags should allow spaces.

Out of 4, for only 1 & 2 jinja2 is working fine.

from jinja2 import Template
t = Template(input_string)
t.render(context)

But for 3rd & 4th, it's not working.(or I am mistaking.)

I found only 1 template engine called "mustache" which supports above all 4 conditions. But I don't know how it works in case of performance.

As jinja2 is mature template engine, I think it's possible to customize default behaviour.

Can anybody know solution?

Thnx in advance.

My primary testing shows that Mustache(Pystache) is too faster than jinja2. If possible please give expert opinion.

http://mustache.github.io/

https://github.com/defunkt/pystache

Upvotes: 1

Views: 537

Answers (2)

userx
userx

Reputation: 876

Finally I continue with mustache. It's really awesome template engine.

http://mustache.github.io/

For mustache build for python

https://github.com/defunkt/pystache

Upvotes: 1

jaime
jaime

Reputation: 2334

I don't think this is possible. The documentation is quite clear on identifiers:

Jinja2 uses the regular Python 2.x naming rules. Valid identifiers have to match [a-zA-Z_][a-zA-Z0-9_]*. As a matter of fact non ASCII characters are currently not allowed. This limitation will probably go away as soon as unicode identifiers are fully specified for Python 3.

Upvotes: 0

Related Questions