Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Ability to use Twig namespaces in templates {% include `@name/test.twig' %}

Started by Chris Clark 9 years ago · 1 replies · 2703 views
9 years ago

Does anyone have insight being able to use twig namespaces in templates?
https://twig.symfony.com/doc/2.x/api.html
https://symfony.com/doc/current/templating/namespaced_paths.html

I have been working on a plugin that hooks onTwigTemplatePaths and am able to addPath() to register a namespace. It also seem to return the paths in that namespace when i use getPaths(). But in grav I get:

TXT
Template "@atoms/test.twig" is not defined in "default2.html.twig" at line 1.

So it seems like grav isn't registering or checking for @ as a namespace indicator.

PHP
   public function onTwigTemplatePaths(Event $e)
{
  $config = $this->config->get('plugins.twig-namespaces');

  if (array_key_exists("namespaces", $config)) {
    $namespaceLoader = new Twig_Loader_Filesystem(array());

    foreach ($config["namespaces"] as $namespace => $item) {
      foreach ($item["paths"] as $path) {
        $namespaceLoader->addPath($path, $namespace);
      }
    }
  }

}

then i have a plugin config file twig-namespaces.yaml

YAML
enabled: true
namespaces:
  atoms:
    paths:
      - user/themes/pinterest-lab/templates/01-atoms
      - user/themes/pinterest-lab/templates/01-atoms/test

I'm not sure if I my $namespaceLoader needs to be registered with the changes.
This is on similar work here: https://github.com/EvanLovely/plugin-twig-namespaces/blob/master/src/PatternLab/TwigNamespaces/PatternLabListener.php#L31
but not finding how to do addLoader() and have it recognize the name space.

9 years ago

I've added namespaces support in latest develop branch of Grav. You need to use Twig::addPath($path, $namespace)

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 193 2 months ago
Themes & Styling · by Ian, 2 months ago
3 90 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 449 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 44 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 123 3 months ago