php - drupal 8 error to render template twig with controller -
i trying render template controller not work show me error :
logicexception: controller must return response (
hello bob!
given). in symfony\component\httpkernel\httpkernel->handleraw() (line 163 of core/vendor/symfony/http-kernel/symfony/component/httpkernel/httpkernel.php).
my function :
public function helloaction($name) { $twigfilepath = drupal_get_path('module', 'acme') . '/templates/hello.html.twig'; $template = $this->twig->loadtemplate($twigfilepath); return $template->render(array('name' => $name)); }
in drupal 8 either return response object or render array controller. have 2 options:
1) place rendered template response object:
public function helloaction($name) { $twigfilepath = drupal_get_path('module', 'acme') . '/templates/hello.html.twig'; $template = $this->twig->loadtemplate($twigfilepath); $markup = $template->render(array('name' => $name)); return new response($markup); }
2) place rendered template render array:
public function helloaction($name) { $twigfilepath = drupal_get_path('module', 'acme') . '/templates/hello.html.twig'; $template = $this->twig->loadtemplate($twigfilepath); $markup = $template->render(array('name' => $name)); return array( '#markup' => $markup, ); }
Comments
Post a Comment