1

I am trying to render a template with my controller but does not work it show me this error :

LogicException: The controller must return a 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));
}
2
  • You need to return a response object rather than a string which the template is generating. return new Response($template->render(array('name' => $name))); should do it for you although I can't be sure as I've only used the templating component as part of the Symfony framework. Commented Jun 13, 2015 at 10:38
  • @Qoop it works thnks Commented Jun 13, 2015 at 10:50

3 Answers 3

3

In Drupal 8 you either return a Response object or a render array from a controller. So you have two options:

1) Place the rendered template into a 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 the rendered template into a 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,
  );
}
Sign up to request clarification or add additional context in comments.

1 Comment

note there's also AjaxResponse() for AJAX requests - it extends Symfony's JsonResponse class and runs the output through json_encode() first.
0

Also you can Use the second option without custom template, doing this:

public function helloAction($name) {
  $markup = "<p> Without custom Template</p>";
  return array(
    '#markup' => $markup,
  );
}

Comments

0
class ClientController extends ControllerBase implements ContainerInjectionInterface ,ContainerAwareInterface {

protected $twig ;

public function __construct(\Twig_Environment $twig)
{
    $this->twig = $twig ;
}


public function index()
{

    $twigFilePath = drupal_get_path('module', 'client') . '/templates/index.html.twig';
    $template = $this->twig->loadTemplate($twigFilePath);
    $user = ['user' => 'name'] ; // as example
    $markup = [
        '#markup' => $template->render( ['users' => $users ,'kit_form' => $output] ),
        '#attached' => ['library' => ['client/index.custom']] ,
    ];
    return $markup;

}

// this is called first then call constructor 
public static function create(ContainerInterface $container)
{
    return new static(
        $container->get('twig') ,
    );
}
}

this full example to render twig by dependency injection from controller

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.