Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Slimphp-细枝动态包含_Php_Twig_Slim - Fatal编程技术网

Slimphp-细枝动态包含

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

我需要包括在树枝模板动态模板。因此,模板将包括routes.php中定义的页面

我尝试像下面的代码那样连接字符串和变量,但仍然不起作用

routes.php

$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)。“哎呀,我的错了。”。我没有意识到这一点。谢谢你救了我一天。