2

I want to create custom template for particular pages. Single template for multiple page in drupal 8.

1

Twig Debugging

It’s great that we have the ability to override our template files, but we are still left with the question of how we are to know which template file needs to be overridden in any particular case. After all, template files are nested one inside the other—which one contains the markup that we want to change?

This brings us to the killer feature of Drupal 8 theming—Twig debugging. Getting started is very easy. Inside of the sites/default folder is a file named service.yml. Inside that file you will find a setting for Twig debug. This setting will be set to false by default. Simply change it to true as shown in the image below:

enter image description here

As usual, clear your cache after making this change. When we return to our page and inspect the markup we will find debugging information added in the form of code comments. Here’s an example of what our home page markup will look like when inspected in DevTools:

enter image description here

You can see the debug info provides the path to the new template file we have created. This information is repeated for other template files found on the page, providing a fast and easy way of finding the file you need to override or edit.

Important: Do not edit template files in core! Copy the file to your theme and edit that version instead.

Also included are file name suggestions for overriding the currently active templates. If you’ve ever had to track down the correct template file to override in Drupal 7, then you know what a huge improvement this is.

Author: John Hannah

Source and more great twig info: Drupal 8 Theming Fundamentals, Part 2

Note: In the 2nd pic, the x shows you the current twig template that is being used. In this example it's page--front.html.twig

To answer your Q, enable twig debug and go to your page and with your browser inspect element. The debug will suggest you all the template names you can use for that page.

PS: Don't forget to turn off twig debug before your site goes live.

  • I want use one template for multiple page. – Sourabh Surana Apr 18 '16 at 8:10
  • or create your own suggestion in template_preprocess_page(&$variables), see here drupal.stackexchange.com/questions/98002/… – rémy Apr 18 '16 at 8:35
  • @SourabhSurana then look at the suggestion that appears in all pages (test 2 or 3 pages) in twig debug and use that twig name. I haven't done this myself yet, but I assume when you look at a page that is not the front page, there should be some suggestion that uses the content type name. You should go with that one, as you probably don't want to affect the front page. – No Sssweat Apr 18 '16 at 8:38
  • It is not working after enable debug it show node with not id in console all page have different id can you tell me how can i write condition using node id or slug for call one template on more than one page. – Sourabh Surana Apr 18 '16 at 9:37
  • I want use one template for multiple page like as wordpress – Sourabh Surana Apr 18 '16 at 10:09

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.