simon-dt / gulp-twig

Twig plugin for gulp.js, The streaming build system. Looking for maintainer or collaborators. See wiki
https://github.com/zimmen/gulp-twig/wiki/Looking-for-maintainer-or-collaborator(s)
MIT License
62 stars 33 forks source link

include TwigException #49

Open AnneCav opened 6 years ago

AnneCav commented 6 years ago

First of all, thank you for your plugin, I use it a lot :)

I've tryed to update from 0.5.0 to the latest version but when I run the task (which worked fine with 0.5.0) it returns a lot of twigexception like this one:

Error parsing twig template dev/templates/_layout/base.twig: 
TwigException: Unable to find template file dev/templates/_inc/styleguide/3-breadcrumb.twig

Task:

gulp.task('twig',"compile twig", function() {
  return gulp.src('./dev/pages/**/*.twig')
    .pipe(twig({
      base: './dev/templates/',
      data: {baseURL, config, configPackage, configScampi},
      extend: twigMarkdown
    }))
    .pipe(gulp.dest('./public/pages/'));
});

Nevertheless, templates are compiled.

Did I miss something?

AnneCav commented 6 years ago

So, after many tests, it appears that the problem is due to hyphens in the file name. Is it possible to fix this in the plugin?

jameelmoses commented 6 years ago

Experiencing the same issue with a nested include, where I'm including a partial in a partial. It compiles, but I get an exception that it's unable to find the template file. My file also has hyphens in the filename.

olets commented 6 years ago

Oops look like this ticket slipped through the cracks.

@jameelmoses for you is there no error if the file name doesn't have hyphens?

jameelmoses commented 6 years ago

I can check in the morning and report back

jameelmoses commented 6 years ago

So it looks like this issue for me is actually related to kss-node not being able to locate the nested include and not an issue with gulp-twig

paulgoodfield commented 5 years ago

@jameelmoses Did you manage to get nested includes working? When it tries to compile the following twig code I get the error TwigException: include function does not exist and is not defined in the context. I'm trying to pass an include as data to the card.twig component.

{% include 'components/card.twig' with {
    title: "My Title",
    actions: [
        include('components/button', {
            text: "Click Me"
        }, false)
    ]
} only %}
jameelmoses commented 4 years ago

yes, my issue was with kss-node and needing to use relative paths for nested includes. i've never attempted an implementation the way you're doing so can't really speak to it, but i will says when I am passing variable includes into a template, the best way is to use embed and have a block inside of it.

example:

{# components/card.twig #}
<div class="card">
  <div class="card-title">{{ title }}</div>
  {% block extra_stuff %}{% endblock %}
</div>

{# components/button.twig #}
<button>{{ text }}</button>

{# page.twig #}
{% embed 'components/card.twig' with { title: 'My Title' } %}
  {% block extra_stuff %}
    {% include 'components/button.twig' with { text: 'Click Me' } %}
  {% endblock %}
{% endembed  %}

alternatively, if you always just have an optional button, you should just be able to do this also:

{# components/card.twig #}
<div class="card">
  <div class="card-title">{{ title }}</div>
  {% if button_text %}
    {% include 'components/button.twig' with { text: button_text } %}
  {% endif %}
</div>

{# components/button.twig #}
<button>{{ text }}</button>

{# page.twig #}
{% include 'components/card.twig' with { title: 'My Title', button_text: 'Click Me' } %}