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.orgYou can get the scheme of a valid url
{{ 'https://www.example.org/test'|url_scheme }}Output:
httpsYou can get the user of a valid url
{{ 'ftp://admin:hidden@example.org'|url_user }}Output:
adminYou can get the password of a valid url
{{ 'ftp://admin:hidden@example.org'|url_pass }}Output:
hiddenYou can get the host of a valid url
{{ 'example.org:8080'|url_host }}Output:
example.orgYou can get the port of a valid url
{{ 'example.org:8080'|url_port }}Output:
8080You 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
helloYou can get the query of a valid url
{{ 'http://example.org/hello?arg=test&x=y'|url_query }}Output:
arg=test&x=yYou can get the fragment of a valid url
{{ 'http://example.org/hello#test'|url_fragment }}Output:
test