Last active
August 20, 2022 18:57
-
-
Save jmolivas/d29065493a91f16f35b2 to your computer and use it in GitHub Desktop.
Drupal 8 example: How to render a Twig template and load a CSS file from a Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.acme-hello-text { | |
background-color: #dedede; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function acme_theme() { | |
$theme['hello_page'] = [ | |
'variables' => ['name' => NULL], | |
'template' => 'hello' | |
]; | |
return $theme; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
acme_hello: | |
path: '/acme/hello/{name}' | |
defaults: | |
_content: '\Drupal\acme\Controller\DefaultController::hello' | |
_title: 'acme Title' | |
requirements: | |
_permission: 'access content' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\acme\Controller; | |
use Drupal\Core\Controller\ControllerBase; | |
class DefaultController extends ControllerBase | |
{ | |
/** | |
* hello | |
* @param string $name | |
* @return string | |
*/ | |
public function hello($name) | |
{ | |
return [ | |
'#theme' => 'hello_page', | |
'#name' => $name, | |
'#attached' => [ | |
'css' => [ | |
drupal_get_path('module', 'acme') . '/assets/css/acme.css' | |
] | |
] | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="acme-hello-text"> | |
<h1>Hello {{ name }}!</h1> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
glad this is still useful @ahernandezsouza