EzDevInfo.com

pystache

Mustache in Python {{ mustache }}

pystache - return all templates in a string

In pystache (or most templating libraries), you can do a substitution such as this:

>>> print pystache.render('{{person}} in {{place}}', {'person': 'Mom', 'place': 'Spain'})
Mom in Spain

Is it possible to do the 'opposite'? i.e. return a dictionary (or more correctly, a set) with all the templates in a string?

>>> templates = pystache.get_templates('{{person}} in {{place}}')
>>> print templates
{'person', 'place'}

Thanks.


Source: (StackOverflow)

mustache loop over dict of items without a name (by index)

heres my dict:

{   
1: {   'A': u'Eggs',
       'B': 1400,
       'C': u'Jibber',
       'D': u'355'},
2: {   'A': u'Avocados',
       'B': 1000,
       'C': u'Jabber',
       'D': u'356'},
..}

Template.mustache

{{#each}}
   <li><strong>{{A}}</strong></li>
   <li><strong>{{B}}</strong></li>
...
{{/each}}


{{#empty}}
 <p>The list is empty.</p>
{{/empty}}

If the variable is called data, how do I iterate over its items if they are only identifiable by index?


Source: (StackOverflow)

Advertisements

mustache/pystache: Rendering complex objects

I'm trying to wrap my head around rending complex objects using Mustache. I'm actually in Python using pystache, but the docs say it's compatible with the JS edition of Mustache.

In mustache, all works nice if information were a simple string: {{information}}

Mustache renders the value of information as XYZPDQ for example.

If information were a complex object however, it doesn't work: {{information.property1}} - {{information.property2}} shows nothing.

I'm expecting to see something like this: I am property 1 - XYZPDQ

There's also partials. But that seems like a crazy amount of overkill. In that situation, I suppose there would be a setup like this:

layout.html

<div>
    {{> information}}
</div>

information.mustache

{{property1}} - {{property2}}

Now, I will have an enormous number of .mustache partials around for every property. That can't be right.

UPDATE: Here's a variation of @trvrm's answer below using objects, which shows the problem. It works with dictionaries, but not complex types. How do we do this with complex types?

import pystache

template = u'''
  {{greeting}}
   Property 1 is {{information.property1}}
   Property 2 is {{information.property2}}
'''

class Struct:
  pass

root = Struct()
child = Struct()
setattr(child, "property1", "Bob")
setattr(child, "property2", 42)
setattr(root, "information", child)
setattr(root, "greeting", "Hello")

context = root

print pystache.render(template, context)

yields:

   Property 1 is 
   Property 2 is 

If you change the last two lines to this:

context = root.__dict__

print pystache.render(template, context)

Then you get this:

  Hello
   Property 1 is 
   Property 2 is 

That example, plus trvrm's answer below, shows that pystache seems to prefer dictionaries and has trouble with complex types.


Source: (StackOverflow)

Where is documentation for pystache {{.}} ? (dot / period / implicit operator tag)

I am looking at some pystache template code that has this:

{{#image_size}}width="{{.}}"{{/image_size}}

It looks like {{.}} gets replaced by the value of image_size. Where is the pystache / mustache documentation that explains this? I looked through the documentation, but the only thing I found was a pystache example with {{.}} but without explanation:

>>> parsed = pystache.parse(u"Hey {{#who}}{{.}}!{{/who}}")

pystache repository and documentation
mustache documentation


Source: (StackOverflow)

Python dump json with accents [duplicate]

This question already has an answer here:

How can i print a json with special characters as "à" or "ç"?

I can print like this:

import json

weird_dict ={"person": "ç", "á": 'à', "ç": 'ã'}
print json.dumps(weird_dict, indent=4, sort_keys=True)

output:

{
    "person": "\u00e7", 
    "\u00e1": "\u00e0", 
    "\u00e7": "\u00e3"
}

if i use 'ensure_ascii=False'

weird_dict={"person": "ç", "á": 'à', "ç": 'ã'}
print json.dumps(weird_dict, indent=4, sort_keys=True, ensure_ascii=False)
output:
{
    "person": "ç", 
    "á": "à", 
    "ç": "ã"
}

How to overcome special characters issue using json? I need to render when i use pystache and try to print pystache.render('Hi {{person}}!', weird_dict) it occurs me:

"'ascii' codec can't decode byte 0xc3 in position 4770: ordinal not in range(128)"

Source: (StackOverflow)

pystache renderer.render with special characters

i'm getting a problem with pystache using special characters.. :(

How can i render the follow example

example = renderer.render("person:{{name}}", {"name":"João"})

Anyone have any idea how to lead with special characters using pystache? when i try to print this 'example' it occurs me a UnicodeDecodeError:

UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 7: invalid continuation byte

Any suggestion?


Source: (StackOverflow)

Iterating over keys and values of a dictionary in mustache / pystache

Suppose I have simple dictionary like this:

d = {'k1':'v1', 'key2':'val2'}

How can I render key, value lines in pystache using that dictionary?


Source: (StackOverflow)