Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a url from symfony2 route name in a twig template?

Tags:

php

twig

symfony

I want to avoid hard-coding relative URLs in my twig templates in a Symfony2 project.

I have defined some route names in my controllers, for example:

@Route("/", name="homepage")

Is there a Twig or Symfony2 function that could help me generate links properly? Something like:

<a href='{{ magic_fct("homepage") }}'>Home</a>

would return:

<a href='/'>Home</a>
like image 817
Jérôme Verstrynge Avatar asked Sep 17 '25 12:09

Jérôme Verstrynge


2 Answers

Use following:

<a href='{{ path('homepage') }}'>Home page</a>
like image 55
Jasmin Mistry Avatar answered Sep 19 '25 03:09

Jasmin Mistry


Yes, of course you can. Take a look in Linking to Pages documentation.

<a href="{{ path('homepage') }}">Home</a>
like image 41
Federkun Avatar answered Sep 19 '25 01:09

Federkun