1

In my page.html.twig template file, I'm looking to set classes depending on the content type the page is being viewed. I pretty much have two options, if the page being viewed is a program page, then I want one style applied to the div, if it's anything else, then the other style can be applied.

{% 
  set aside_classes = [
    node.content-type is 'program' ? 'program',
    node.content-type is anything else ? 'standard'
  ]
%}

I know the above code is not proper but it's an example of what I'm trying to achieve.

I would then have the following code below on my div:

<div{{ content_attributes.addClass(aside_classes) }}> 

Am I going about this right or is there a better way to achieve this?

EDIT: Here are my kint variables. Not sure if this helps at all?: Kint Variables

5
  • With default Drupal config, you should see two classes automatically created on the body tag for all page types in Drupal. I would expect you to see the classes ".path-node" and ".node-type-program". This is done in the html.twig template file with {{ attributes.addClass(body_classes) }} on the body tag. Do these not work for your situation? Commented Jan 30, 2018 at 21:44
  • I see what you're saying but I'd prefer to add the style to the specific div within the body depending on the content type. Would the following work? node_type is 'program' ? 'program'. But how would I tell it to add 'standard' if it's anything else? Commented Jan 30, 2018 at 21:56
  • @ACanadianCoder something like this should work {% node.content-type is 'program' ? 'program' : 'standard' %}
    – cchen
    Commented Jan 30, 2018 at 22:26
  • @cchen that didn't seem to work. Just got a page error. Commented Jan 31, 2018 at 15:06
  • @ACanadianCoder sorry that was not the exact working code..I think the answer below should work, you just need to modify it a little bit to make for the page level template.
    – cchen
    Commented Jan 31, 2018 at 15:37

2 Answers 2

2

Not sure about page.html.twig. Would have to dig around...

In node.html.twig:

{%
  set newClassNameVar = [
    node.type.entity.label == 'Program' ? 'Program' : 'Not Program'
  ]
%}

<div{{ attributes.addClass(newClassNameVar) }}></div>

or:

{%
  set newClassNameVar = [
    node.bundle == 'content__type' ? 'set-yes-class' : 'set-no-class'
  ]
%}

<div{{ attributes.addClass(newClassNameVar) }}></div>

use debug such as {{ dump(_context | keys) }} to see what is available and best suits your use case.

5
  • I've added my kint screengrab to the original post from my page.html.twig file. Does any of that help? Commented Jan 31, 2018 at 15:09
  • I ended up just using your method mentioned above but used {% if node.bundle == 'program' %} and that seemed to work. I still think there's a more elegant way of doing it similar to what @cchen had suggested in the above post but this will work for now. Thanks again! Commented Jan 31, 2018 at 15:41
  • updated my answer with two options, credit to @cchen too Commented Jan 31, 2018 at 16:12
  • Awesome...that works and looks much nicer...however, instead of using the label, is there a way of using the machine name? ex. node.type.entity.machine_name == 'program'. Sorry for being so picky. Commented Jan 31, 2018 at 16:36
  • 1
    I was looking myself and will try some more when I can. node.type.entity.machine_name looks like the right idea. Also I am always a fan of {{ dump(node | keys) }}, it's a lot lighter than kint. {{ dump(_context | keys) }} is a quick way to see everything available such as 'node', 'elements', 'content'... Commented Feb 1, 2018 at 17:15
1

Another way to do this is to add the class programmatically in your custom theme as discussed here.

/**
 * Implements template_preprocess_html().
 */
function MYTHEME_preprocess_html(&$variables) {
  if (isset($variables['node_type'])) {
    $node = \Drupal::routeMatch()->getParameter('node');

    // Add node-TYPE class to the element.
    $variables['attributes']['class'][] = 'page-node-' . $node->getType();
    $variables['attributes']['class'][] = 'page-node-' . $node->id();
  }
}

Your Answer

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

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