Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

html_jquery_tabs

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
andref
Smarty n00b


Joined: 17 Sep 2009
Posts: 3

PostPosted: Thu Sep 17, 2009 7:47 pm    Post subject: html_jquery_tabs Reply with quote

Hello, I just added a new plugin to the Smarty Wiki.
This is the html_jquery_tabs plugin based on the jquery tab plugin from .


The plugin can be found in the Wiki under the name of html_jquery_tabs

Thanks to Klaus Hartl for developing jquery tabs.

Code:

 $templates[0]['title'] = "Edit";
 $templates[0]['template'] = 'edit.tpl';

 $templates[1]['title'] = "View";
 $templates[1]['template'] = '{html_table params=$somevar }';
 $smarty->assign("templates", $templates);
 $smarty-assign("container", "test");



The Code also here:

Code:

<?php

/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 *
 */


/**
 * Smarty plugin
 *
 * Type:     function
 * Name:     html_jquery_tabs
 * Date:     Set 17, 2009
 * Purpose:  Uses jquerytabs JS to generate tabs in html from a template or smarty function plugin
 * Input:
 *         - tabs = array - containing a associative of title + template = All Tabs + templates or smarty function
 *          - container = string ( unique id for the div container )
 * Example: 
 * {php}
 * $templates[0]['title'] = "Edit";
 * $templates[0]['template'] = 'edit.tpl';

 * $templates[1]['title'] = "View";
 * $templates[1]['template'] = '{html_table_creator params=$somevar }';
 * $smarty->assign("templates", $templates);
 * $smarty-assign("container", "test");
 * {/php}
 *
 * {html_tabs tabs=$templates container=$container}
 *
 * @version  1.0
 * @author   Fernando Andre <netriver at gmail . <om>
 * @param array
 * @param string
 * @return void
 */

/*
 * Header for using the tabs
 * @param string $container
 * @param array $titulos menu caption
 *
 */
function tabHeader( $container, $titulos )
{
    $header = '<script type="text/javascript">';
    $header .= "$(function() {\n";
   $header .= "$('#$container').tabs({ fxAutoHeight: true });";
   $header .= "});\n";
   $header .= "</script>\n";
   $header .= "<div id=\"$container\">\n"; // Container for the created tabs
   $menu = tabMenu($container, $titulos);

   return ($header.$menu);
}

/*
 * create the menu
 * @param string $container
 * @param array $titulos
 *
 */
function tabMenu($container, $titles){
   if (!is_array($titles) || count($titles) <= 0 ) {
      $smarty->trigger_error("html_tabs: No titles for menus.");
        return;
   }

   $menu = "<ul>\n";
   $i=0;
   foreach ($titles as $title){
      $menu .= "<li>\n
      \t<a href=\"#".$container."_".$i."\"><span><font face=arial>$title</font></span></a>\n";
      $menu .= "</li>\n";
      $i++;
   }
   $menu .= "</ul>\n";

   return $menu;
}

/*
 * Tab Content via plugin function ou template
 *
 * @param string $containerkey
 * @param string $titulo
 * @param object $smarty
 *
 */

/*
* Prepares the div and places the content of the template
* or variable/function
* @param string $containerkey
* @param string $titulo ( menu title )
* @param string $content content of the div
* @param object $smarty Object
*/
function tabContent($containerkey, $title, $content, $smarty){
   print "<div id=\"$containerkey\">\n";
   print "<fieldset>\n";
   print "<legend>$title</legend>\n";

   if ($smarty->template_exists($content))
   {
      $smarty->display($content);
   }else{
      // Treat it has a function;
      $content = ltrim($content);
      $C = strpos($content, " ");
      if ( $C !== false &&  ){
         $func = trim(substr($content, 1, $C));

         if ( ($tmp = $smarty->_get_plugin_filepath("function", $func)) !== false )
         {
            require_once $smarty->_get_plugin_filepath('function', $func);

            $parametros = substr($content, $C, strlen($content));

            $parametros = substr($parametros,0, strlen($parametros)-1);

            $param = preg_split("/ [\s=\s]+ /", $parametros);

            $cp = count($param);

            for($i=0; $i < $cp ; $i++  ){
               $param[$i] = trim($param[$i]);
               $param[$i+1] = trim(substr($param[$i+1], 1, strlen($param[$i+1])));
               $params[ $param[$i] ] = $smarty->get_template_vars($param[$i+1]);
               $i = $i+2;
            }

            $func = "smarty_function_".$func;
            $func($params, $smarty);
         }else{
            print $content;
         }
      }
   }

   print "</div>\n";
}
/*
 * Display the footer of the container
 */
function tabFooter(){
   print "</div>\n";
   print "<!-- fim tab footer -->\n";
}

/*
 * Criar tabs para ligada a cada template
 * @param array $templates
 * @param string $container nome unico para uma pagina
 * @param object $smarty
 */
function tabThis($templates = array(), $container = 'container-1', $smarty)
{
    if ( !is_array($templates) || count($templates) <= 0 ) {
        $smarty->trigger_error("html_tabs: No templates set.");
        return;
    }

    foreach ($templates as $template ){
       $menu[] = $template['title'];
    }

    $header = tabHeader( $container,  $menu );

    print $header; // print header

    $i=0;
    foreach($templates as $template ){
       // show content of include
       tabContent($container."_".$i, $menu[$i], $template['template'], $smarty);
       $i++;
    }

    tabFooter(); // print footer

}

/*
 * Called from smarty
 * @param array $params
 * @param Object $smarty
 */
function smarty_function_html_jquery_tabs($params, $smarty) {
   if (!isset($params['tabs']) || !isset($params['container']) ){
      $smarty->trigger_error("html_tabs: Missing tabs or container.");
        return;
   }
   $templates = $params["tabs"];
   $container = $params["container"];
   tabThis($templates, $container, $smarty );
}


?>
Back to top
View user's profile Send private message
andref
Smarty n00b


Joined: 17 Sep 2009
Posts: 3

PostPosted: Fri Sep 25, 2009 9:07 pm    Post subject: fix Reply with quote

Had a problem in using smarty functions

Replacing the for cycle fixes this
Code:

                for($i=0; $i < $cp ; $i++  ){
                    $param[$i] = trim($param[$i]);
                    if (strpos($param[$i], "=") !== false ){
                        $param[$i] = explode("=", $param[$i]);
                        $param[$i][1] = trim(substr($param[$i][1], 1, strlen($param[$i][1])));
                        $params[ trim($param[$i][0]) ] = $smarty->get_template_vars( trim($param[$i][1]) );
                    }else{
                        $param[$i+1] = trim(substr($param[$i+1], 1, strlen($param[$i+1])));
                        $params[$param[$i]] = $smarty->get_template_vars( trim($param[$i+1]));
                        $i = $i+2;
                    }

                }
Back to top
View user's profile Send private message
andref
Smarty n00b


Joined: 17 Sep 2009
Posts: 3

PostPosted: Fri Sep 25, 2009 9:13 pm    Post subject: sample.php Reply with quote

Sample.php
<code>
$smarty = new Smarty();
// Other code
// First TAB
$templates[0]['title'] = "Edit";
$templates[0]['template'] = 'edit.tpl';

//Second Tab
$templates[1]['title'] = "View";
$templates[1]['template'] = '{html_SOME_FUNCTION params=$somevar }';

$smarty->assign("params", $somevar); <- this is important so smarty can get the var for the other function your calling
$smarty->assign("templates", $templates);
$smarty-assign("container", "test");
</code>
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP