6

I have created a content type called "Hotels". I want to display to the view in different manner other than view options provided by Views module. For that I have to create custom template but don't know how to do it. What should be the naming conventions and where to store the template file. Please help me.

closed as unclear what you're asking by kiamlaluno Jul 29 '16 at 9:13

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Welcome to Drupal Answers! Notice that you are just saying what you want to do without asking any explicit question. It's not clear what exactly you don't understand in what you need to do, what exactly you already did, or what exactly you know. We cannot copy-paste all the documentation available on the topic, hoping we tell you something you missed, or did wrong. – kiamlaluno Jul 29 '16 at 9:13
  • I am sorry, but if you don't know anything about creating a custom template for a view, you first need to read the available documentation. – kiamlaluno Jul 29 '16 at 9:28
  • To get started, and narrow down your question later on, have a look at the answers to some of these questions – Pierre.Vriens Jul 29 '16 at 9:47
6

To create custom template for that view, create file and give name to that template as views-view-fields--viewname--page/block.html.twig and then create the HTML structure and then simply print the field accordingly and then save the file and don't forget to flush the cache.

To print the field...suppose you want to print the title and image field then do as below {{ fields.title.content}} {{fields.machine_name_of_image_field.content}} thats it.

Note: To get the machine name of the field, goto the content type and then manage fields, there you will find machine name of those fields. And remember if you have not add field in view in admin panel and adding it in your template file then it will not reflect.

  • Notice that the first version of the question speaks of unformatted view. Does an unformatted view use fields? – kiamlaluno Jul 29 '16 at 9:34
  • Absolutely!! Without fields there will be nothing to display in the view. – Sachin Jul 29 '16 at 9:42
  • It doesn't seem so. i.stack.imgur.com/4RZwM.png – kiamlaluno Jul 29 '16 at 10:11
  • In the view you mentioned you are using show as "content", but there is another option too i.e "fields". Try that also.Once you select that then you must select fields which you want to display in fields.By default "Title" field is selected...I am new to stack overflow so I don;t know how to upload image otherwise I've screenshot of the unformatted view using fields which i created for one of my website. – Sachin Jul 29 '16 at 10:58
  • You said that without fields there would not be anything to show in the view. I proved it is not true, since the default front page view doesn't use any field. – kiamlaluno Jul 29 '16 at 11:02
6

Below give are the views template suggestion allowed in Drupal 8.

  • views-view--foobar--page.html.twig
  • views-view--page.html.twig
  • views-view--foobar.html.twig
  • views-view.html.twig
  • views-view-unformatted--foobar--page.html.twig
  • views-view-unformatted--page.html.twig
  • views-view-unformatted--foobar.html.twig
  • views-view-unformatted.html.twig
  • views-view-fields--foobar--page.html.twig
  • views-view-fields--page.html.twig
  • views-view-fields--foobar.html.twig
  • views-view-fields.html.twig

Refer Views template files for more information.

4

Create a services.yml file (an example file is under sites/default)

In that file, set debug: true (set to false in example file)

Save the file and clear you cache.

If you go to your view and inspect the code it will show you all of the theme suggestions you can use (similar to what theme info did in Views 3)

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