基础篇31. 内容的覆写(中)

{#
/**
 * @file
 * Theme override to display a node.
 *
 * Available variables:
 * - node: The node entity with limited access to object properties and methods.
 *   Only method names starting with "get", "has", or "is" and a few common
 *   methods such as "id", "label", and "bundle" are available. For example:
 *   - node.getCreatedTime() will return the node creation timestamp.
 *   - node.hasField('field_example') returns TRUE if the node bundle includes
 *     field_example. (This does not indicate the presence of a value in this
 *     field.)
 *   - node.isPublished() will return whether the node is published or not.
 *   Calling other methods, such as node.delete(), will result in an exception.
 *   See \Drupal\node\Entity\Node for a full list of public properties and
 *   methods for the node object.
 * - label: The title of the node.
 * - content: All node items. Use {{ content }} to print them all,
 *   or print a subset such as {{ content.field_example }}. Use
 *   {{ content|without('field_example') }} to temporarily suppress the printing
 *   of a given child element.
 * - author_picture: The node author user entity, rendered using the "compact"
 *   view mode.
 * - metadata: Metadata for this node.
 * - date: Themed creation date field.
 * - author_name: Themed author name field.
 * - url: Direct URL of the current node.
 * - display_submitted: Whether submission information should be displayed.
 * - attributes: HTML attributes for the containing element.
 *   The attributes.class element may contain one or more of the following
 *   classes:
 *   - node: The current template type (also known as a "theming hook").
 *   - node--type-[type]: The current node type. For example, if the node is an
 *     "Article" it would result in "node--type-article". Note that the machine
 *     name will often be in a short form of the human readable label.
 *   - node--view-mode-[view_mode]: The View Mode of the node; for example, a
 *     teaser would result in: "node--view-mode-teaser", and
 *     full: "node--view-mode-full".
 *   The following are controlled through the node publishing options.
 *   - node--promoted: Appears on nodes promoted to the front page.
 *   - node--sticky: Appears on nodes ordered above other non-sticky nodes in
 *     teaser listings.
 *   - node--unpublished: Appears on unpublished nodes visible only to site
 *     admins.
 * - title_attributes: Same as attributes, except applied to the main title
 *   tag that appears in the template.
 * - content_attributes: Same as attributes, except applied to the main
 *   content tag that appears in the template.
 * - author_attributes: Same as attributes, except applied to the author of
 *   the node tag that appears in the template.
 * - title_prefix: Additional output populated by modules, intended to be
 *   displayed in front of the main title tag that appears in the template.
 * - title_suffix: Additional output populated by modules, intended to be
 *   displayed after the main title tag that appears in the template.
 * - view_mode: View mode; for example, "teaser" or "full".
 * - teaser: Flag for the teaser state. Will be true if view_mode is 'teaser'.
 * - page: Flag for the full page state. Will be true if view_mode is 'full'.
 * - readmore: Flag for more state. Will be true if the teaser content of the
 *   node cannot hold the main body content.
 * - logged_in: Flag for authenticated user status. Will be true when the
 *   current user is a logged-in member.
 * - is_admin: Flag for admin user status. Will be true when the current user
 *   is an administrator.
 *
 * @see template_preprocess_node()
 *
 * @todo Remove the id attribute (or make it a class), because if that gets
 *   rendered twice on a page this is invalid CSS for example: two lists
 *   in different view modes.
 */
#}
<article{{ attributes }}>

  {{ title_prefix }}
  {% if not page %}
    <h2{{ title_attributes }}>
      <a href="{{ url }}" rel="bookmark">{{ label }}</a>
    </h2>
  {% endif %}
  {{ title_suffix }}

  {% if display_submitted %}
    <footer>
      {{ author_picture }}
      <div{{ author_attributes }}>
        {% trans %}Submitted by {{ author_name }} on {{ date }}{% endtrans %}
        {{ metadata }}
      </div>
    </footer>
  {% endif %}

  <div{{ content_attributes }}>
    {{ content }}
  </div>

</article>

鼆鼃鼂鼁黽黺黷黴黱黯黽黬黨黥黤

黨黥黠黯點黛黗黺黷黓黐黏黌黋黉黆黯黅黁麾麻麸麷麴點黛黠麳麯麮麭麩麥黯麢麟麝麛麙黤

鼆麕黺黷麒麐麌麭麊麈麇黯麄鼁麁鹽

page鹽 鼆麕黯page麛鹼鹻鹸鼃鹶鼃黽黬鹴鹳麾黌鹰麾黽黬1麾鼃黺黷鹫鹧黯鹤鼁麭黽黬黤鹣鼆麭黽黬鹟鹛鹚麴鹗鹖黠黯鹔鹐麾黌鹰鹌鹴黯鹗鹖麾鹋鹉鹆鼃鼁麭鹴鹳麾鹃鼃鼁麭鹀麇麾鸾黅鸺鸸teaser鸵鸴鸲麾黺黷鼆麭node黨黥麾鸱鸭鹻鹸鼃鹶黗page麾鸬麸麷鸫鸪黽黬鹴鹳黯html麾鸨麸麷鸫鸪黽黬鹀麇黯html黤

鹣鸤麾麻鸨麸麷鹆鸢麛鹻鹸麾鹃鸠鸟鸢麛node--[鸞鸜鸛鸙]--teaser.html.twig鼆麭黨黥鸘鸠鸟鸫鸪黽黬麴鸞鸜鹗鹖黠黯鸕鸓黤鼆鸒黯鸎麾麻鹉鹆鸌麇麴node黨黥黠鸊黗鸆鸂鷿鷽黱鷹鷶黤

label鹽麛鹼鹫鷲黽黬黯鷮鷭黤黺黷麸麷鷪麯鷹鷶黠鷧麴黽黬黗teaser鷥鷤麁鷢鹫鷲鷮鷭麾麴鹴鹳鷥鷤麁鷟鷝鹫鷲鷮鷭麾鼆鼃鸴黗麾麴鹴鹳鼂麾鷝鼁麭鷙鷗黓黐黯鷮鷭鷓鷑黤黺黷鸱鷎鷍麸麷鷉鷇鼆麭鷓鷑麾鷄麴node黨黥黠鸢麛label麩麥鸘鹫鷲鷮鷭黤麴鼆麕麾黺黷麛bootstrap黗黺黷黓黐黯黅鷃鷀黠鸛鶼text-align-center鸘鶻鷮鷭鷀黠黤鹃鶹麾鶵鹼鼆麕鼃鹴鹳黯鷮鷭麾鸴鸲麾鹆鸌麇鶲麴teaser黠鶮鸒麾鶬鼁麭鶩鸟黤

title_ prefix

title_ suffix

鼆鸆麭鼃黗鶦鹋黨鷑鶣鶟黯麾鶝黷麮鶙麴鷮鷭黯鶗鹳麝鶖鹳鶒鹧鶦鹋黨鷑鶏麇鹫鷲黯鶎鶍麾麻鹆鶉鶅鶃鶁鶝黷黤黌鹰Contextual Links黨鷑麾鷍麴鷮鷭黯鶀鵿鼁麭麛鸘鵾鵽黽黬黯鵻鵹鵵鷮黤鹰鸓麻鵱鼆鸆鵮鷹鷶鶃鶁黏麾鵫鶉黯鵵鷮鹉鷍鵨鵦黤

display_ submitted鹽

麛鹼鹻鹸鼃鹶鹛鹚黓鵤鵢鵡麾鹰鸓鹛鹚麾鵠鶀鵿鵝鵙鵖鶲麝鶼鵒麾麷鵎鵊鵇鵃鵂黤麻麸麷麴鸞鸜鸛鸙黯鵾鵽鹴鹳麳麯鼆麭麐鵀黤鶗鹳鴽鴹鴸鸭黏麾黽黬鴷鹼鹆鴴黯鸞鸜鸛鸙麾鸴鸲麾黴黱黽黬黨黥黯鹔鹐麾麝黽黬鴲鴷黯鸞鸜鸛鸙鵡鵡鵫鷉黤

content鹽鶝鴯鴬黏黽黬黯鴲鷝鷃鴫黤黺黷鴪麴黯黽黬鴷鹼article鸞鸜鸛鸙麾鸴鸲麾麴黨黥黠鶀鵿鷲content黯鹔鹐麾鹉鷍鵱article鸞鸜鸛鸙黯鴲鷝鷃鴫鴦鶀鵿鷲鸘麾鹋鴯鴢鵵鶲鴟鷮鴞鴟鴜黅麝鴘鴕黤

鴔鴑鹔鹐麾黗黏鴐鴏鸒鴋麾麻鸌麇鴊鴈麭鷃鴫鴆鴃鶀鵿鷲鸘麾鶮鴂麻麸麷鸢麛鸛鴀鼆鸒黯鳾鳼鹽

{{ content.field_name }}

麴content鶖鹳麛黬鳸鸟鷃鴫黯鳶鳵鶼黤麻麸麷麴鸞鸜鸛鸙黯鷃鴫鹗鹖鹴鹳鳲鷪鳰鼁麭鷃鴫黯鳶鳵鶼黤

鸴鸲麾鼆麕黺黷麛鼂鹳黯鳬麙鸘鶀鵿鵵鳩麝鴜黅鷃鴫黤鷄鶹鵱鵵鳩鶬麯鼁鳧div麕鹳麾鷄鶹鳥鼆麭div鼁麭鶗鹳麛鸭黯css鸛鶼麾鸘鸢鳤鵵鳩鷀黠黤

鸲鳠麾鳞鳚鴔鸜鳗鳓鼁麭鳏鳍鹽麴node鹴鹳黨黥黠麾麛鼂鹳黯鷹鷶鸊鶀鵿鴲鷝黯鷃鴫黤鼆鸒鸸黯鳋鳠鼃麾鹰鸓鶦鶝鳈鳅鳄鳀鲽黗鼆麭鸞鸜鸛鸙鳞鲹鲷黏鼁麭鷃鴫黯鸎麾鼆麭鳞鷃鴫鹉鲴麙鹟鹛鹚鷲鸘黏黤鼆鸒鹉鷍鲳鲱鳈鳅鲮鲫黯鲩鵦黤鵝黗鼁麭themer麾麻黯鲨黗鹆鶉鶅鲦鲢鳈鳅黯鲮鲫黤

鸴鸲麾黗黏鲠鲜鼆鸂鳏鳍麾黺黷鲘鼃麇鵱content鶀鵿鷲鸘麾鸤鶖麛without鸊鲗鲔鶗鴽鴹鶀鵿鸭黯鷃鴫麾鼆鸒黯鸎麾麷鶖鳞鲐鲷黯鷃鴫鲘鼃鷍鹟鼆鵮鷹鷶鶀鵿鷲鸘黤

{{ content|without('field_image','body','comment','field_tags') }}

點鲎鼆麕麾黺黷鶁黏鵱鵵鳩麝鴜黅鲋鶁黏鲔鲇麾鲘鲋鶁黏鴘鴕麝鷮鴞黤鼆鸒麾鹴鹳鼂鹉鹆鷍鶀鵿鴘鴕麝鷮鴞鷃鴫黤鼆麕鼆鸨鸸鷧鼃黗黏黽鲄鹔鲀麾鸴黗麾麻鶉鶅麴鸞鸜鸛鸙黯鳄鳀鹛鹚鹴鹳鸊鱾麛鹆鸌麇黯鷃鴫黤

鱺鲇麾鱸鳤點鲎黯鼃麾麴鼆麭鳄鳀鹛鹚黯鹴鹳鼂麾鲘麸麷鴐鴏鷃鴫鶀鵿黯鱴鱱麾鱰鼃鼆麭鲮鲫鷧鷝麴麻鸢麛content鶀鵿鴲鷝鷃鴫黯鹔鹐鷢鱯鸕黤鹰鸓麻麴黨黥黠鸢麛content.field_name鴆鴃鶀鵿黏鴈麭鷃鴫黯鸎麾鹋黯鱴鱱鶵鹋麴黨黥黅鱮黠鷲鴪黯鱭鱫鸘鱪麢

鸤鶖黺黷鱨鵱麁鹳鼆鱤麛鹼鹫鷲鷃鴫黯鷹鷶鱢鱟鶬麯鼁麭if鹻鹸黠麾鷧麛麴鹴鹳鼂鷢鶀鵿鼆鱤鷃鴫麾鹃鹀麇黯鱟鱜麾鸨鶉鶅鶬麴鱺鼁麭if鹻鹸黠麾鷄鶹鲘麇鴐鴏鼁麁title prefix 麝 suffix黯鱭鱫黤麊鶖麾鹆麇鱘鱔鷉鷇鳞鲐鲷黯if鳬鱑黤

鸤鶖黺黷鱍麯鷓鷑鵇鱉鹴鹳麾鱾麛page title鷓鷑麾鹉麸麷鲠鲜鹴鹳鼂鷮鷭麈鱈鸆鱅黯鴪鱁黤鹣鸤麾麻鲘麸麷鴐鴏鵾鵽鰾鴆黯鸒鴋麾鼆麕鹉鲄鰻黏黤

麷麁鼃鰹鰶鲔鶖黯node--article.html.twig黨黥黯鷹鷶鰴鰰鰬鰩鰥鹽

{#
/**
 * @file
 * Theme override to display a node.
 *
 * Available variables:
 * - node: The node entity with limited access to object properties and methods.
 *   Only method names starting with "get", "has", or "is" and a few common
 *   methods such as "id", "label", and "bundle" are available. For example:
 *   - node.getCreatedTime() will return the node creation timestamp.
 *   - node.hasField('field_example') returns TRUE if the node bundle includes
 *     field_example. (This does not indicate the presence of a value in this
 *     field.)
 *   - node.isPublished() will return whether the node is published or not.
 *   Calling other methods, such as node.delete(), will result in an exception.
 *   See \Drupal\node\Entity\Node for a full list of public properties and
 *   methods for the node object.
 * - label: The title of the node.
 * - content: All node items. Use {{ content }} to print them all,
 *   or print a subset such as {{ content.field_example }}. Use
 *   {{ content|without('field_example') }} to temporarily suppress the printing
 *   of a given child element.
 * - author_picture: The node author user entity, rendered using the "compact"
 *   view mode.
 * - metadata: Metadata for this node.
 * - date: Themed creation date field.
 * - author_name: Themed author name field.
 * - url: Direct URL of the current node.
 * - display_submitted: Whether submission information should be displayed.
 * - attributes: HTML attributes for the containing element.
 *   The attributes.class element may contain one or more of the following
 *   classes:
 *   - node: The current template type (also known as a "theming hook").
 *   - node--type-[type]: The current node type. For example, if the node is an
 *     "Article" it would result in "node--type-article". Note that the machine
 *     name will often be in a short form of the human readable label.
 *   - node--view-mode-[view_mode]: The View Mode of the node; for example, a
 *     teaser would result in: "node--view-mode-teaser", and
 *     full: "node--view-mode-full".
 *   The following are controlled through the node publishing options.
 *   - node--promoted: Appears on nodes promoted to the front page.
 *   - node--sticky: Appears on nodes ordered above other non-sticky nodes in
 *     teaser listings.
 *   - node--unpublished: Appears on unpublished nodes visible only to site
 *     admins.
 * - title_attributes: Same as attributes, except applied to the main title
 *   tag that appears in the template.
 * - content_attributes: Same as attributes, except applied to the main
 *   content tag that appears in the template.
 * - author_attributes: Same as attributes, except applied to the author of
 *   the node tag that appears in the template.
 * - title_prefix: Additional output populated by modules, intended to be
 *   displayed in front of the main title tag that appears in the template.
 * - title_suffix: Additional output populated by modules, intended to be
 *   displayed after the main title tag that appears in the template.
 * - view_mode: View mode; for example, "teaser" or "full".
 * - teaser: Flag for the teaser state. Will be true if view_mode is 'teaser'.
 * - page: Flag for the full page state. Will be true if view_mode is 'full'.
 * - readmore: Flag for more state. Will be true if the teaser content of the
 *   node cannot hold the main body content.
 * - logged_in: Flag for authenticated user status. Will be true when the
 *   current user is a logged-in member.
 * - is_admin: Flag for admin user status. Will be true when the current user
 *   is an administrator.
 *
 * @see template_preprocess_node()
 *
 * @todo Remove the id attribute (or make it a class), because if that gets
 *   rendered twice on a page this is invalid CSS for example: two lists
 *   in different view modes.
 */
#}



  {% if not page %}
<article{{ attributes }}>
    {{ title_prefix }}
    <h2{{ title_attributes }}>
      <a href="{{ url }}" rel="bookmark">{{ label }}</a>
    </h2>
    {{ title_suffix }}
  {% endif %}


  {% if page %}
<article{{ attributes }}>
    <h1 class="text-align-center">
      {{ title_prefix }}
      {{ label }}
      {{ title_suffix }}
    </h1>

    <div{{ content_attributes }}>
      <div class="text-align-center">
          {{ content.field_image }}
      </div>

        {{ content.body }}
    </div>

    {% if display_submitted %}
      <footer>
          {{ author_picture }}
        <div{{ author_attributes }}>
            {% trans %}Submitted by {{ author_name }} on {{ date }}{% endtrans %}
            {{ metadata }}
        </div>
      </footer>
    {% endif %}

    {{ content|without('field_image','body','comment','field_tags') }}
  {% endif %}
</article>

本书共42小节:

评论 (2)

    • 2017-10-20

    想获取node--article.html.twig模版上的标题title变量,听了您的课程,装了devel kint 模块,写上{{ kint() }} 页面就没反应了。现在只知道page模版上的title 变量为{{ page['#title'] }},那么在node模版上怎么表示呢?{{ kint() }}撤掉就没事,写上就没反应,是我哪里搞错了吗?

    • 2017-10-21

    In reply to by silence

    label用于输出节点标题,文档中有的。   kint会消耗大量内存,因此开启之前应该先提高内存限制,详见第五节。