Skip to content

Instantly share code, notes, and snippets.

@xwero
Last active December 21, 2015 22:58
Show Gist options
  • Save xwero/6378648 to your computer and use it in GitHub Desktop.
Save xwero/6378648 to your computer and use it in GitHub Desktop.
Load twig templates from the lib directory of your module
<?php
namespace Drupal\module_name\Controller;
use Drupal\Core\Controller\ControllerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Template\TwigEnvironment;
class FooController implements ControllerInterface {
public function __construct(TwigEnvironment $twig, $twig_loader) {
$twig_loader->addpath(str_replace('Controller','View',__DIR__));
$this->twig = $twig;
}
public static function create(ContainerInterface $container) {
return new static(
$container->get('twig'),
$container->get('twig.loader')
);
}
public function displayTwig(){
return $this->twig->render('displayTwig.html.twig');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment