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.
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.
The double "render" works pretty well (and also saved my day). Thanks a lot!
ReplyDeleteUseful code. It resulted in the solution of our problem. Thanks!
ReplyDeleteSeems to ignore anything in the query string, so anything that follows the ? in the url.
ReplyDelete