Twig template suggestions for paths such as page--node--page-url

Solutions

You can add suggestions for the page template in a hook:

mytheme.theme:

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function mytheme_theme_suggestions_page_alter(&$suggestions, $variables) {
  $path = \Drupal::requestStack()->getCurrentRequest()->getPathInfo();
  if ($path != '/') {
    $path = trim($path, '/');
    $arg = str_replace(["/", '-'], ['_', '_'], $path);
    $suggestions[] = 'page__' . $arg;
  }
}

This is an example how to add a suggestion with the path for any page. You can narrow it down to the pages you are interested in and if necessary check for paths that are too long.

Similar questions

Use page.html.twig template variable in another twig template
How do I call a page.html.twig template variable from the book-tree.html.twig template. Using kint, I have found the variable, nid as in the screenshot below: The 'nid' is highlighted, but it is the 'x-default value that is required. This variable will be used in a set statement, comparing it to the item.original_link.nid to set an active class. Th...
Template suggestions for page on specific node id
I created 2 nodes of the content type page for which i want to create a custom template file. In the theme_hook_suggestions array there is an element with 'node__821' so i created the file node--821.tpl.php but it doesn't work. I also tried page--node--821.tpl.php, this works in that it uses the file but this replaces the page.tpl.php and thats not...
Node view mode in page template suggestions
I would like page template suggestions when a node is using a custom view mode. I think this would happen in hook_theme_suggestions_page_alter(). I have already been able to get page-level template suggestions based on the node's content type, so it seems like I should be able to snag the name of the custom view mode? I'm not good at this part of D...
Define custom twig page template for specific multiple node pages by nid - page--custom.html.twig
I have a page from different content type and I want to define a custom twig page template to these pages by their nid. Eg. I have pages like node/2, node/4, node/20 and node/40 from the different content type and I want to apply page--custom.html.twig template only these specific pages by checking their ID. Anyone know how to do this?
How to have a content type that displays a title, a description and a list of feeds imported from an external URL such as blogspot?
I know this is already discussed, I read many answers, but I am unable to make this simple operation. I want a content type that displays a title a description and a list of feed items from an external URL that user can set when creates this content type node. Ok, I installed feeds module, created an importer. Before this I created a content type n...
"Twig_Error_Loader: Template "file.html.twig" is not defined" for new twig template file
After creating a new paragraph type and its template file in the correct folder location, I am receiving the following error every time I view the page with that houses the new paragraph type. Twig_Error_Loader: Template "file.html.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "file.html.twig" in the...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.