When attempting to bake views, I consistently get stopped with syntax errors as follows:

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

The context for the code causing the error here doesn't seem problematic to me.

<?
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;
?>

It seems to have problems because it is trying to interpret the code it should generate in the string as code it should run, and thus thinks the string '$this->Paginator->sort(...)' is an object variable calling a function, and so on.

I am running PHP 5.3.37-1 on Ubuntu Trusty x64 on a Vagrant box.

2

There are 2 answers

0
ndm On

You really shouldn't modify the core, actually you souldn't modify any vendor files at all! If you need custom bake output, then do it the proper way as described in the docs:

Cookbook > Shells, Tasks & Console Tools > Code Generation with Bake > Modify default HTML produced by “baked” templates

That being said, you are using double quotes, hence $ has a special meaning, to actually echo a $ you have to escape it using \, ie like this:

echo "\t\t<th><?php echo \$this->Paginator->sort('{$field}'); ?></th>\n";

Which is also what is used in the original bake template. Also, as mentioned in another answer, do not use short open tags!

2
ficuscr On

Don't use short tags: <?

Use <?php to open a PHP script and use <?= to echo when out of PHP.

As others are saying it is a simple syntax error. If you have trouble seeing syntax errors, or understanding what your logs are telling you, you may want to consider using an IDE that would help highlight these mistakes.

<?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";
    }
}
?>