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

本书共56小节:

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