Adding 'user/templates' path from themes

I had a question.

on my theme.php, i add this:

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.

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.

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.

@Jrbdog, I finally found it.

onGetPageTemplates and onGetPageBlueprints must also be included in getSubscribedEvents()

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

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

1 Like