twig.js icon indicating copy to clipboard operation
twig.js copied to clipboard

Namespaces with source() do not resolve

Open ryuran opened this issue 7 years ago • 7 comments

Hello

When I include a partial with include tag it's work really well

  {% include '../partials/_exemple.twig' %}

But when I use source tag with same path,

  {{ source('../partials/_exemple.twig') }}

It displays :

Template "../partials/_exemple.twig" is not defined.

It should display partial content without interpreting it.

ryuran avatar Jan 10 '17 10:01 ryuran

This is because of 4012 in twig.js. It's asking for:

if (isNodeEnvironment) {
    loader = 'fs';
    path = __dirname + '/' + name;
} else {
    loader = 'ajax';
    path = name;
}

Which then gives you: /Users/xyz/your_application/bin/node_modules/twig

It should be using the path shared by Node's Twig implementation.

mattkenefick avatar Jan 19 '17 20:01 mattkenefick

I'm encountering the same issue. Has anyone figured out a fix (maybe re-implementing the "source" tag as a custom function)?

chrisvanpatten avatar Jun 28 '17 12:06 chrisvanpatten

Here is the most simple implementation of the source function which worked for my use case. Eventually the dir will probably need to be adapted.

const dir = __dirname;
Twig.extendFunction('source', function(src) {
  return fs.readFileSync(dir + src, 'utf8');
});

idflood avatar Mar 14 '18 21:03 idflood

This may be fixed via https://github.com/twigjs/twig.js/pull/526 . Mind re-testing?

RobLoach avatar Jul 23 '18 18:07 RobLoach

Now source function works if you use a path relative to the root of the project, however the source function does not support namespaces as it does with Twig

Not sure if this issue should be open for this or if other issue should be created

HakS avatar Oct 30 '19 16:10 HakS

Real bummer here. Does it take much effort to implement that? I can also create a PR if you point me to the gap in the code that has to be filled.

renestalder avatar May 27 '21 13:05 renestalder

bump, any update on this one?

arismag avatar Apr 03 '22 20:04 arismag