Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

PHP Parse error: unexpected ':'

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
Fahr
Smarty Rookie


Joined: 26 Feb 2007
Posts: 13

PostPosted: Mon Feb 26, 2007 10:43 pm    Post subject: PHP Parse error: unexpected ':' Reply with quote

Please see next post. For some reason I had to have 1 post first before I can post the actual thing, since it supposedly contains links :\
Back to top
View user's profile Send private message
Fahr
Smarty Rookie


Joined: 26 Feb 2007
Posts: 13

PostPosted: Mon Feb 26, 2007 10:43 pm    Post subject: Reply with quote

Warning: very ugly code bits ahead Razz

Hello Smarty team,

I recently ran into a weird error in a compiled Smarty template. All templates work fine except for this particular one. It causes a PHP Parse error at an unexpected :.

The block that causes the error is the following;

Code:
{foreach from=$explexps item=explexp name=explexps}
  <tr>
    <td>{if $smarty.foreach.explexps.first}{t var=explexp}{/if}</td>
    <td>{$explexp.name}</td>
    <td>{if $explexp.item > 0}{t var=explosives index=$explexp.item}{else}{t var=none}{/if}</td>
    <td>{if $meleader}<a href="{$ozone.MODULE_LINK}kick&id={$explexp.userid}">{t var=kick}</a>{/if}</td>
  </tr>
{/foreach}


The {t ...} function is one I added myself and works fine in all other templates. This particular block causes the error on this line;

Code:
    <td>{if $explexp.item > 0}{t var=explosives index=$explexp.item}{else}{t var=none}{/if}</td>


The line gets compiled to a construction with the alternative if statement;

Code:
$_from = $this->_tpl_vars['explexps']; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array'); }$this->_foreach['explexps'] = array('total' => count($_from), 'iteration' => 0);
if ($this->_foreach['explexps']['total'] > 0):
    foreach ($_from as $this->_tpl_vars['explexp']):
        $this->_foreach['explexps']['iteration']++;
?>
  <tr>
    <td><?php if (($this->_foreach['explexps']['iteration'] <= 1)):  if ($this->caching && !$this->_cache_including) { echo '{nocache:33b32f62fdaf4880689a632d0a682830#9}';}echo $this->_plugins['function']['t'][0][0]->function_translate(array('var' => 'explexp'), $this);if ($this->caching && !$this->_cache_including) { echo '{/nocache:33b32f62fdaf4880689a632d0a682830#9}';} endif; ?></td>
    <td><?php echo $this->_tpl_vars['explexp']['name']; ?>
</td>
    <td><?php if ($this->_tpl_vars['explexp']['item'] > 0):  if ($this->caching && !$this->_cache_including) { echo '{nocache:33b32f62fdaf4880689a632d0a682830#10}';}echo $this->_plugins['function']['t'][0][0]->function_translate(array('var' => 'explosives','index' => $this->_tpl_vars['explexp']['item']), $this);if ($this->caching && !$this->_cache_including) { echo '{/nocache:33b32f62fdaf4880689a632d0a682830#10}';} else:  if ($this->caching && !$this->_cache_including) { echo '{nocache:33b32f62fdaf4880689a632d0a682830#11}';}echo $this->_plugins['function']['t'][0][0]->function_translate(array('var' => 'none'), $this);if ($this->caching && !$this->_cache_including) { echo '{/nocache:33b32f62fdaf4880689a632d0a682830#11}';} endif; ?></td>
    <td><?php if ($this->_tpl_vars['meleader']): ?><a href="<?php echo $this->_tpl_vars['ozone']['MODULE_LINK']; ?>
kick&id=<?php echo $this->_tpl_vars['explexp']['userid']; ?>
"><?php if ($this->caching && !$this->_cache_including) { echo '{nocache:33b32f62fdaf4880689a632d0a682830#12}';}echo $this->_plugins['function']['t'][0][0]->function_translate(array('var' => 'kick'), $this);if ($this->caching && !$this->_cache_including) { echo '{/nocache:33b32f62fdaf4880689a632d0a682830#12}';}?>
</a><?php endif; ?></td>
  </tr>
<?php endforeach; endif; unset($_from); ?>


and the : after the first if causes PHP to explode. When I edited the compiled template manually and changed the alternative if statement into one with brackets, the template worked fine:

