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.

Gantry

Adding 'user/templates' path from themes

Started by tacoen 6 years ago · 3 replies · 935 views
6 years ago

I had a question.

on my theme.php, i add this:

PHP
public function onTwigTemplatePaths()
{
        $locator = $this->grav['locator'];
        $tdir = $locator('user://').'/templates';
        if (! file_exists($tdir) ) { mkdir($tdir); }
    $this->grav['twig']->twig_paths[] = $locator('user://templates');
}

to make a new 'templates' path in 'user', and move some of template files (.html.twig) into that new path.

But then in 'admin', I don't see those files.

Questions:

  1. Are adding new templates path doesn't work in theme like in plugins? But, 'print_r( $this->grav['twig'])' sure show the additions.
  2. Are 'templates' in 'user' path is not correct/welcomed in grav twig path?

Thanks in advance.

6 years ago

I'm still new to Grav and Twig, but I'm guessing there's a file somewhere that tells Grav where to look for templates. It's probably not a good idea to edit that directly. I can think of two solutions:

  1. Create a fork of Grav and then edit the file.

  2. When you move the files to user/templates, create a new file in the default templates folder that extends the file from user/templates.

6 years ago

Yes, I always fork it for updating reason.

But yet, they always add a templates path by using or merge
$this->grav['twig']->twig_paths[] = $newpath;

Still had no idea, why it doesn't show up.

6 years ago

@Jrbdog, I finally found it.

onGetPageTemplates and onGetPageBlueprints must also be included in getSubscribedEvents()

PHP
public function onGetPageTemplates(Event $event)
{
    $types = $event->types;
    $types->scanTemplates('user://templates');
}

public function onGetPageBlueprints(Event $event)
{
    $types = $event->types;
    $types->scanBlueprints('user://blueprints');
}

Capture|690x367

👍 1
last edited 11/05/20 by tacoen

Suggested topics

Topic Participants Replies Views Activity
Gantry · by Chris, 11 months ago
1 176 11 months ago
Gantry · by Sam, 1 year ago
7 221 1 year ago
Gantry · by Dima, 2 years ago
2 75 2 years ago
Gantry · by KimTaeWoong, 9 years ago
2 1885 3 years ago
Gantry · by stani, 6 years ago
2 832 3 years ago