Created
November 6, 2012 10:29
-
-
Save serapheem/4023912 to your computer and use it in GitHub Desktop.
Symfony2 - Generate links in template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Twig: | |
<a href="{{ path('_welcome') }}">Home</a> | |
PHP: | |
<a href="<?php echo $view['router']->generate('_welcome') ?>">Home</a> | |
If more complicated route. | |
Twig: | |
{# src/Acme/ArticleBundle/Resources/views/Article/recentList.html.twig #} | |
{% for article in articles %} | |
<a href="{{ path('article_show', {'slug': article.slug}) }}"> | |
{{ article.title }} | |
</a> | |
{% endfor %} | |
PHP: | |
<!-- src/Acme/ArticleBundle/Resources/views/Article/recentList.html.php --> | |
<?php foreach ($articles in $article): ?> | |
<a href="<?php echo $view['router']->generate('article_show', array('slug' => $article->getSlug()) ?>"> | |
<?php echo $article->getTitle() ?> | |
</a> | |
<?php endforeach; ?> | |
To generate absolute URL: | |
Twig: | |
<a href="{{ url('_welcome') }}">Home</a> | |
PHP: | |
<a href="<?php echo $view['router']->generate('_welcome', array(), true) ?>">Home</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment