Tuesday 30 April 2019

How to find text from URL in Twig Template drupal 8?

It is easy way to find any text in any string or url in a Twig template file, here is we have syntax structure for this problem, have a look.

Syntax:

                 {% set testvar = url('<current>') %}
                 {% if 'test' in testvar|render|render %}
                         <p>url contains "test"</p>
                 {% endif %}

Description: here we set a 'testvar' variable with contain of current page URL, and then we find a 'test' string in 'testvar' variable means in URL, if contain string then <p> tag data will print.

Example:
               {% set testvar = node.field_link.0.url %}
               {% if '/node/add' in testvar|render|render %}                               
               {% else %}
               <a href="{{ node.field_link.0.testvar }}">Read More</a>
               {% endif %} 

Description: here 'testvar' variable contain the value of that 'node.field_link.0.url' field. and we are checking/finding the '/node/add' string. if this will exist in then data will not display and in else case 'Read More' will display with that link.



Note: If have any suggestions or issue regarding 'How to find text from URL in Twig Template drupal 8?' then you can ask by comments.

3 comments:

  1. The double "render" works pretty well (and also saved my day). Thanks a lot!

    ReplyDelete
  2. Useful code. It resulted in the solution of our problem. Thanks!

    ReplyDelete
  3. Seems to ignore anything in the query string, so anything that follows the ? in the url.

    ReplyDelete

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...