基础篇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麆

麫麃鹿鹾鼄鹼鹞鹛點鵞鵛鵚黈鹴鸈鹞鹛黈鵙鶀鵾鵕鵔鷃鵒黈黄鵏鵎鵌鵉鵅黪黇黆黄黂鸕鸔鸓鸐黶鵻鵸鹹鹶黀麿鼆麺麜鵁黈鹴麊鵱黪鶝鹶鴿鴼鴺鸫默黈黾黴鴷麃鹇鴴黶鸕鸔鸓鸐黈鸴鸲黈黹黷黾黴黲黮黶鹕鹑黈麮黾黴鴳鴷黶鸕鸔鸓鸐鵚鵚鵩鷐黪

display submitted

content麆鶟鴰鴭默黾黴黶鴳鷝鷉鴪黈鴩鴦鸕鸔鸓鸐黶鴥鴡鹞鹛鴝admin/structure/types/manage/content-type/display鴜黩黶鴘鴕黏黾麮鴓鴐鶀鵾鴳鷝黶鷉鴪黈鴘鴕黢鴌麫黶鷉鴪鵙鹇鶀鵾黈鴋麊鵱黈鶪黩content-type鼄鸕鸔鸓鸐黶鴇鴃鷃鵒黪黽黺鴂黂黶黾黴鴷麃article鸕鸔鸓鸐黈鸴鸲黈黂黲黮黩鶀鵾鷯content黶鹕鹑黈鹊鷑鵯article鸕鸔鸓鸐黶鴳鷝鷉鴪鳾鶀鵾鷯鸍黈鹎鴰鳼鵱鶷鳸鷭鳵鳸鳲黍麮鳯鳬黪

content type manage display

鳫鳪鹕鹑黈黢默鳧鳥鸆鳡黈黇鷿麑鳠鳞麺鷉鴪鳝鳛鶀鵾鷯鸍黈鶶鳘黇黆黄鸚麫鸓鳖鼆鸆黶鳓鳐麆

{{ content.field_name }}

黂content鶛鹶麫黴鳎鸖鷉鴪黶鴇鳋鷃黪黇黆黄黂鸕鸔鸓鸐黶鷉鴪鹚鹙鹹鹶鳇鷨鳃鼁麺鷉鴪黶鴇鳋鷃黪鸯鸫鼆鷸鳁鳡鶀鵾鷯黶鷉鴪黈鲿鸝鲾鲻鸕鸔鸓鸐黶鴥鴡鹞鹛鴝admin/structure/types/manage/content-type/display鴜黩黶鴘鴕黏黾黈黕鹴鼄鹼鶀鵾鷭鳵黈鷭鳵黶鲸鴕黈黄鵏鷉鴪黶鲶鳡鲳黪

鸴鸲黈鼆麤黽黺麫鼃鹶黶鲰麧鸍鶀鵾鵱鲯麮鳲黍鷉鴪黪鷋鶼鵯鵱鲯鶲麿鼁鲭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>

本书共76小节:

评论 (4)

    • 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会消耗大量内存,因此开启之前应该先提高内存限制,详见第五节。

  • 29
    • 2018-05-27

    主题开发教程写的真不错,有的文字怎么是乱码啊

  • 你好,谢谢支持,文字做了加密,只支持符合标准的现代浏览器比如Chrome、Firefox、Edge