Code:
$_from = $this->_tpl_vars['explexps'];
if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array'); }
$this->_foreach['explexps'] = array('total' => count($_from), 'iteration' => 0);
if ($this->_foreach['explexps']['total'] > 0)
{
  foreach ($_from as $this->_tpl_vars['explexp'])
  {
    $this->_foreach['explexps']['iteration']++;
?>
  <tr>
    <td>
<?php
    if (($this->_foreach['explexps']['iteration'] <= 1))
    {
      if ($this->caching && !$this->_cache_including) { echo '{nocache:15a3c0d0aafccc8aee491498da6f30ff#9}';}
      echo $this->_plugins['function']['t'][0][0]->function_translate(array('var' => 'explexp'), $this);
      if ($this->caching && !$this->_cache_including) { echo '{/nocache:15a3c0d0aafccc8aee491498da6f30ff#9}';}
    }
    ?></td>
    <td><?php echo $this->_tpl_vars['explexp']['name']; ?>
</td>
    <td>

<?php
    if ($this->_tpl_vars['explexp']['item'] > 0)
    {
      if ($this->caching && !$this->_cache_including) { echo '{nocache:15a3c0d0aafccc8aee491498da6f30ff#10}';}
      echo $this->_plugins['function']['t'][0][0]->function_translate(array('var' => 'explosives','index' => $this->_tpl_vars['explexp']['item']), $this);
      if ($this->caching && !$this->_cache_including) { echo '{/nocache:15a3c0d0aafccc8aee491498da6f30ff#10}';}
    }
    else
    {
      if ($this->caching && !$this->_cache_including) { echo '{nocache:15a3c0d0aafccc8aee491498da6f30ff#11}';}
      echo $this->_plugins['function']['t'][0][0]->function_translate(array('var' => 'none'), $this);
      if ($this->caching && !$this->_cache_including) { echo '{/nocache:15a3c0d0aafccc8aee491498da6f30ff#11}';}
    }
    ?></td>
    <td><?php if ($this->_tpl_vars['meleader']): ?><a href="<?php echo $this->_tpl_vars['ozone']['MODULE_LINK']; ?>
kick&id=<?php echo $this->_tpl_vars['explexp']['userid']; ?>
"><?php if ($this->caching && !$this->_cache_including) { echo '{nocache:15a3c0d0aafccc8aee491498da6f30ff#12}';}echo $this->_plugins['function']['t'][0][0]->function_translate(array('var' => 'kick'), $this);if ($this->caching && !$this->_cache_including) { echo '{/nocache:15a3c0d0aafccc8aee491498da6f30ff#12}';}?>
</a><?php endif; ?></td>
  </tr>
<?php
  }
}

unset($_from); ?>


I'm not sure if this is a Smarty bug or a PHP bug. I'm hoping someone here can shed some light on it.

Regards,
- Fahr
Back to top
View user's profile Send private message
danilo
Smarty Rookie


Joined: 07 Feb 2007
Posts: 33

PostPosted: Tue Feb 27, 2007 9:54 am    Post subject: Reply with quote

Thanks for reporting this issue!

Its partially a PHP problem:
Code:
if (true) :
    if (true) {}
else:
    if (true) {}
endif;

Results in an error because mixing up syntax in php is bad Twisted Evil .

Its partially a Smarty problem:
Because Smarty mixes them up it doesnt work. _push_cacheable_state(), _pop_cacheable_state() use normal syntax. The rest uses alternate syntax.

Here is a patch to fix the situation:

Code:
Index: Smarty_Compiler.class.php
===================================================================
RCS file: /repository/smarty/libs/Smarty_Compiler.class.php,v
retrieving revision 1.392
diff -r1.392 Smarty_Compiler.class.php
2241c2242
<         $_ret = 'if ($this->caching && !$this->_cache_including) { echo \'{nocache:'
---
>         $_ret = 'if ($this->caching && !$this->_cache_including): echo \'{nocache:'
2243c2244
<             . '}\'; };';
---
>             . '}\'; endif;';
2258c2259
<         return 'if ($this->caching && !$this->_cache_including) { echo \'{/nocache:'
---
>         return 'if ($this->caching && !$this->_cache_including): echo \'{/nocache:'
2260c2261
<             . '}\'; };';
---
>             . '}\'; endif;';


Could you try this out if it works for you?
Back to top
View user's profile Send private message
Fahr
Smarty Rookie


Joined: 26 Feb 2007
Posts: 13

PostPosted: Tue Feb 27, 2007 10:56 am    Post subject: Reply with quote

The patch works Smile thanks a lot. Will this be fixed in the Smarty distribution too?
Back to top
View user's profile Send private message
danilo
Smarty Rookie


Joined: 07 Feb 2007
Posts: 33

PostPosted: Tue Feb 27, 2007 10:59 am    Post subject: Reply with quote

I think so. I will ask monte/boots/messju to have a quick look at it.
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Feb 27, 2007 11:27 am    Post subject: Reply with quote

I cannot reproduce this problem. (PHP 5.1.6 (cli))
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Fahr
Smarty Rookie


Joined: 26 Feb 2007
Posts: 13

PostPosted: Tue Feb 27, 2007 11:36 am    Post subject: Reply with quote

Running PHP 5.2.1-0.dotdeb.1 with Suhosin-Patch 0.9.6.2 (cli), here is a test that fails;

Code:
<?php

$i = 1;
if($i == 1):
  if(true) { echo 'x'; }
else:
  if(false) { echo 'y'; }
endif;

?>


running php -f on this gives me;

Parse error: syntax error, unexpected ':' in /home/fahr/fail.php on line 6
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Feb 27, 2007 11:43 am    Post subject: Reply with quote

I see. Thanks for the example.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
danilo
Smarty Rookie


Joined: 07 Feb 2007
Posts: 33

PostPosted: Tue Feb 27, 2007 11:44 am    Post subject: Reply with quote

Code:
$ php -v
PHP 5.2.0 (cli) (built: Nov  8 2006 20:09:27)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies

$ php -r "if(true): if(true){} else: if(true){} endif;"

Parse error: syntax error, unexpected ':' in Command line code on line 1
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Feb 27, 2007 6:07 pm    Post subject: Reply with quote

Good stuff all around Smile
Back to top
View user's profile Send private message
danilo
Smarty Rookie


Joined: 07 Feb 2007
Posts: 33

PostPosted: Tue Feb 27, 2007 6:38 pm    Post subject: Reply with quote

Fahr wrote:
Will this be fixed in the Smarty distribution too?


I commited it to CVS HEAD. It will most likely be included into 2.6.17, when that comes out.
Back to top
View user's profile Send private message
Fahr
Smarty Rookie


Joined: 26 Feb 2007
Posts: 13

PostPosted: Tue Feb 27, 2007 7:34 pm    Post subject: Reply with quote

Thanks for that and thanks for all the feedback Smile
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP