Nunjucks: include tag doesn't preserve indentation level on included text

Created on 22 Apr 2016  路  10Comments  路  Source: mozilla/nunjucks

Given the input

line one
  {% include "test.txt" %}
  another indented line

and test.txt contents:

included line one
  included line two

I would expect everything included to have the same relative indentation, like so:

line one
  included line one
    included line two
  another indented line

but instead, only the first line is indented, and the rest are reset to 0 relative indent (so only the indentation amount present in the included file is left):

line one
  included line one
  included line two
  another indented line

This ends up causing a couple nasty interactions for me, such as using a markdown region (with the nunjucks-markdown tag) with a fenced code block鈥攖he stripped indentation doesn't display correctly inside the resultant <pre><code> bits. I think this will generally be an issue when trying to include code inside a

 with predictable indentation.

I created a failing test case which is in .

Thanks!

enhancement

Most helpful comment

Can we please re-open this issue? It is directly related to https://github.com/zephraph/nunjucks-markdown/pull/23#issuecomment-262103635. The rendering of variables doesn't get indented on the line breaks either. See my comment in the previous link for an example.

All 10 comments

Hmm. This is expected behavior (by me), but I can see how it could be unexpected in some contexts, especially if using includes with indentation-sensitive formats like Markdown.

I think I am slightly opposed to changing it, however, because a) the current behavior matches the behavior of Jinja2, and Jinja2 compatibility is generally high priority for me, and b) the current behavior is very simple: the results of rendering the included template are literally included as-is in the including template in place of the include tag, with no munging and no special treatment of newlines. Your proposed behavior would involve always treating newlines specially in an included template, and inserting new spaces after every newline. This might solve some use cases, but could easily break others (imagine an included template with some pre-formatted text in it).

If we did this, it would not be a change to the default behavior of include, but an optional argument to include (which would be false by default, maintaining the current behavior by default). I guess I would probably accept a PR for an implementation that takes that approach.

Gotcha, thanks! Do you have any pointers on where I'd look to try to implement this? (If you'd prefer I take that discussion to the mailing list or a chat somewhere I'm happy to).

You'd probably want to start with compileInclude in src/compiler.js, though to add the optional argument you'll also need to modify parseInclude in src/parser.js, and probably modify the Include node type in nodes.js as well.

Hi,

I have similar problem. Below is example:

c.tpl

<ul class="list">
    <li class="item">
        <div class="feature">

        </div>
    </li>
</ul>

index.tpl

<div class="main">
    {% include "c.tpl" %}
</div>

result:

                <div class="main">
                    <ul class="list">
    <li class="item">
        <div class="feature">

        </div>
    </li>
</ul>
                </div>

should be:

                <div class="main">
                    <ul class="list">
                        <li class="item">
                            <div class="feature">

                            </div>
                        </li>
                    </ul>
                </div>

Do you know how I can do this? or maybe you working with additional parametr for this?
If you add it would be great.

Thanks,

@nicolasartman, @carljm how does Jinja2 handle nesting of included files?

@kriss145 does it need to be Nunjucks which does the "prettifying" of your HTML or could you use another module after Nunjucks render, like an HTML beautifier to achieve this?

In general I'd say it's a good thing each module just does one thing well. Nunjucks renders templates. Other modules can beautify. However I understand that for some filetypes (like Yaml), correct indentation is critical.

As I mentioned above, Jinja2 does the same thing nunjucks currently does -- no special handling of indentation for included files.

@carljm sorry, missed that. I fully agree with "Jinja2 compatibility is generally high priority" and extra configuration makes things more complicated than they should. So then I would say maybe Nunjucks isn't the right tool for @nicolasartman's use case. And just like @kriss145's use case another tool could do the job. So for those reasons I would close this issue as "won't fix".

Yeah, I kind of agree that on second look this should probably just be closed.

Sorry, one last thought. Nunjucks has support for the indent filter (Jinja2 docs). Maybe you could wrap it in a {% filter indent(...) %} to get the same outcome.

Can we please re-open this issue? It is directly related to https://github.com/zephraph/nunjucks-markdown/pull/23#issuecomment-262103635. The rendering of variables doesn't get indented on the line breaks either. See my comment in the previous link for an example.

Was this page helpful?
0 / 5 - 0 ratings