|
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 |
sincerity77 Smarty n00b
Joined: 15 Apr 2005 Posts: 1
|
Posted: Fri Apr 15, 2005 3:16 am Post subject: Parameterized includes |
|
|
I often wanted to call a sub-template from within a template as if it were a function and pass in my own parameters which are then used as vars within the sub-template. ie: parameterized includes. Why? Because sometimes using global variables everywhere can get a bit messy! Anyway, this evening I wrote a plugin to do just that. I wondered if anyone else thinks this is a good idea?
- sincerity77
PS Here is the src:
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: call_template
* Author: Ross Balmer <ross@i2dm.co.uk>
* Purpose: Calls a sub-template as if it were a function.
* Supplied parameters appear as Smarty vars inside
* the template. A kind of rough-and-ready scoping
* mechanism.
* Input: template = filename of sub-template
* template_dir (opt) = directory of sub-template
* global (opt) = global vars to pass into template
* (otherwise, global vars do not appear inside the
* sub-template, except for SCRIPT_PATH)
* [user-defined] (opt) = values to be passed in as
* arguments
* -------------------------------------------------------------
*/
function smarty_function_call_template($params, &$smarty)
{
// default values
$template = null;
$template_dir = null;
$globals = array('SCRIPT_NAME');
foreach ($params as $_key=>$_value) {
switch ($_key) {
case 'template':
case 'template_dir':
$$_key = $_value;
break;
case 'global':
$globals = array_merge($globals, explode(',', $_value));
default:
$temp_params[$_key] = $_value;
break;
}
}
// if no template dir supplied, use standard smarty template dir
if ($template_dir==null) {
$template_dir = $smarty->template_dir;
}
if (isset($template)) {
// store values of existing smarty vars
// and nullify non-global variables
$smarty_vars = $smarty->get_template_vars();
foreach ($smarty_vars as $_key => $_val) {
if (!in_array($_key,$globals)) {
$smarty->assign($_key,null);
}
}
// assign params to smarty
// typically: _label, _name, _value, _error
foreach ($params as $_name =>$_value) {
$smarty->assign("$_name", $_value);
}
// switch template dirs
$smarty_template_dir = $smarty->template_dir;
$smarty->template_dir = $template_dir;
if (isset($template)) $smarty->display($template);
// switch template dirs back
$smarty->template_dir = $smarty_template_dir;
// restore smarty vars
foreach ($smarty_vars as $_key => $_val) {
$smarty->assign($_key, $_val);
}
}
return null;
}
/* vim: set expandtab: */
?> |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Apr 15, 2005 3:24 am Post subject: |
|
|
Hi.
I believe {include} already does this, although it still sees its calling parent's scope (but of course can not alter it).
Code: | {include file="sample.tpl" var1=$var1 var2=$var2 ...}
|
|
|
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
|