0

I'm using Drupal 8.7.8

I would like to create a new twig theme file for all pages that are after a certain URL. For example: example.com/example/this-page

"This-page" is what I want to create my twig file for. But there doesn't seem to be an option in the theme debugging for anything after example.com/example.

Another example...The twig file would be named page--example.html.twig ... Is there no way to say page--example/*.html.twig ?

0

You can use THEME_NAME_theme_suggestions_page_alter to create your own template suggestions!

In your theme:

File: my_theme/my_theme.theme

/**
 * Implements hook_theme_suggestions_HOOK_alter.
 */
function my_theme_suggestions_page_alter(&$suggestions, $variables) {
  $current_path = \Drupal::service('path.current')->getPath();
  $current_path = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);

  if (strpos($current_path, 'this-page') !== false) {
    $suggestions[] = 'page__this_page';
  }
}

File: my_theme/templates/page--this-page.html.twig

// Output html and stuff

Obviously replace "my_theme" with the name of your custom theme.

Here is a nice tutorial that expands more on it.

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.