php - 如何访问服务变量?
问题描述
public function pages($slug, PagesGenerator $pagesGenerator)
{
$output = $pagesGenerator->getPages($slug);
$id = $page->getId();
return $this->render('list.html.twig', [ 'output' => $output, 'id' => $id]);
}
但我得到了错误:
注意:未定义变量:page
解决方案
您的新控制器显然没有template
变量到变量列表中,只是output
. 看起来您的正确代码应该是:
public function pages($slug, PagesGenerator $pagesGenerator)
{
$output = $pagesGenerator->getPages($slug);
return $this->render('list.html.twig', $output);
}
更新:更新问题的答案基本相同,您需要做的就是将模板变量作为普通数组传递
return $this->render('list.html.twig', array_merge($output, ['table' => $table]));
或根据模板中的实际结构使用它们,例如,{{ output.page }}
而不是{{ page }}
推荐阅读
- linux - 无法启动在 Linux 机器上运行的 Jenkins 控制台
- c# - 如何调用程序类中的方法?
- pandas - 如何以百分比获得两个分类变量之间的关系?
- c# - C#检测发送到泛型类的对象并开始处理
- kubernetes - Kubernetes helm chart 一个命名空间包含多个入口文件
- android - 如何在 TextInputLayout 中更改 startIconDrawable 大小
- python-3.x - 在按下 Enter 之前,子进程 popen 不会退出
- php - 如何使用 Ajax 使用类获取下拉名称
- http - 为什么“不支持 505 HTTP 版本”被归类为服务器错误?
- mongodb - Multiple Group By with condition in MongoDB