|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
yuzaosong Smarty Rookie
Joined: 30 Apr 2010 Posts: 6
|
Posted: Sat May 12, 2012 4:58 pm Post subject: Upgrading to 3.1.8 problem |
|
|
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 |
|
yuzaosong Smarty Rookie
Joined: 30 Apr 2010 Posts: 6
|
Posted: Sat May 12, 2012 5:05 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sat May 12, 2012 6:00 pm Post subject: |
|
|
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 |
|
yuzaosong Smarty Rookie
Joined: 30 Apr 2010 Posts: 6
|
Posted: Sun May 13, 2012 1:56 am Post subject: |
|
|
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 |
|
|
|
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
|
|