consider my current project url is http://localhost/myproject/ i want to get this url in every template in php-view how to get it
You can set template variables in the PhpRenderer
instance, either when constructing the instance of afterwards using setAttributes
and addAttribute
.
For example using the constructor:
$app = new App([
// set the baseUrl variable at construction time
'renderer' => new PhpRenderer('./templates', [
'baseUrl' => '/myproject/'
])
]);
The variable baseUrl
can then be used in the templates rendered with the renderer, e.g.:
<p>Back to the <a href="<?php print htmlspecialchars($baseUrl); ?>">homepage</a>.</p>
First you need to add this extension to your twig container:
// Twig
$container['view'] = function ( $c ) {
$view = new \Slim\Views\Twig( $c['settings']['view']['template_path'], $c['settings']['view']['twig'] );
// extension for path_for and base_url
$view->addExtension( new Slim\Views\TwigExtension( $c['router'], $c['request']->getUri() ) );
return $view;
};
Then you can use it in your views like this:
<link rel="shortcut icon" href="{{ base_url() }}/favicon.ico" type='image/x-icon'>