SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Addict Hajduk's Avatar
    Join Date
    Nov 2004
    Location
    Behind you
    Posts
    357
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smarty template selection.

    Ok, I am learning to handle smarty slowly but fail to see how I should use this code? If anyone can point me in the right direction I would apreciate it.
    Code:
    <?php
    
    /**
    
    * Smarty plugin
    
    * @package Smarty
    
    * @subpackage plugins
    
    */
    
    /**
    
    * Smarty {list_templates} plugin
    
    *
    
    * Type: function<br>
    
    * Name: list_templates<br>
    
    * Purpose: Prints the dropdowns for templates selection.
    
    *
    
    * ChangeLog:<br>
    
    * Removed some useless assgned vars.
    
    * Changed to get the selected template from $template assigned from the template, not from $_GET!
    
    * Removed associative array keeping path=>name, names are == to paths when setting $smarty->template_dir
    
    * @version 0.0.4
    
    * @author Anton Blajev
    
    * @param string
    
    * @param Smarty
    
    * @return string
    
    */
    
    function smarty_function_list_templates($params, &$smarty)
    {
    
    require_once $smarty->_get_plugin_filepath('function','html_options');
    
    /* Default values. */
    
    foreach ($params as $_key=>$_value) {
    
    switch ($_key) {
    
    case 'selected_template':
    
    case 'sort':
    
    case 'template':
    
    $$_key = (string)$_value;
    
    break;
    
    default:
    
    $smarty->trigger_error("[list_templates] unknown parameter $_key", E_USER_WARNING);
    
    }
    
    }
    
    ####################################################################
    
    if($template_dir=="") { $template_dir=$smarty->template_dir; }
    
    $tplDirs=array();
    
    if($dtpl = opendir("$template_dir")) {
    
    while(false !== ($dir = readdir($dtpl))) {
    
    if(is_dir($template_dir."/".$dir) && $dir!="." && $dir!="..") {
    
    array_push($tplDirs,"$dir");
    
    }
    
    }
    
    closedir($dtpl);
    
    }
    
    if($sort==1) { sort($tplDirs); }
    
    if(sizeof($tplDirs)<1) { $tplDirs[" "]="No templates found in $template_dir."; }
    
    /* I see no point in this, if the path and name are different, yes but not in my case, when I set $smarty->template_dir where my tpls are it's pointless to keep them in bigger array! */
    
    /*
    
    $tplName=array();
    
    $tplPath=array();
    
    foreach($tplDirs as $key => $val) {
    
    array_push($tplName, $key);
    
    array_push($tplPath, $val);
    
    }
    
    */
    
    $tpl_result .= '<form name=template_select method=post>
    
    ';
    
    $tpl_result .= '<select name=template ';
    
    foreach($_GET as $key => $val) { if($key!="template") { $get_params.="$key=$val"; } }
    
    if($get_params!="") $get_params.="&";
    
    echo "TPL VAR: ".$smarty->get_template_vars('template')."<br>";
    
    if($smarty->get_template_vars('template')) {
    
    $selected_template=$smarty->get_template_vars('template');
    
    }
    
    $tpl_result .="onChange=\"return self.location.href='".$_SERVER[PHP_SELF]."?".$get_params."template='+document.template_select.template.value+'';\"";
    
    $tpl_result .='>
    
    <option> </option>
    
    ';
    
    $tpl_result .= smarty_function_html_options(array('output' => $tplDirs,
    
    'values' => $tplDirs,
    
    'selected' => $selected_template,
    
    'print_result' => false),
    
    $smarty);
    
    $tpl_result .= '</select></form>';
    
    $html_result .= $tpl_result;
    
    return $html_result;
    }
    
    /* vim: set expandtab: */
    
    ?>

  2. #2
    SitePoint Zealot euringroy's Avatar
    Join Date
    Sep 2005
    Location
    Staffordshire, England
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The Smarty WIKI plugins page offers the following description:
    Purpose: List all available templates(from $smarty->templates_dir="somedir" and adds JS so you can switch between templates degining template=$TEMPLATE.
    which I take to mean that it is a supporting utility that helps to manage different templates during development. I have been using Smarty on numerous sites for some time without ever needing it! Maybe someone will explain how this could transform our productivity in an instant....?
    Eric "Motorlegs" Roy
    If you can't keep up - stay at home

  3. #3
    SitePoint Addict Hajduk's Avatar
    Join Date
    Nov 2004
    Location
    Behind you
    Posts
    357
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I just started with it and am transferring things from normal CSS & PHP to Smarty. I run a site in 2 languages and hope this would ease it somehow. But I might as well just keep it all in 1 template with different files.

  4. #4
    SitePoint Zealot euringroy's Avatar
    Join Date
    Sep 2005
    Location
    Staffordshire, England
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you are working along the right lines.

    For multi-language support, you could look at using configs to pre-load the required set of language text strings into a single template with config_load. This might be easier to maintain, as you would have a single template to maintain and not have to manage two keeping them in step.
    Eric "Motorlegs" Roy
    If you can't keep up - stay at home


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •