简体   繁体   中英

Slim Framework and Twig templating engine

Hello so I have a simple code here that will render home.html using slim framework and twig. Here's the codes:

In my index.php file:

require_once 'vendor/autoload.php';

$app = new \Slim\Slim([
        'debug' => true,
        'templates.path' => 'app/views'
    ]);

$app->view = new \Slim\Views\Twig();
$app->view->setTemplatesDirectory("app/views");

$view = $app->view();
$view->parserOptions = ['debug' => true];
$view->parserExtensions = [new \Slim\Views\TwigExtension()];

$app->get('/home', function () use ($app) {
    $app->render('home.html');
});

$app->run();

Here is the base.html template: 在此处输入图片说明

And my home.html:

{% extends "base.html" %}
{% block content %}
 Some content here
{% endblock %}

My question is, since the only rendered part is the home.html , what if I want some data loaded in my base template? Like this.. 在此处输入图片说明

So that I won't have to repeat it on every page I render. Is that possible on a base template? Thank you in advance.

Also, this is what I followed to install twig in slim.

I think the best answer is given here . There the answerer say "write a Twig custom function " to load dynamically data in your views.

So you would be able to write your own PHP, can inject your DB and use this along with that templating engine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM