Skip to content

Instantly share code, notes, and snippets.

Created October 18, 2011 20:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1296645 to your computer and use it in GitHub Desktop.
Save anonymous/1296645 to your computer and use it in GitHub Desktop.
Expose Slim urlFor to Twig
<?php
// Wrapper function to access urlFor.
function slim_url_for($name, $params = array()) {
return Slim::getInstance()->urlFor($name, $params);
}
// Setup Twig.
TwigView::$twigDirectory = '/path/to/twig/';
// Expose urlFor to Twig.
$view = new TwigView;
$twig = $view->getEnvironment();
$twig->addFunction('url', new Twig_Function_Function('slim_url_for'));
// Setup Slim.
$app = new Slim(array('view' => new TwigView));
// Create a named route.
$app->get('/article/(:id)', function($id) {
// Your logic here.
})->name('article_view');
/*
* In your templates you can now the following code to generate the URL:
* {{ url('article_view', { 'id': article.id }) }}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment