if string contains substring twig

Solutions

url() returns an array, as 4x4 says. You can see this if you do a for loop on the variable.

path(), however, seems to return a string that you can do your operation on. But it won't include hostname.

The twig function url() returns a render array, when you render it with the twig filter |render you get a markup object and when you render it again you get a string, which you can check whether it contains a substring:

{% set url = url('<current>') %}
{% if 'test' in url|render|render %}
  <p>url contains "test"</p>
{% endif %}
Tags: Theming / Drupal 8

Similar questions

How to check if menu item contains a word using twig
in the menu.html.twig theme template, I want to check if a menu item title contains a specific word. so I tried this codes, but none of them worked. and I have the items: 'separator 1', ' separator 2', 'separator' but couldn't solve the problem!
How can I find out a list of available $string values for user_access($string) function?
user_access($string, $account = NULL) $string: The permission, such as "administer nodes", being checked for. How can I find out a list of available $string values for user_access() function?
can I use db_update to update a field ( appending a string to the current string in the database )
Is it posible to do something like: Or do I have to load the field value first by using db_select() ? Maybe someway with db_query() ?
String Overrides is not overriding the "Read more" string
I've filed a support request on drupal.org about this question, but I figured I should ask here as well because someone could know the answer. I want to change the "Read more" link in teasers so that it says "Full story". I was pointed to the String Overrides module which supposedly makes it very simple to change the built-in strings. The UI isn't ...
Inserting UTF-8 encoded string into UTF-8 encoded mysql table fails with "Incorrect string value"
Inserting UTF-8 encoded string into UTF-8 encoded table gives incorrect string value. PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xF0\x9D\x84\x8E i...' for column 'body_value' at row 1: INSERT INTO I have a character, in a string that mb_detect_encoding claims is UTF-8 encoded. I try to insert this string into a MyS...
PHP match string parent level URL string only. Ignore sub-directory paths
I'm looking to implement a simple conditional within a template file (Drupal 7) that checks the URL path and only operates on the condition when the string is exact/page is the parent. At the moment, I have a conditional working, but the condition still executes for sub pages of the URL string when matched. E.g: This still operates for anything hie...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.