{% if logged_in == false %} 
 <div id="promoiton">
  <div id="promotion-image">
   <div id="promotion-bg">
   <div id="promotion-text">
   <p>some text.</p>
     <li>something here</li>

    <button<a class="btn btn-primary btn-lg" href="#" role="button">Learn
{% else %}

 <iframe{{ attributes }}{% if url is not empty %} src="{{ url }}{% if 
 query is not empty %}?{{ query | url_encode }}{% endif %}"{% endif %}>    
 {% endif %}

Hi Folks, in the code example above I have tried do doing some twig foo. It works greate if th user is looged out.

But what if I like to seperate contnet types by machine name?

So for example you have two names lets say article and block

How to make shure that the field is visible on block but not on artivle for logged out users? in short what is the required functioon to get false or true bi a string containing the content type machine name to do something like

 {% if logged_in == false || somefunction= 'content_type_machine_name' %}

Is that possible?

1 Answer 1


You can set a variable in hook_preprocess_Hook function which has the content type and then use that variable in twig file. $node = \Drupal::routeMatch()->getParameter('node'); $nids = $node->nid->value; if($nids){ $variables['content_type'] = $node->bundle(); }

  • thats $node='content_type' in the if statement?
    – Josh
    Dec 10, 2016 at 10:37
  • {% if content_type == "etc" %} Dec 10, 2016 at 10:55

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.