Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Base option properly with Extends, Block, Include and Import tags? #54

Open
franklang opened this issue Jun 4, 2018 · 1 comment

Comments

@franklang
Copy link

franklang commented Jun 4, 2018

Please note: GitHub sources to reproduce this issue are available here.

Make sure to start form the "test-twig" branch.

Hi,

I'm trying to use Twig Extends tag in my Twig templates, but get Gulp log errors.

Here's how my files are organised within my project:

templates
|- layouts
|  |- default.twig
|- pages
|  |- index.twig
|- partials
   |- forms.twig (macro)
   |- partial.twig

Here's my template (templates/pages/index.twig):

{# index.twig #}
{% extends "default.twig" %}

{% set pageTitle = "Use Twig from the start!" %}
{% set benefits = ["Fast!", "Easy!", "Efficient!"] %}

{% block page %}
  <header>
    <h1>{{ pageTitle }}</h1>
  </header>
  <p>
    This page is generated by Twig.js using the gulp-twig gulp plugin.
  </p>
  <ul>
    {% for value in benefits %}
      <li>{{ value }}</li>
    {% endfor %}
  </ul>

  {% include "partial.twig" %}

  {% import "forms.twig" as forms %}
  <p>{{ forms.input('username') }}</p>
  <p>{{ forms.input('password', null, 'password') }}</p>
{% endblock %}

Here's my gulpfile.js:

// Compile Twig templates to HTML
function twig() {
  return gulp.src('src/templates/pages/*.twig')
    .pipe($.twig({base: 'src/templates/**', errorLogToConsole: true}))
    .pipe(gulp.dest(PATHS.dist))
    .pipe(browser.reload({ stream: true }));
}
// Watch for changes to static assets, pages, Sass, and JavaScript
function watch() {
  gulp.watch('src/templates/pages/*.twig').on('all', gulp.series(twig, browser.reload));
}

As you see, I'm trying to use the Base option which allows you to sets the views base folder.
That's because I want to avoid giving relative paths to the Extends tag within my Twig templates.

But here's the error I got in the logs:

Error parsing twig template /home/intlangf/sandbox/foundation-zurb-template/src/templates/pages/index.twig:
TwigException: Unable to find template file src/templates/**/default.twig
[10:11:15] gulp-twig TwigException: Unable to find template file src/templates/**/default.twig

If I don't use the Base option in the gulpfile, but give a relative path to the Extends tag, it works like a charm (templates/pages/index.twig):

{# index.twig #}
{% extends "../layouts/default.twig" %}

So my question is: am I doing something wrong when setting the Base option? My first guess is that it doesn't allow globbing. I've tried several solutions so far but with no success...

Subsidiary question:
Documentation says "Extends can be loaded relative to this path". But would that also work when using Block, Include or Import tags?

Thanks!

@jameelmoses
Copy link

@franklang your base should just be the directory without the globs. In your case: src/templates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants