0

Lets say I have a hook_theme like this :

/**
 * Implements hook_theme().
 */
function mytheme_theme($existing, $type, $theme, $path) {
  return [
    'partial' => [
      'variables' => ['current_url' => NULL],
      'template' => 'partial'
    ]
  ];
}

Then in a node.html.twig, I want to call the template.

Currently I'm doing this :

{% include '@mytheme/partials/partial.html.twig' {'current_url': url('<current>')} %}

But the problem with this is that I never go into my

function mytheme_preprocess_partial(&$variables) {}

Also I can't benefit from the TWIG DEBUG mode.

Is there any way to do something like this :

{{ theme('partial', {'current_url': url('<current>')}) }}

Thanks for helping.

1

In D8 there is no theme() function, so if you want to render a template you use a render array for it. Put it in a twig variable and print it:

{% set partial = {
  '#theme': 'partial',
  '#current_url': url('<current>'),
} %}

{{ partial }}

As a side note, because of a security issue for Twig in Views you might be no longer be able to do this in Drupal 8.6 and are forced to use PHP in the near future. There is an ongoing discussion what to do about the code execution vulnerability without (hopefully) preventing such valid use cases, see https://www.drupal.org/project/drupal/issues/2860607

  • Thank you for that very clear and documented answer. I'll keep an eye on that topic :-) – pbonnefoi Feb 27 '18 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.