Slim Framework and Twig templating engine

纵然是瞬间 提交于 2019-12-13 02:13:08

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/32108084/slim-framework-and-twig-templating-engine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!