Slim Framework 和 Twig 模板引擎 [英] Slim Framework and Twig templating engine
问题描述
你好,我这里有一个简单的代码,它将使用纤薄的框架和树枝呈现 home.html
.代码如下:
Hello so I have a simple code here that will render home.html
using slim framework and twig. Here's the codes:
在我的 index.php 文件中:
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();
这是 base.html 模板:
Here is the base.html template:
还有我的 home.html:
And my home.html:
{% extends "base.html" %}
{% block content %}
Some content here
{% endblock %}
我的问题是,由于唯一呈现的部分是 home.html
,如果我想在我的基本模板中加载一些数据怎么办?像这样..
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.
另外,这个 是我在 Slim 中安装 Twig 时遵循的方法.
Also, this is what I followed to install twig in slim.
推荐答案
我认为最好的答案是这里.回答者说编写一个 Twig 自定义函数"来动态加载数据您的意见.
I think the best answer is given here. There the answerer say "write a Twig custom function" to load dynamically data in your views.
因此,您将能够编写自己的 PHP,可以注入您的数据库并将其与模板引擎一起使用.
So you would be able to write your own PHP, can inject your DB and use this along with that templating engine.
这篇关于Slim Framework 和 Twig 模板引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!