You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Twig模板显示店铺名称与Logo?

解决方案:添加店铺信息容器并处理Logo/名称显示

看起来你需要给产品展示模板补上店铺信息的容器,同时搞定店铺Logo和名称的显示逻辑——我来帮你调整代码,同时针对「无店铺名称字段」的情况给出两种常见解决方案:

<article{{ attributes }}>
  {# 新增店铺信息容器,用来统一包裹Logo和名称 #}
  <div class="store-info-container">
    {# 店铺Logo显示 - 基于你提到的field_professionnel_logo字段 #}
    {% if content.field_professionnel_logo is not empty %}
      <div class="store-logo">
        {{ content.field_professionnel_logo }}
      </div>
    {% endif %}

    {# 店铺名称显示 - 分两种场景处理 #}
    <div class="store-name">
      {# 场景1:如果之后打算给产品添加店铺名字段(比如field_store_name) #}
      {# {{ content.field_store_name }} #}

      {# 场景2:如果产品关联了独立的店铺实体(更合理的架构,比如字段是field_professionnel) #}
      {% if product.field_professionnel is not empty %}
        {{ product.field_professionnel.entity.name }}
      {% else %}
        <span class="default-store-text">未设置店铺</span>
      {% endif %}
    </div>
  </div>

  {# 保留你原有的产品展示代码 #}
  <h3 class="views-teaser-title">
    {{ product.title }}
  </h3>
  <div class="views-teaser-variation">
    {{ product.variation_price }}
    {{ product.variations }}
  </div>
  {# 其他原有内容... #}
</article>

关键修改说明:

  • 新增店铺容器:添加了.store-info-container,方便你统一控制店铺信息的样式布局,解决「无法显示店铺容器」的问题
  • 店铺Logo处理:用{% if %}判断字段是否有内容,避免空标签出现;直接调用content.field_professionnel_logo会自动使用你在后台配置的图片显示格式(比如缩略图尺寸)
  • 店铺名称处理:
    • 如果你计划直接在产品类型里新增店铺名称字段,取消对应注释即可使用
    • 如果产品是关联到独立的店铺实体(更符合电商架构逻辑),通过product.field_professionnel.entity.name获取店铺实体的名称;没有关联时显示默认提示文本,避免空白
  • 原有产品展示的代码完全保留,不会影响已有的功能

额外补充:

如果你的field_professionnel_logo是店铺实体的字段(不是产品本身的字段),只需要把Logo的代码改成下面这样:

{% if product.field_professionnel is not empty and product.field_professionnel.entity.field_professionnel_logo is not empty %}
  <div class="store-logo">
    {{ product.field_professionnel.entity.field_professionnel_logo }}
  </div>
{% endif %}

内容的提问来源于stack exchange,提问作者Fred72

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:09:15