How to make preg_replace in a branch

I'm currently trying to make a variable using the current url for the view. To get the url, I use {% set uri = app.request.uri%} (uri is my variable, which is the current URL for this particular view). The thing is that I’m only interested in what makes the URL unique (its end is a unique object from an array — it happens to be uri), and not the beginning (the path to my application). I thought I could use preg_replace, but TWIG does not have this function. Just wondering, can anyone find out how to accomplish what I'm trying to do?

I am new to Symfony (and quite new to PHP), so my explanations may not be clear (sorry).

Ref.

{% set uri = app.request.uri %} 

output: http://website.com/http://item.org/1

I want to change the uri variable to ONLY have http://item.org/1 (and not the path to my site).

I think creating a Twig Extension with preg_replace will allow me to do this .. but not sure if this is the best way to go (inexperienced).


General purpose: The unique value for "uri" in the view is added to the website path by another view from an array of objects ($ results) with attributes, one of which is "uri". My ultimate goal is to show only all related attributes (or string) for the object in my $ results array. I thought I could do this by first creating a key (my uri variable) in foreach and returning a string in the array that matches that key. This is why I am trying to create a variable with a URL so that I can use it as a key for my foreach loop to iterate over $ results. I DO NOT use a database or Doctrine.

Thank you for your help!

+4
source share
2 answers

The best way is to move the logic from the template to the controller.

If you need preg_replace in twig, you must create a custom extension .

+4
source

Someone seems to have created the Twig extension for preg_replace, see https://github.com/victor-in/Craft-TwigPCRE

0
source

Source: https://habr.com/ru/post/1490736/


All Articles