templates - 如何显示城市和邮政编码?

标签 templates twig drupal-8

我有一个使用 Drupal 8 的站点,我想自定义我的页面模板。

我使用模块:

  • 《简介》https://www.drupal.org/project/issues/profile
  • “地址”https://www.drupal.org/project/address

  • 这是模块“地址”的模板:
    {#
    /**
     * @file
     * Default template for the 'plain' address formatter.
     *
     * Available variables:
     *   - given_name: Given name.
     *   - additional_name: Additional name.
     *   - family_name: Family name.
     *   - organization: Organization.
     *   - address_line1: First address line.
     *   - address_line2: Second address line.
     *   - postal_code: Postal code.
     *   - sorting_code: Sorting code.
     *   - dependent_locality: The dependent locality.
     *     - dependent_locality.code: Dependent locality code.
     *     - dependent_locality.name: Dependent locality name.
     *   - locality: The locality subdivision.
     *     - locality.code: Locality code.
     *     - locality.name: Locality name.
     *   - administrative_area: The administrative area subdivision.
     *     - administrative_area.code: Administrative area code.
     *     - administrative_area.name: Administrative area name.
     *   - country: The country.
     *     - country.code: Country code.
     *     - country.name: Country name.
     *
     * if a subdivision (dependent_locality, locality, administrative_area) was
     * entered, the array will always have a code. If it's a predefined subdivision,
     * it will also have a name. The code is always prefered.
     *
     * @ingroup themeable
     */
    #}
    <p class="address" translate="no">
      {% if given_name or family_name %}
        {{ given_name }} {{ family_name }} <br>
      {% endif %}
      {% if organization %}
        {{ organization }} <br>
      {% endif %}
      {% if address_line1 %}
        {{ address_line1 }} <br>
      {% endif %}
      {% if address_line2 %}
        {{ address_line2 }} <br>
      {% endif %}
      {% if dependent_locality.code %}
        {{ dependent_locality.code }} <br>
      {% endif %}
      {% if locality.code or postal_code or administrative_area.code %}
        {{ locality.code }} {{ postal_code }} {{ administrative_area.code }} <br>
      {% endif %}
      {{ country.name }}
    </p>
    

    这是我自定义个人资料页面的模板:
    {#
    /**
     * @file
     *
     * Default theme implementation for profiles.
     *
     * Available variables:
     * - content: Items for the content of the profile.
     *   Use 'content' to print them all, or print a subset such as
     *   'content.title'. Use the following code to exclude the
     *   printing of a given child element:
     *   @code
     *   {{ content|without('title') }}
     *   @endcode
     * - attributes: HTML attributes for the wrapper.
     * - profile: The profile object.
     * - url: The profile URL, if available.
     *
     * @ingroup themeable
     */
    #}
    <div{{ attributes.addClass('profile--personnel--teaser') }}>
    
      <div class="views-row">
    
        <div class="views-teaser-profil-address">
          {{ content.field_personnel_adresse }}
        </div>
    
      </div>
    
    </div>
    

    当前显示完整地址。如何只显示城市和邮政编码?

    我只想要没有标签的文本。

    最佳答案

    使用kint()这样您就可以深入研究所有这些值

    {{ kint(content.field_personnel_adresse.0) }}
    <div>
      {{ content.field_personnel_adresse.0.locality['#value'] }}
      {{ content.field_personnel_adresse.0.postal_code['#value'] }}
    </div>
    {{ content|without('field_personnel_adresse')}}
    

    关于templates - 如何显示城市和邮政编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49777810/

    相关文章:

    c++ - 模板 friend 和嵌套类

    c++ - 非类型模板数组?

    php - 使用 Symfony 2 在 Twig 模板中获取内核根目录

    javascript - CKEditor:PasteFromWord 忽略 pasteFilter

    javascript - 收到错误请求的资源上不存在 'Access-Control-Allow-Origin' header

    drupal - 默认情况下禁用通过配置导入的字段。如何启用它们?

    c++ - 无法理解与模板和构造函数相关的错误

    twig - 循环计数

    css - 在 sonata admin 中水平对齐标签和输入

    c++ - 考虑引用和 const 的可变参数函数包装器