Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle nested context?
    text
    copied!<p>I wanted to use nested dictionaries in <a href="http://mustache.github.com" rel="nofollow">Mustache</a>, section <strong>Non-False Values</strong> in <a href="http://mustache.github.com/mustache.5.html" rel="nofollow">Mustache manual</a> suggests it's possible and presents the following example:</p> <p><strong>Template:</strong></p> <pre class="lang-js prettyprint-override"><code>{{#person?}} Hi {{name}}! {{/person?}} </code></pre> <p><strong>Hash:</strong></p> <pre class="lang-js prettyprint-override"><code>{ "person?": { "name": "Jon" } } </code></pre> <p><strong>Output:</strong></p> <pre><code>Hi Jon! </code></pre> <p>I tried to run above example in <a href="http://mustache.github.com/#demo" rel="nofollow">online demo</a> and I got:</p> <pre><code>Hi ! </code></pre> <p>I also tried <a href="https://github.com/defunkt/pystache" rel="nofollow">pystache</a> (pystache 0.3.1, Python 2.7.2):</p> <pre class="lang-python prettyprint-override"><code>import pystache tmpl = """ {{#person}} Hi {{name}}! {{/person}} """ dct = { "person": { "name": "Jon" } } print(pystache.render(tmpl, dct)) </code></pre> <p>And I got an error:</p> <pre><code>Traceback (most recent call last): File "test2.py", line 13, in &lt;module&gt; print(pystache.render(tmpl, dct)) File "c:\Python27\lib\site-packages\pystache\__init__.py", line 7, in render return Template(template, context).render() File "c:\Python27\lib\site-packages\pystache\template.py", line 42, in render template = self.render_sections(template, context) File "c:\Python27\lib\site-packages\pystache\template.py", line 78, in render_sections insides.append(self.render(inner, item)) File "c:\Python27\lib\site-packages\pystache\template.py", line 43, in render result = self.render_tags(template, context) File "c:\Python27\lib\site-packages\pystache\template.py", line 97, in render_tags replacement = func(self, tag_name, context) File "c:\Python27\lib\site-packages\pystache\template.py", line 105, in render_tag raw = context.get(tag_name, '') AttributeError: 'str' object has no attribute 'get' </code></pre> <p>I had no problems with lists, so structure like below worked fine:</p> <pre class="lang-js prettyprint-override"><code>{ "person?": [{ "name": "Jon" }] } </code></pre> <p>I can make a work-around by input dict preprocessing (flattening or changing dictionaries to lists), but why it doesn't work? Am I doing something wrong?</p> <hr> <h2>Solution of pystache problem</h2> <p>Version of pystache located in <a href="http://pypi.python.org" rel="nofollow">PyPI</a> is really old (from May 2010) and that's the problem. Version from <a href="https://github.com/defunkt/pystache" rel="nofollow">GitHub</a> is much newer (and problem with nested dictionaries doesn't show up).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload