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 to 3.1.8 problem

 
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 -> Smarty 3
View previous topic :: View next topic  
Author Message
yuzaosong
Smarty Rookie


Joined: 30 Apr 2010
Posts: 6

PostPosted: Sat May 12, 2012 4:58 pm    Post subject: Upgrading to 3.1.8 problem Reply with quote

The standard of English is poor, he will be able to see to understand..


PHP:
Code:
<?php
require('../libs/Smarty.class.php');
$smarty = new Smarty;
function my_template_function(){
   global $page,$smarty,$db;
   
   // There may need to register some global variables, the metaphor of the recorded data page information
   $page = array(
      'total' => 20,
      'limit' => 30,
      'number' => 40,
      'current' => 5,
      // ....
   );
   $smarty->assign('page',$page); // Such use in versions prior to v3.1
   
   // Return may be a list of query to data, and do not want to be paged data added to the list of data returned with
   $list_data = $db->query("sql ...");
   return $list_data;

}
$smarty->display('index.tpl');
?>




tpl:
Code:

<!--
/// my_template_function() - This method is used in the template
-->
{$arr = my_template_function()}
{foreach $arr as $e}
  ...
{/foreach}



<!--
///  How to obtain my_template_function() method of variable ?
 -->
{print_r($page)}
Back to top
View user's profile Send private message Visit poster's website AIM Address MSN Messenger
yuzaosong
Smarty Rookie


Joined: 30 Apr 2010
Posts: 6

PostPosted: Sat May 12, 2012 5:05 pm    Post subject: Reply with quote

I can not be modified like this function, because the support great as my system reform:

PHP:
Code:

function my_template_function(){
   
   ...
   
   return array($page,$list_data);

}
Back to top
View user's profile Send private message Visit poster's website AIM Address MSN Messenger
U.Tews
Administrator


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

PostPosted: Sat May 12, 2012 6:00 pm    Post subject: Reply with quote

Because of some speed optimization in Smarty 3.1 you can not assign addition variables as you have done in your function.

A workaround is to create a template object and work on this.
Code:
<?php
require('../libs/Smarty.class.php');
$smarty = new Smarty;
function my_template_function(){
   global $page,$tpl,$db;
   
   // There may need to register some global variables, the metaphor of the recorded data page information
   $page = array(
      'total' => 20,
      'limit' => 30,
      'number' => 40,
      'current' => 5,
      // ....
   );
   $tpl->assign('page',$page); // assgin to template object
   
   // Return may be a list of query to data, and do not want to be paged data added to the list of data returned with
   $list_data = $db->query("sql ...");
   return $list_data;

}
$tpl = $smarty->createTemplate('index.tpl',$smarty);
$tpl->display();
?>
Back to top
View user's profile Send private message
yuzaosong
Smarty Rookie


Joined: 30 Apr 2010
Posts: 6

PostPosted: Sun May 13, 2012 1:56 am    Post subject: Reply with quote

U.Tews wrote:
Because of some speed optimization in Smarty 3.1 you can not assign addition variables as you have done in your function.

A workaround is to create a template object and work on this.
Code:
<?php
require('../libs/Smarty.class.php');
$smarty = new Smarty;
function my_template_function(){
   global $page,$tpl,$db;
   
   // There may need to register some global variables, the metaphor of the recorded data page information
   $page = array(
      'total' => 20,
      'limit' => 30,
      'number' => 40,
      'current' => 5,
      // ....
   );
   $tpl->assign('page',$page); // assgin to template object
   
   // Return may be a list of query to data, and do not want to be paged data added to the list of data returned with
   $list_data = $db->query("sql ...");
   return $list_data;

}
$tpl = $smarty->createTemplate('index.tpl',$smarty);
$tpl->display();
?>



Thank you very much...

I can be modified within the function? If you need to use external createTemplate() to me now system structure changes larger.

Code:
<?php
require('../libs/Smarty.class.php');

$smarty = new Smarty;

// Only be modified within the function, how to achieve it?
function my_template_function(){
   global $page,$tpl,$db,$smarty;
   
   $page = array(
      'total' => 20,
      'limit' => 30,
      'number' => 40,
      'current' => 5,
      // ....
   );
   
   $tpl = $smarty->createTemplate('index.tpl'); // ?
   $tpl->assign('page',$page);
   
   $list_data = array(1,2,3,4,5);
   return $list_data;

}


$smarty->display('index.tpl'); // Best not to change here, Involving many parts of the original HTML generation need to be modified.
Back to top
View user's profile Send private message Visit poster's website AIM Address MSN Messenger
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 -> Smarty 3 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