How Drupal 8 uses Twig, how different data is transferred from general template to specific templates?

For view block I have created block template as per default template suggestions. In my new Twig template if I print {{ content }} everything (view result fields) are getting rendered as per Drupal default.

How can I access and apply my theme markup to all view fields in my block template. {{ dump(_context|keys) }} display below.

enter image description here

enter image description here


In block--views-block--page-fields-block-1.html.twig

You can access the view fields like this:

{% for row in content['#view'].result %}

  {{ content['#view'].field['field_name'].value(row) }} 
  // change field_name to the actual field machine name. 

{% endfor %}

Personally, I rather use views-view-fields--[view-name]--[machine-name].html.twig

In your case should be views-view-fields--page-fields--block-1.html.twig

Here you do not need to do a for loop and you can access the fields easily with:

{{ fields.field_name.content }}
// change field_name to the actual field machine name. 
  • Now how can i get field_image url with first approach, currently getting file id – harish Nov 26 '18 at 13:30
  • @harish use the Image URL Formatter module. Then in views click on your image field and set the formatter to URL. – No Sssweat Nov 26 '18 at 23:39
  • That i have in D8 without any module, in dashboard view results i get image file url, but in view template your above twig syntax gives numeric value ( target file_id ) need to find how to get entity and then uri from this numeric file id – harish Nov 27 '18 at 5:24
  • How can i get header and footer for view – harish Nov 29 '18 at 5:56

If you want to render the block into your custom template:

  1. Create your twig file block--views-block-page-fields-block-1.html.twig inside templates folder in your custom module.

  2. implement hook_theme like the following:

     * Implements hook_theme().
    function yourModuleName_theme($existing, $type, $theme, $path) {
      return [
        'block__views_block_page_fields_block-1'     => [
          'base hook' => 'block'

  1. Clear cache.
  2. Now you have content variable.

If you use Twig Tweak you can use drupal_view_result to get view result, so you can access to the fields like:

{% for result in drupal_view_result(content['#view'].id, content['#view'].current_display) %}
  {{ result._entity.field_name.value }}
{% endfor %}
  • Thanks for your answer....I am having access to content variable without hook_theme, My question is how can i access specific fields in my template using content. e.g. field_title, field_field_slides from this content variables as i shared in image none of key in content taking me to view fields – harish Nov 24 '18 at 17:15
  • I want something information that i can traverse through content variable to get my view fields .... don't i have access them in this block template ? – harish Nov 24 '18 at 17:18
  • The question wasn't about rendering a block... was about how to access view field values. – No Sssweat Nov 26 '18 at 9:47
  • Thanks ..Twig tweak approach is also working..Good that i could easily get image source for image field result._entity.field_image.entity.fileuri – harish Nov 26 '18 at 17:28
  • You are welcome, i'm glad that help. – berramou Nov 26 '18 at 17:29

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.