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.58k stars 640 forks source link

Is there a way to render a template with only specific nodes? #1294

Closed elantion closed 4 years ago

elantion commented 4 years ago

I want to combine nunjunks with html-webpack-plugin. I don't want to parse all nodes but only 'extends', 'block', 'include'. Is there a way to achieve this? Thanks, good day.

ogonkov commented 4 years ago

Can you give an example of what you trying to achieve?

elantion commented 4 years ago

For example, I want to convert below template to html and I don't want to deal with variables.

<!doctype html>
<html lang="cn">
    <head>
    <title>{{ title }}</title>
    </head>
    <body>
    {% block body %}{% endblock %}
    </body>
</html>

expect

<!doctype html>
<html lang="cn">
    <head>
    <title>{{ title }}</title>
    </head>
    <body>
    <div id="app"></div>
    </body>
</html>
ogonkov commented 4 years ago

Looks like you want to wrap some parts of your template in {% raw %}...{% raw %}, to avoid rendering of this part. It's the only way to render only specific nodes.

<!doctype html>
<html lang="cn">
    <head>
    <title>{% raw %}{{ title }}{% endraw %}</title>
    </head>
    <body>
    {% block body %}{% endblock %}
    </body>
</html>

What problem did you trying to solve? May be there a easier way.

elantion commented 4 years ago

It seem like it works for me. but what I expecting was some kind of configuration to achieve this. Thanks all the same. Good day, mate.