Suppose you have 3 pages, 3 routes (/index
,/about
,/contact
) and a shared header which displays a list of items retrieved from the database.
Is there a better way in Slim to retrieve these items for all pages/routes, to pass to the corresponding templates, as opposed to essentially replicating code for each route controller method?
e.g., any way other than this?
$app->get('/', function ($request, $response, $args) {
return $this->view->render($response, 'index.twig', [
'items' => /* retrieve items from database */
]);
});
$app->get('/about', function ($request, $response, $args) {
return $this->view->render($response, 'about.twig', [
'items' => /* retrieve items from database (duplicate code) */
]);
});
$app->get('/contact', function ($request, $response, $args) {
return $this->view->render($response, 'contact.twig', [
'items' => /* retrieve items from database (duplicate code) */
]);
});
Routes that share the same data could also use the same responder to render the content.
Pseudo example:
<?php
namespace App\Responder;
use Psr\Http\Message\ResponseInterface;
use Slim\Views\Twig;
final class Responder
{
private $twig;
public function __construct(Twig $twig)
{
$this->twig = $twig;
}
public function render(ResponseInterface $response, string $template, array $data = []): ResponseInterface
{
$shared = ['item1', 'item2'];
$data = array_replace(shared, $data);
return $this->twig->render($response, $template, $data);
}
}
Usage
$this->responder->render($response, 'index.twig', ['page' => 'content']);
or...
use App\Responder\Responder;
// ...
$app->get('/index', function($request, $response, $args){
return $this->get(Responder::class)->render($response, 'index.twig', ['pagecontent' => 'This is some content for /index only']);
});
$app->get('/about', function($request, $response, $args){
return $this->get(Responder::class)->render($response, 'about.twig', ['pagecontent' => 'You can have custom content for /about']);
});
$app->get('/contact', function($request, $response, $args){
return $this->get(Responder::class)->render($response, 'contact.twig', ['pagecontent' => '...and /contact as well']);
});
One option is to add these items as global variables to TwigEnvironment, so that they are available in every template.
First, you need to add global variables to Twig environment:
// A simple items provider which helps you generate a list of items
// You can change this to something that reads the items from database, etc.
class ItemsProvider {
public function getItems()
{
return ['item 1', 'item 2', 'item 3', 'item-4', ];
}
}
// Set view in Container
$container->set('view', function($c) {
$twig = Twig::create('<path-to-tiwg-templates'>);
// get items from items-provider and add them as global `items` variable
// to all twig templates
$twig->getEnvironment()->addGlobal('items', $c->get('items-provider')->getItems());
return $twig;
});
// set sample items, you can modifiy this
$container->set('items-provider', function($c){
return new ItemsProvider;
});
Now variableitems
is available to every Twig template without the need to pass it explicitly torender
method:
layout.twig:
These are some items available in every twig template:<br>
<ul>
{% for item in items%}
<li>{{ item }}</li>
{% endfor %}
</ul>
<br>
And this is some page specific content:<br>
{% block content %}
{{ pagecontent }}
{% endblock %}
All three templates index.twig, about.twig and contact.twig can extend layout.twig:
{% extends 'layout.twig' %}
Route definitions with different value per route for same variable used in layout.twig:
$app->get('/index', function($request, $response, $args){
return $this->get('view')->render($response, 'index.twig', ['pagecontent' => 'This is some content for /index only']);
});
$app->get('/about', function($request, $response, $args){
return $this->get('view')->render($response, 'about.twig', ['pagecontent' => 'You can have custom content for /about']);
});
$app->get('/contact', function($request, $response, $args){
return $this->get('view')->render($response, 'contact.twig', ['pagecontent' => '...and /contact as well']);
});
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Slim is, as its name suggests, a micro-framework that is compact and fast. The reason for its such characteristics lies in the fact that it is completely independent of third-party code. It was created in 2010, at the moment its most recent version is 4.5.0.
https://www.slimframework.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.