Older Newer
Fri, 25 Sep 2009 22:10:17 . . . . (Fernando Andre)? [fix]


Changes by last author:

Added:
Note that this code uses jquery tabs from http://stilbuero.de/jquery/tabs/ before you use this code YOU MUST have already made the references in html for the css and javascript code of the jquery tab plugin.

Sample.php

<code>

$smarty = new Smarty();

// Other code

$templates[0]['title'] = "Edit";

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

$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>

Sample.tpl

<code>

{html_jquery_tabs tabs=$templates container=$container}

</code>

<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 params=$somevar }';

* $smarty->assign("templates", $templates);

* $smarty-assign("container", "test");

* {/php}

*

* {html_tabs tabs=$templates container=$container}

*

* @version 1.0

* @author Fernando Andre <netriver@gmail.com>

* @link http://stilbuero.de/jquery/tabs/

* @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]);

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;

}

}

$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.");

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 );

}

?>

</code>