Search code examples
tagstwigdrupal-8markup

How to display the title of the node in plain text in TWIG on Drupal 8?


I created TWIG for my node, my products, my groups and my stores.

I'm on Drupal 8. For my nodes, I want to display the title. I use the following code:

{{ label }}

But there is the markup inside and I do not want it. So I tried the following code and it works. But is it a good practice ?

{{ label.0 }}

How to display the title of the node in plain text in TWIG on Drupal 8 ?


Solution

  • When using the node.html.twig or a child of that template, you will have access to the node variable, which is a limited implementation of the node object.

    From the comments for the classy theme node.html.twig file:

     * - 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. 
    

    As is described in that comment, the value of the node title without any markup can be accessed in twig by using:

    {{ node.label }}.