Re: [SMARTY] Including modules

From: Date: Fri, 15 Apr 2005 21:52:57 +0000
Subject: Re: [SMARTY] Including modules
References: 1 2 3 4 5 6 7  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-12519@lists.php.net to get a copy of this message
You don't need to have sql in the smarty function. Here's example code I've built to do something similar to what you're talking about. <?php /* * Created on Feb 27, 2005 * * Author: Chris Hubbard * Project: project_name * Description: * Load modules for display */ // drop file "function.load_mods.php" in plugin directory // setup our function for fetching stock data /** * load_mods * this function receives an array of modules (mods) based on this structure: * [section] = array (
 *                      0=>[path_to_module/module_name.php],
 *                      1=>[path_to_module/module_name.php])
* @param array mods * @param object smarty * @return array */ function load_mods($mods, &$smarty) {
    if (is_array($mods))
    {
        /**
         * sectionkeys is the extracted list of sections from the mods array
         */
        $sectionkeys = array_keys($mods);
        $modarray = array();
        $lmcount = count($sectionkeys);
        for ($xx=0; $xx<$lmcount; $xx++)
        {
            $tmp = "";
            $innercount = count($mods[$sectionkeys[$xx]]);
            for ($yy=0; $yy<$innercount; $yy++)
            {
                include_once($mods[$sectionkeys[$xx]][$yy]);
                // $data gets built in the included file
                $tmp .= $data;
            }
            /*
             * if you see a php notice: undefined index, then it's likely that
             * the included file is overwriting one of the variables in this function
             */
            $modarray[$sectionkeys[$xx]] = $tmp;
        }
        return $modarray;
    }else{
        return;
    }
} function smarty_function_load_modules($params, &$smarty) { // call the function $data = load_mods($params['mods'], $smarty); // assign template variable $smarty->assign($params['assign'], $data); } ?> In the php file I've got this code:
        require_once('modules.class.php');
        $mods = new modules();
        $mods->set_modules("right", array("mods/mods_google_search.php", "mods/mods_google_adsense.php"));
        $mods->set_modules("left", array("mods/mods_category_nav.php", "mods/mods_recent_viewed_products.php"));
        $tpl->assign("page_mods", $mods->get_modules());
All the modules class does is build an array of modules with the set_modules function. There's no other logic at this point. Then in my template file I've got this at the top: {if isset($page_mods)} {load_modules mods=$page_mods assign="mods"} {/if} And then in the template I've got this: {if isset($mods)} {$mods.left} {/if} And for an example module: <?php /* * Created on Mar 3, 2005 * * Author: Chris Hubbard * */ /** * string_width * the maximum number of display characters for this mod */ $string_width = 20; require_once('product_viewed.class.php'); $pv = new product_viewed(); // viewed_ids is an array of the recently viewed product ids $viewed_ids = $pv->display($_SESSION['id']); if ($viewed_ids) {
    // now get the product name to display
    require_once('product.class.php');
    $prod = new product;
    $count = count($viewed_ids);
    if ($count > 0)
    {
        $data = "";
        for ($x=0; $x<$count; $x++)
        {
            if (strlen($prod->productnamebyid($viewed_ids[$x])) > $string_width)
            {
                $trimmed_title = substr($prod->productnamebyid($viewed_ids[$x]), 0, 20) ."...";
            }else{
                $trimmed_title = $prod->productnamebyid($viewed_ids[$x]);
            }
            $data .= "<a href=\"book.php?id=". $viewed_ids[$x] ."\" class=\"link\">". $trimmed_title ."</a><br>";
        }
    }else{
        $data = NULL;
    }
    $smarty->assign("product_viewed", $data);
    $data = $smarty->fetch("./mods/templates/mods_recent_viewed_products.tpl");
} ?> Please note this code is still in beta and needs to be improved. But it should at least demonstrate how all the bits work together. Hope this helps. Chris On Apr 15, 2005, at 2:35 PM, Jonathan Villa wrote:
My only thing is that I wanted to keep SQL and etc. out of the smarty function. My plan was to only use smarty functions for things that are portable, i.e. rss feeds, weather, stock ticker, etc... Perhaps I can take a look Smarty.class.php and see if I can tweak it to fit stubborn ways? *shrug*
This approach has worked for me... header.tpl: {load_top_nav assign="nav_data"} {* navigation display goes here, example: *} {foreach from=$nav_data key="key" item="val"} {html_options values=$key output=$val} {/foreach} plugins/function.load_top_nav.php: function smarty_function_load_top_nav($params,&$smarty) { // do SQL, etc. to load up the navigation data array (with NO presentation markup!) return $nav_data; } Now you have your application code in a separate function, and your presentation 100% in smarty templates. There is no recursive smarty template fetches or similar complications involved. Jonathan Villa wrote:
I figured that... *shrug* Here's what I've done on header.tpl {top_nav} then in plugins/function.top_nav.php function smarty_function_top_nav($params, &$smarty) {
    include('top_nav.php');
} then on top_nav.php $smarty->display('top_nav.tpl'); and tried $this->display('top_nav.tpl'); errors are smarty: Fatal error: Call to a member function display() on a non-object... this: Fatal error: Using $this when not in object context in... you're probably wondering, why don't I just put my logic in the smarty_function_top_nav function like it's supposed to be... well answer is because I want application specific code in the same directory...I'll only be giving write permissions to a specific directory to future developers... so I don't want them worry about Smarty... just pretty much running middle tier code... *shrug*
FYI, when you are inside of something like {php_include}, the proper object name is $this->func(), not $smarty->func(). Jonathan Villa wrote:
why not refactor these 3 as smarty plugins... then you can do stuff like: header.tpl ----------- {top_nav} {left_nav} {right_nav} take a look at the files in the plugins dir of smarty for info on how to write a smarty function/plugin.
I did exactly that but I kept getting an error about $smarty-> not being instantiated... you know the error "xxxon a non object" I'm instantiating it in file called app-config.inc that get's prepended to all application files... that setup has worked for months... just get an error when trying to use it in a smarty plugin.
-- Smarty General Mailing List (http://smarty.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.smarty.general (#12519) next »