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

Upgrading from 2 -> 3 RC1, compiler plugin change questio

 
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 -> Plugins
View previous topic :: View next topic  
Author Message
obeyrobots
Smarty n00b


Joined: 24 Aug 2011
Posts: 3

PostPosted: Wed Aug 24, 2011 10:16 pm    Post subject: Upgrading from 2 -> 3 RC1, compiler plugin change questio Reply with quote

Hi,

I'm finally upgrading from 2 to the latest RC of 3. I'm beyond thrilled that you can access static variables in Smarty 3, which in Smarty 2 I had to use compiler plugins to retrieve.

I've got a fetch plugin that acts strangely in 3 and I'm not sure what changed. Basically I don't want to hard-code my field names in the HTML and instead declare them as static variables in the PHP pages themselves. This fetch plugin accesses the value of the field if it's set by PHP (in case a user has a validation error, for example).

What confuses me is that this plugin works fine in Smarty 2, it assigns the value which I then can access by {$assigned_name} in the template. In Smarty 3 it's throwing the resulting "$this->assign( 'foo', $bar );" in the HTML itself rather than assigning it to the Smarty object. I verified that I'm calling registerPlugin with 'compiler' as the first argument.

What changed between versions here?

Thanks!

Code:

<?php
function smarty_compiler_static_fetch( $params, &$compiler )
{
  # class, var required
  if( !isset( $params[ 'class' ] ) )
  {
    $compiler->_syntax_error( "static: missing 'class' parameter.", E_USER_ERROR );
  }
 
  if( !isset( $params[ 'var' ] ) )
  {
    $compiler->_syntax_error( "static: missing 'var' parameter.", E_USER_ERROR );
  }

  $class = str_replace( array( "'", '"' ), '', $params[ 'class' ] );
  $var   = str_replace( array( "'", '"' ), '', $params[ 'var' ] );

  $result  = sprintf( '$fetch_key = %s::$%s;', $class, $var ) . "\n";
  $result .= '$fetch_value = $this->_tpl_vars[ $fetch_key ];' . "\n";

  if( isset( $params[ 'assign' ] ) )
  {
    $result .= sprintf( '$this->assign( %s, $fetch_value );', $params[ 'assign' ] );
  }
  else
  {
    $result .= 'echo $fetch_value';
  }

  return $result;
}
?>
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Aug 25, 2011 6:48 pm    Post subject: Reply with quote

Smarty 3 does require <?php ... ?> tags included in the returned result.
Back to top
View user's profile Send private message
obeyrobots
Smarty n00b


Joined: 24 Aug 2011
Posts: 3

PostPosted: Thu Aug 25, 2011 9:34 pm    Post subject: Reply with quote

Thanks! I was dubious it would be that easy. Smile
Back to top
View user's profile Send private message
obeyrobots
Smarty n00b


Joined: 24 Aug 2011
Posts: 3

PostPosted: Thu Aug 25, 2011 11:44 pm    Post subject: Reply with quote

Just a note for other people upgrading compiler plugins from 2 to 3:

Within the compiler plugin $this used to give you the Smarty object.

What once was:
Code:

$bar = $this->_tpl_vars[ 'foo' ];


...is now:
Code:

$bar = $_smarty_tpl->smarty->getTemplateVars( 'foo' );


Hope this helps!
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 -> Plugins 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