-
-
Save LittleCoding/482bbcffbaa2a1cfaa53c6bcf35273ba to your computer and use it in GitHub Desktop.
{# | |
/** | |
* @file | |
* Get the current URL path via Drupal 8 route name and twig function. | |
*/ | |
#} | |
<a href="{{ url('<current>') }}">Current page</a> |
Note that this returns the complete URL, not the path. If you just want the path, use
{{ path('<current>') }}
This works great, but what if I only need a specific parameter from the URL? For example, I want to style a portion of the Commerce checkout process if the user is on the Review page (mysite.com/checkout/388/review). How can I get just that final parameter (review)? When I use this code (
If I can get just "review" as that part of the class, I will be able to use CSS to style that area of the order review page.
Ah, I believe I have found the solution:
{% set url = url('')['#markup'] %}{{ url|split('/')|last }}
Ah, I believe I have found the solution:
{% set url = url('')['#markup'] %}{{ url|split('/')|last }}
Thanks, it works perfectly.
Nice, thanks for sharing!
Thanks, works well! For more URL path Twig tips in Drupal, see Functions in Twig templates.
If you use this in a block view, remember to turn off block caching in the "Advanced Settings"