简体   繁体   English

如何按树枝所在的区域命名树枝文件?

[英]How to name twig files by the region they are in?

I am using Drupal with the Commerce Module to build a webshop. 我正在将Drupal与Commerce Module一起使用来构建网上商店。 I am using the Commerce Cart Block to display a cart icon with the amount of items in the cart, in the navigation bar. 我正在使用Commerce Cart Block在导航栏中显示带有购物车中物品数量的购物车图标。

Now I would also like to display the Cart Block on the Cart page, but with a different template than being used in the navigation bar. 现在,我也想在“购物车”页面上显示“购物车块”,但模板与导航栏中的模板不同。

I am using the debug mode, which let me see what I could call the file names to use them like I would like to. 我正在使用调试模式,该模式使我看到可以使用的文件名,就像我想要的那样。 But above both Cart Blocks it says the same file name, so I can't output two different templates. 但是在两个购物车块上方,它说的是相同的文件名,因此我无法输出两个不同的模板。 I tried putting primary_menu-- before the navigation cart block and content-- (the region the cart block is going to be in), but they don't work. 我尝试将primary_menu(导航购物车块和内容之前)(购物车块将要位于的区域)放在前面,但是它们不起作用。

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'commerce_cart_block' -->
<!-- BEGIN OUTPUT from 'themes/custom/verdamigo/templates/commerce-cart-block.html.twig' -->

This is shown above both cart blocks (which are on the same page). 这显示在两个购物车块上方(在同一页面上)。 So how can I use two different templates for both blocks. 因此,如何为两个块使用两个不同的模板。

primary_menu--commerce-cart-block.html.twig

is not working. 不管用。

I would like to be able to edit both the block in the primary_menu and the block in the content-region. 我希望能够同时编辑primary_menu中的块和内容区域中的块。 But both carts get output with the same template. 但是两个购物车都使用相同的模板获得输出。

In an effort to decouple Blocks from Displays, Drupal 8 renders a block independently of which display it's in and what region/weight it has in that display (see Twig Template naming conventions ) : 为了使图块与显示器脱钩,Drupal 8渲染了一个图块,而与它所在的显示器以及该显示器在哪个区域/权重无关(请参阅“ Twig模板命名约定” ):

Region-specific block templates are not available in Drupal 8. 特定于区域的块模板在Drupal 8中不可用。

This removes the ability to override block.tpl.php by region, and for hook_preprocess_block() to adjust variables based on it. 这消除了按区域覆盖block.tpl.php的功能,并使hook_preprocess_block()可以基于该功能调整变量。 Instead, core developers recommend to manage block template overrides with CSS or using additional blocks. 相反,核心开发人员建议使用CSS或使用其他块来管理块模板替代。

But you can still work around this by implementing hook_theme_suggestions_HOOK_alter() : 但是您仍然可以通过实现hook_theme_suggestions_HOOK_alter()解决此问题:

function SOME_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  if (!empty($variables['elements']['#id'])) {
    $block_id = $variables['elements']['#id'];
    $block = Drupal\block\Entity\Block::load(block_id);
    $region = $block->getRegion();
    // Allow per-region block templating.
    $suggestions[] = 'block__' . $region . '__' . $block_id;
  }

  return $suggestions;
}

Note : the template name should begin with " block " since you override a block template, so in your case the override file should be named block--primary_menu--commerce-cart-block.html.twig . 注意:模板名称应以“ block ”开头,因为您覆盖了一个阻止模板,因此在您的情况下,覆盖文件应命名为block--primary_menu--commerce-cart-block.html.twig

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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