mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.52k stars 637 forks source link

Output missing from templates #693

Open webOS101 opened 8 years ago

webOS101 commented 8 years ago

When building our enyo-docs documentation output, we're having issues where templates referenced in a for loop are not outputting (it appears to be skipping entries). Unfortunately, it's extremely difficult to isolate the issue. Our docs repo is located here:

https://github.com/enyojs/enyo-docs

and includes instructions on setup and building. One example, the documentation output for our moonstone/Drawers/Drawer kind has two sections that output the same data, one loops and outputs just the name of the methods and the other section outputs the details. The details section only shows four methods while the overview shows 19.

In one of my templates I have a {% for %} loop that iterates over an array and then outputs pieces of the objects within the array. Some of the objects in the array have a .description member, some don't. If I remove the section of the template that attempts to render the description property, everything renders OK. If I don't, it will skip rendering anything that doesn't have a description (not just skip printing description, but anything at all from that iteration of the for loop).

The whole set of templates is fairly extensive. This is the relevant, debug portion of the template:

    {% set count = 0 %}
    {% for method in methods %}
        {{ method.name }} - {{ method.description }}<br />
        {% if method.access == 'public' %}
            {% set count = count + 1 %}
        {% endif %}" +
    {% endfor %}

So, for my data, it will only output four items into the output. If I remove the {{ method.description}} part, it will output all 19 names.

I have tested this on several versions of nunjucks and all exhibit the same behavior.

I realize this is not exactly a high-quality bug report and I am happy to work with anyone who can lend a hand in isolating and resolving the issue.

Jermaine0Forbes commented 7 years ago

I think I am having a similar problem. There are some variables that are not outputting into the html. At first I thought it was the database that I was working with that was acting funny. But, when I switched databases I still had the same problem. It is so weird because there are some things that are being printed to html, but other variables are not. It is very strange

ArmorDarks commented 7 years ago

@webOS101 Maybe some value in methods defined as non-enumerable? In such case they will be skipped, same as in JavaScript.

Also, quite dumb way to debug things is to create global function like this:

env.addGlobal('log', function (...values) => {
  console.log(...values)
})

And then make something like:

    {% set count = 0 %}
    {% for method in methods %}
        {{ method.name }} - {{ log('description', method.description) }}<br />
        {% if method.access == 'public' %}
            {% set count = count + 1 %}
        {% endif %}" +
    {% endfor %}

Now watch what will print console. Does it output anything, or undefined, or actual values?

@Jermaine0Forbes This applies to your case too, though, I'm not sure that your issue related to this one, unless you have issue within loops too.