Created
October 18, 2011 20:38
-
-
Save anonymous/1296645 to your computer and use it in GitHub Desktop.
Expose Slim urlFor to Twig
This file contains hidden or 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
<?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