-
-
Save jmolivas/d29065493a91f16f35b2 to your computer and use it in GitHub Desktop.
| acme_hello: | |
| path: '/acme/hello/{name}' | |
| defaults: | |
| _content: '\Drupal\acme\Controller\DefaultController::hello' | |
| _title: 'acme Title' | |
| requirements: | |
| _permission: 'access content' |
| <?php | |
| namespace Drupal\acme\Controller; | |
| use Drupal\Core\Controller\ControllerBase; | |
| use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | |
| use Symfony\Component\DependencyInjection\ContainerInterface; | |
| use Drupal\Core\Template\TwigEnvironment; | |
| class DefaultController extends ControllerBase implements ContainerInjectionInterface | |
| { | |
| /** | |
| * @var Drupal\Core\Template\TwigEnvironment | |
| */ | |
| protected $twig; | |
| public function __construct(TwigEnvironment $twig) | |
| { | |
| $this->twig = $twig; | |
| } | |
| public static function create(ContainerInterface $container) | |
| { | |
| return new static( | |
| $container->get('twig') | |
| ); | |
| } | |
| /** | |
| * hello | |
| * @param string $name | |
| * @return string | |
| */ | |
| public function hello($name) | |
| { | |
| $template = $this->twig->loadTemplate( | |
| drupal_get_path('module', 'acme') . '/templates/hello.html.twig' | |
| ); | |
| $markup = [ | |
| '#markup' => $template->render([ 'name' => $name ]), | |
| '#attached' => [ 'css' => [ | |
| drupal_get_path('module', 'acme') . '/assets/css/acme.css' | |
| ] | |
| ] | |
| ]; | |
| return drupal_render($markup); | |
| } | |
| } |
| <h1>Hello {{ name }}!</h1> |
Hello, where is the location of hello.html.twig please, I need to create a directory with a specific name for this file ? Thanks.
@AxelBriche: Twig templates must be created at templates directory within the module directory.
@UnnikrishnanBhargavakurup: Attaching css is no longer valid in order to add js/css you need to create a library more information at:
Hi, can't we directly render a something from the controller using a twig file rather than sending it to .module file?
I thought it might help people to get the full folder structure so I put together an example module . I also fixed the "not allowed to use css in #attached" by creating a library. I hope this helps a couple people that may have been confused on where the files are supposed to go.
great!!
@ericski thanks man!!
webform.twig.html file in custom module drupal 8 not working.
Thanks in advance
Hey Sr. Olivas! You got me out of a bind. Thank you for this (y por la Consola)!
glad this is still useful @ahernandezsouza
Hi,
I am getting an error Uncaught PHP Exception LogicException: "You are not allowed to use css in #attached" at common.inc line 731