当尝试烘烤视图时,我总是因为语法错误而停止,如下所示:
Parse error: syntax error, unexpected '$this' (T_VARIABLE), expecting identifier (T_STRING) in /vagrant/lib/Cake/Console/Templates/default/views/index.ctp on line 27
导致错误的代码上下文对我来说似乎没有问题。
<?
foreach ($fields as $field):
if (!in_array($field, array('created', 'body', 'description', 'position', 'slug'))) {
echo "'t't<th><?php echo $this->Paginator->sort('{$field}'); ?></th>'n";
}
endforeach;
?>
它似乎有问题,因为它试图解释它应该在字符串中生成的代码,因为它应该运行,因此认为字符串'$this->Paginator->sort(…)'是调用函数的对象变量,等等。
我在Ubuntu Trusty x64上运行PHP 5.3.37-1。
你真的不应该修改内核,实际上你根本不应该修改任何供应商的文件!如果您需要自定义烘焙输出,那么按照文档中描述的正确方式进行:
Cookbook> shell, Tasks &控制台工具>使用Bake生成代码>修改由" Bake "模板生成的默认HTML
话虽如此,您使用双引号,因此$
具有特殊含义,要实际响应$
,您必须使用'
进行转义,即像这样:
echo "'t't<th><?php echo '$this->Paginator->sort('{$field}'); ?></th>'n";
这也是在中使用的原始烘焙模板。另外,正如在另一个答案中提到的,不要使用短的打开标签!
请勿使用短标签:<?
使用<?php
打开PHP脚本,并在退出PHP时使用<?=
回显。
<?php
foreach ($fields as $field) {
if (!in_array($field, array('created', 'body', 'description', 'position', 'slug'))) {
echo "'t't<th>" . $this->Paginator->sort($field) . "</th>'n";
}
}
?>