0

enter image description hereThis is the code which I have put in node.html.twig. It doesn't print the body and title of the node, whose bundle is Basic page.

How do I print node title and body of a node?

{%
  set classes = [
'node',
'node--type-' ~ node.bundle|clean_class,
node.isPromoted() ? 'node--promoted',
node.isSticky() ? 'node--sticky',
not node.isPublished() ? 'node--unpublished',
view_mode ? 'node--view-mode-' ~ view_mode|clean_class,
'clearfix',
  ]
%}
{{ attach_library('classy/node') }}
<article{{ attributes.addClass(classes) }}>
  <header>
{{ title_prefix }}
{% if not page %}
  <h2{{ title_attributes.addClass('node__title') }}>
    <a href="{{ url }}" rel="bookmark">{{ label }}</a>
  </h2>
{% endif %}
{{ title_suffix }}
{% if display_submitted %}
  <div class="node__meta">
    {{ author_picture }}
    <span{{ author_attributes }}>
      {% trans %}Submitted by {{ author_name }} on {{ date }}{% endtrans %}
    </span>
    {{ metadata }}
  </div>
{% endif %}
  </header>
  <div{{ content_attributes.addClass('node__content', 'clearfix') }}>
     {{ content }}
  </div>
</article>    
2
  • Is the body field set to be displayed in "Manage display" on the content type?
    – johndevman
    May 16, 2017 at 6:34
  • yes it is set to be displayed in the manage display. May 16, 2017 at 8:44

1 Answer 1

1

You can print the node

Title as

{{ label }}

Body

{{ content.body }}

Try this,

{{ content.body| raw }}

It will be used to remove the markup in the content.

6
  • when i use content.body. it prints the body with html tags. for example: i want to print 'lorem ipsum' but it prints '<p>lorem ipsum</p>' . when i put <table> tag it prints that also. I tried node.body.value also it prints the same. what m i doing wrong here? May 16, 2017 at 8:41
  • Make sure that text format should be full html for body field May 16, 2017 at 8:44
  • i tried all the three : basic html, restricted html and full html also. I tried removing the tags, then it prints fine for <p> but in some cases i need to make a table. when i use table tag it displays everything in the front end May 16, 2017 at 8:46
  • Can you send me the screenshot of body field of the node.Please edit the node and take the screenshot May 16, 2017 at 8:50
  • I have added the screen shot in my question. It shows correctly in the editor. but its outputs this : <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody> <tr> <th align="left" width="16%">Name of Vessel</th> <th align="center" width="11%">Year Built</th> <th align="center" width="15%">Cargo Capacity (CBM)</th> <th align="center" width="21%">Ownership</th> <th align="center" width="8%">Flag</th> <th align="center" width="17%">Commercial<br /> Manager</th> ....... May 16, 2017 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.