Slimphp-细枝动态包含
我需要包括在树枝模板动态模板。因此,模板将包括routes.php中定义的页面 我尝试像下面的代码那样连接字符串和变量,但仍然不起作用 routes.php:Slimphp-细枝动态包含,php,twig,slim,Php,Twig,Slim,我需要包括在树枝模板动态模板。因此,模板将包括routes.php中定义的页面 我尝试像下面的代码那样连接字符串和变量,但仍然不起作用 routes.php: $app->get('/home', function ($request, $response, $args) { $data['page'] = "home"; return $this->view->render($response, 'Home/layout.html', $data); }); Hom
$app->get('/home', function ($request, $response, $args) {
$data['page'] = "home";
return $this->view->render($response, 'Home/layout.html', $data);
});
Home/layout.html:
{% include 'Home/_header.html' %}
{% include 'Home/_topbar.html' %}
{% include 'Home/_sidebar.html' %}
{% include 'Home/' ~ data.page ~ '.html' %}
{% include 'Home/_footer.html' %}
Unable to find template "Home/.html" (looked into: ../App/Templates) in "Home/layout.html" at line 4.
$app->get('/home', function ($request, $response, $args) {
$data['page'] = "{% include 'Home/home.html' %}";
return $this->view->render($response, 'Home/layout.html', $data);
});
{% include 'Home/_header.html' %}
{% include 'Home/_topbar.html' %}
{% include 'Home/_sidebar.html' %}
{% include template_from_string(page) %}
{% include 'Home/_footer.html' %}
错误消息:
{% include 'Home/_header.html' %}
{% include 'Home/_topbar.html' %}
{% include 'Home/_sidebar.html' %}
{% include 'Home/' ~ data.page ~ '.html' %}
{% include 'Home/_footer.html' %}
Unable to find template "Home/.html" (looked into: ../App/Templates) in "Home/layout.html" at line 4.
$app->get('/home', function ($request, $response, $args) {
$data['page'] = "{% include 'Home/home.html' %}";
return $this->view->render($response, 'Home/layout.html', $data);
});
{% include 'Home/_header.html' %}
{% include 'Home/_topbar.html' %}
{% include 'Home/_sidebar.html' %}
{% include template_from_string(page) %}
{% include 'Home/_footer.html' %}
我找到了自己的解决方案,但不是我想要的。所以我编辑routes.php和layout.html如下:
routes.php:
{% include 'Home/_header.html' %}
{% include 'Home/_topbar.html' %}
{% include 'Home/_sidebar.html' %}
{% include 'Home/' ~ data.page ~ '.html' %}
{% include 'Home/_footer.html' %}
Unable to find template "Home/.html" (looked into: ../App/Templates) in "Home/layout.html" at line 4.
$app->get('/home', function ($request, $response, $args) {
$data['page'] = "{% include 'Home/home.html' %}";
return $this->view->render($response, 'Home/layout.html', $data);
});
{% include 'Home/_header.html' %}
{% include 'Home/_topbar.html' %}
{% include 'Home/_sidebar.html' %}
{% include template_from_string(page) %}
{% include 'Home/_footer.html' %}
Home/layout.html:
{% include 'Home/_header.html' %}
{% include 'Home/_topbar.html' %}
{% include 'Home/_sidebar.html' %}
{% include 'Home/' ~ data.page ~ '.html' %}
{% include 'Home/_footer.html' %}
Unable to find template "Home/.html" (looked into: ../App/Templates) in "Home/layout.html" at line 4.
$app->get('/home', function ($request, $response, $args) {
$data['page'] = "{% include 'Home/home.html' %}";
return $this->view->render($response, 'Home/layout.html', $data);
});
{% include 'Home/_header.html' %}
{% include 'Home/_topbar.html' %}
{% include 'Home/_sidebar.html' %}
{% include template_from_string(page) %}
{% include 'Home/_footer.html' %}
我想发送的变量包含routes.php中模板文件的名称,而不是模板语法。您访问的变量错误。您正在将以下内容传递给
细枝
$data['page'] = "home";
return $this->view->render($response, 'Home/layout.html', $data);
这意味着要访问page
变量在twig
中,您只需要调用page
而不是数据。page
因为数组数据
甚至没有传递到模板
这意味着您的模板应该如下所示
{% include 'Home/_header.html' %}
{% include 'Home/_topbar.html' %}
{% include 'Home/_sidebar.html' %}
{% include 'Home/' ~ page ~ '.html' %}
{% include 'Home/_footer.html' %}
您可以在查看错误消息时自己判断这一点
找不到模板“Home/.html”
由于Home/
和.html
之间没有任何内容,这意味着变量data.page
不存在。我建议在开发时启用调试模式,然后您在尝试访问未定义变量时会出现错误您会给出哪个错误?@Matteo它在第4行的“Home/layout.html”中给了我错误消息“找不到模板”Home/.html(查看:../App/Templates)。“哎呀,我的错了。”。我没有意识到这一点。谢谢你救了我一天。