Skip to content

Instantly share code, notes, and snippets.

@serapheem
Created November 6, 2012 10:29
Show Gist options
  • Save serapheem/4023912 to your computer and use it in GitHub Desktop.
Save serapheem/4023912 to your computer and use it in GitHub Desktop.
Symfony2 - Generate links in template
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