简体   繁体   中英

Drupal block twig file

I have a content type as "news". for this content type i need to show latest 5 articles. so i created block by using views. it is showing articles finely but i need to add my own css using twig files. i tried following options but none of them worked correctly.

   block--newsblock.html.twig
block--newsblock-block.html.twig
views--view-newsblock-block.html.twig

But when i applied in following way , the field template is calling.

views-view-fields--newsblock--block.html.twig

What i want is for first element in block i need to show some teaser text for rest of them i need to show only title. how can i do it?

activate twig debug and look at your console it will tell you suggestions of file names to use

Alternatively add this hook to your_theme_name.theme to proppose your own suggestion

/**
 * Implements hook_theme_suggestions_HOOK_alter() for block templates.
 */
function your_theme_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  $block_id = $variables['elements']['#id'];

  /* Uncomment the line below to see variables you can use to target a block */
  // print $block_id . '<br/>';

  /* Add classes based on the block id. */
  switch ($block_id) {

    case 'your_block_id':
      $suggestions[] = 'block__newsblock';
      break;
  }
}

it will be then block--newsblock.html.twig

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM