vitkarpov / grunt-nunjucks-2-html

Compiles nunjucks templates *right* into HTML
MIT License
35 stars 11 forks source link

Leaking variables #9

Closed ArmorDarks closed 9 years ago

ArmorDarks commented 9 years ago

Hi

First of all, thanks for such handy task! It helped us with our projects a lot of time :)

However, there seems to be an bug with data. Though, I can't say for sure is it related to task or nunjucks itself.

Just let's take for example:

module.exports = ->
  @config 'nunjucks',
    build:
      options:
        paths: '<%= path.source.layouts %>/'
        data: '<%= data %>'
      files: [
        expand: true
        cwd: '<%= path.source.layouts %>/'
        src: ['{,**/}*.nj', '{,**/}*.html']
        dest: '<%= path.build.root %>/'
        ext: '.html'
      ]

We have following structure:

Layouts
|_ _layout.nj
|_ index.nj  // extends `_layout.nj`
|_ index2.nj // extends `_layout.nj`

and following content:

// _layout.nj

<!DOCTYPE html>
<html>

<head></head>

<body>

    <p>TestVar: {{ testVar }}</p>
    <p>TestVar2: {{ testVar2 }}</p>

    <p>MainBlock: {% block main %}{% endblock %}</p>

</body>

</html>
// index.nj

{% extends "_layout.nj" %}

{# --------- #}
{% set testVar = "Test var from <b>index</b>" %}
{% set testVar2 = "Test var from <b>index</b>" %}
{# --------- #}

{% block main %}Test block from <b>index</b>{% endblock %}
// index2.nj

{% extends "_layout.nj" %}

{# --------- #}
{% set testVar = "Test var from <b>index2</b>" %}
{# --------- #}

{% block main %}Test block from <b>index2</b>{% endblock %}

What we're expecting to see:

// index.html

TestVar: Test var from index

TestVar2: Test var from index

MainBlock: Test block from index
// index2.html

TestVar: Test var from index2

TestVar2: // since it's undefined

MainBlock: Test block from index2

What we will see:

// index.html

TestVar: Test var from index

TestVar2: Test var from index

MainBlock: Test block from index
// index2.html

TestVar: Test var from index2

TestVar2: Test var from index // notice that for some reason `index2.html` inherited variable from `index.html` while it shouldn't

MainBlock: Test block from index2

After testing I've noticed that it's definitely somehow related to options.data.

When it's passed to nunjucks, for some reason it start to inherit variables where it shouldn't. When there is no options.data — all works as expected.

vitkarpov commented 9 years ago

@ArmorDarks Maybe you could do a pr with such test?

It could be a such file structure:

tests
  leaking-vars
    base.html
    page1.html
    page2.html
    _output.html
 ...

and additional task to gruntfile.

I'll try to figure that out.

ArmorDarks commented 9 years ago

Will branch of Kotsu with prepared testing data be ok?

https://github.com/LotusTM/Kotsu/tree/feature/testing-leaking-data-n-inheritance

I've disabled most part of tasks, so you won't need to install external deps

vitkarpov commented 9 years ago

No, it's not a good idea. We need tests in the repo to avoid such issues in the future.

vitkarpov commented 9 years ago

Merged the test. Try to explore the problem now.

vitkarpov commented 9 years ago

keeping on work here https://github.com/vitkarpov/grunt-nunjucks-2-html/pull/17

vitkarpov commented 9 years ago

Solved