Re: Recursivity in smarty
| From: | AaronJ | Date: | Thu, 10 Apr 2003 16:44:20 +0000 |
| Subject: | Re: Recursivity in smarty | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-9211@lists.php.net to get a copy of this message | ||
I went a step farther, or perhaps maybe just a different direction. I use
smarty in a more MVC like manner, with a single file that runs all of my
sites, and various function that present objects (calendar,search,email,etc)
for a site to use. However the main content is pulled via the {main}
function (see below). However what this function does, is look for a
parameter passed to the page "?action=somecustomsmartyfunction" and then
loads and executes the smarty function. Additionally, although I think
smarty now handles this (anyone?).. I gather all the URL variables here so
that all these functions don't need to be ugly with calls like {function
param1=$smarty.get.param) instead they are just {function} calls, and I
figure out the params here....
BTW-- this is all part of a Commerce/Content/Traffic/Email management system
that has Smarty/Adodb/MySQL at the core. I would love to get Smarty folks
involved or commenting:
http://sourceforge.net/projects/coolerserver/
(It's all in CVS)
/*
* CSP Smarty Plugin
* -------------------------------------------------------------
* Type: function
* Name: main
* Purpose: All functions that modify the primary template display are
processed here.
* Depending on $action variable, function(plugins) are executed.
Default is display_view
* Author: Aaron Judd
* Modified:
* -------------------------------------------------------------
*/
function smarty_function_main($params, &$smarty) {
global $HTTP_SERVER_VARS;
global $HTTP_POST_VARS;
extract($HTTP_POST_VARS);
// Aaron: "If it came from smarty fine, but we hate long strings so we are
going to put
// the url params into $params ourselves."
// Rhett: "Huh? why do we need to parse the URL here?"
// Aaron: Because smarty doesn't retrieve the URL params unless the template
used the command $smarty.get.param, and we don't want to do that all the
time!
$url=parse_url($HTTP_SERVER_VARS[REQUEST_URI]);
parse_str($url[query],$params);
extract($params);
extract($smarty->_tpl_vars);
if (!$action) {
foreach ($smarty->plugins_dir as $key=>$dir) {
if (include_once ($dir.'function.display_view.php')) break;
}
smarty_function_display_view($params, &$smarty);
} else {
$function= "function." .$action .".php"; // make sure
that file with smarty plugin
foreach ($smarty->plugins_dir as $key=>$dir) {
if (include_once ($dir.$function)) break;
} // is included
$function_name = "smarty_function_".$action; // invoke smarty
plugin by explicit
$function_name($params, &$smarty); // function call
}
}
"Christopher Rousseau" <christopher.rousseau@rezotech.com> wrote in message
news:001201c2ff3a$886435d0$1500a8c0@WKSCRU...
Hello,
I was encounter a small limite using smarty : recursivity
I have this type of class :
Class X {
getChildren() {
// Some code to create a list of children
return $arrayOfChildren ; // Array of Object
instanciate from class X (again, and again, and again,.)
}
}
I was using it for the creation of infinitly recursive menu.
But I was not able to write my recurive menu using Smarty. It's why I
have create this small plugin :
------------------------------------------------------------------------
---
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Author : Christopher Rousseau <christopher.rousseau@wanadoo.fr>
* Type: function
* Name: recursive
* Purpose: execute another smarty template inside a template smarty
* Parameters:
* template: template name
* [param] : all else parameter will be assigned to
* the new smarty instance
* -------------------------------------------------------------
*/
function smarty_function_recursive($params, &$smarty)
{
if
(!array_key_exists('template',$params)||$params['template']==Null) {
$smarty->trigger_error("assign: missing 'template' parameter");
return;
}
$recursiveSmarty = new Smarty();
$recursiveSmarty->template_dir = $smarty->template_dir;
$recursiveSmarty->compile_dir = $smarty->compile_dir;
foreach($params as $key => $value) { // we assign all
else parameter to the new Smarty
if ($key!='template')
$recursiveSmarty->assign($key, $value);
}
return $recursiveSmarty->fetch("$params[template]");
}
?>
This plugin instanciate a new Smarty with the same configuration.
I'm currently use it like that :
I assign my root menu in $list variable, $list is an array of object X
Root.tpl :
------------------------------------------------------------------------
---
<ul>
{foreach from=$list item=menu}
{recursive template='recursive.tpl' menu=$menu}
{/foreach}
</ul>
------------------------------------------------------------------------
---
recursive.tpl :
<li>
{$menu->getField('name')}
<ul>
{foreach from=$menu ->getChildren() item=children}
{recursive template='recursive.tpl' menu=$children}
{/foreach}
</ul>
</li>
------------------------------------------------------------------------
---
I think I have forget some configuration to pass thru the recursive
Smarty (like cache configuration). But those parameter was anough for me
(I cache the root template).
Hop it can be usefull for some of you,
Bye.
Christopher Rousseau