php - Access url in Twig template
Get the solution ↓↓↓This is my first real PHP/Slim/Twig-project, so please excuse me, if this is a silly question.
In my Twig template, I want to set a css class depending on the url:
/personal/... --> <div class="personal">...
/business/... --> <div class="business">...
However, I can't access the url from within the template. I found some answers that suggest to use:
{{ app.request.uri }}
Unfortunately, this results in an empty string. I'm not sure if I have access to my app object at all. This is how I setup my container (Slim Framework 3):
$container['view'] = function ($container) {
$view = new Twig('../templates/', ['cache' => false]);
$view->addExtension( new TwigExtension($container['router'], $container['request']->getUri() ) );
return $view;
};
And this is how I call the template:
public function home ($request, $response, $args)
{
$response = $this->view->render($response, "home.twig");
return $response;
}
Any help is greatly appreciated! I can, of course, provide more information.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: installation failed, reverting ./composer.json and ./composer.lock to their original content.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.