4

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.

101010
  • 14,866
  • 30
  • 95
  • 172
  • I think you've found a bug in pystache. As far as I can tell from the code, this *should* work, but if you look at the source in [context.py](https://github.com/defunkt/pystache/blob/master/pystache/context.py), I think there's a logic error. The code from line 53 onwards *should* be executed on the Struct you passed in, but doesn't. – trvrm Sep 14 '14 at 19:51
  • 1
    Thanks for helping me with this. I opened an issue: https://github.com/defunkt/pystache/issues/167 – 101010 Sep 15 '14 at 00:35
  • Added some notes to the github issue: it seems the code *does* work if you replace `class Struct()` with `class Struct(object)`. I'm not sure how to fix the test in `context.py` – trvrm Sep 15 '14 at 14:01

1 Answers1

8

Pystache has no problem rendering nested objects.

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

context={
    'greeting':'Hello',
    'information':{
         'property1':'bob',
         'property2':42
    }
}
print pystache.render(template,context)

yields:

Hello
  Property 1 is bob
  Property 2 is 42

Update

The example above can be made to work if we replace

class Struct():
    pass

with

class Struct(object):
    pass
trvrm
  • 784
  • 5
  • 17