The url twig extension gives you a simple and efficient way to get specific parts of urls.
The twig extension need to be registered as symfony service.
services:
Sulu\Twig\Extensions\UrlExtension: ~
If autoconfigure is not active you need to tag it with twig.extension.
You can get a specific format of any valid url
{{ 'https://www.example.org/test'|url_format({
scheme: false,
path: false,
}) }}
Output:
www.example.org
You can get the scheme of a valid url
{{ 'https://www.example.org/test'|url_scheme }}
Output:
https
You can get the user of a valid url
{{ 'ftp://admin:[email protected]'|url_user }}
Output:
admin
You can get the password of a valid url
{{ 'ftp://admin:[email protected]'|url_pass }}
Output:
hidden
You can get the host of a valid url
{{ 'example.org:8080'|url_host }}
Output:
example.org
You can get the port of a valid url
{{ 'example.org:8080'|url_port }}
Output:
8080
You can get the path of a valid url
{{ 'http://example.org/hello'|url_path }}
{{ 'example.org/hello'|url_path }}
{{ 'example.org:8080/hello'|url_path }}
Output:
hello
example.org/hello
hello
You can get the query of a valid url
{{ 'http://example.org/hello?arg=test&x=y'|url_query }}
Output:
arg=test&x=y
You can get the fragment of a valid url
{{ 'http://example.org/hello#test'|url_fragment }}
Output:
test