1

I have a Drupal 8 install that I am trying to add a variable to a href URL when it equals #blog but I can't get it to work. Can anyone see what I am doing wrong?

This is my menu template file:

{{item.url}} 
{% if item.url == '#blog'  %} 
    <a href="{{ item.url }}" class="dropdown-toggle" style="color:red;" data-target="#" data-toggle="dropdown">{{ item.title }} <span class="caret"></span> /Tksksks iss</a>
{% else %}
    {{ link(item.title, item.url) }}
{% endif %}
  • what you want to do? if url == #blog create <a else render it simply? what is your problem, you can't add '#blog'? – Yuseferi Sep 7 '16 at 5:31
  • just guessing, maybe try {% if item.url[0] == '#blog' %} – No Sssweat Sep 7 '16 at 5:52
  • Nope that did not work – purencool Sep 8 '16 at 7:43
2

I just ran into this and have a late answer for you. Your twig template's if statement does not work because your string comparison is against the Drupal Core URL object. You can, however, create a variable in twig and use that for your comparison.

{% set menuUri %}{{ item.url }}{% endset %}
{% if menuUri == '#blog'  %} 
    <a href="{{ item.url }}" class="dropdown-toggle" style="color:red;" data-target="#" data-toggle="dropdown">{{ item.title }} <span class="caret"></span> /Tksksks iss</a>
{% else %}
    {{ link(item.title, item.url) }}
{% endif %}

This will let your comparison to the URL "#blog" work just fine.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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