Smarty Plugins
From ConShell
Smarty is a template engine for PHP. This mean it provides a way of separating the php from the html. You load a smarty object up with data and pass it a template.
This page contains some example plugins. Most are not mine except the last one. That one combines several plugins.
Contents |
Samples
FCKEditor display
This plugin displays the FCKEditor, a rich text editor for web apps on the page generated by your smarty template. As a general rule I've had problems displaying more than one FCKEditor on a page in the past. This plugin also makes assumptions about where in your webroot you setup fck editor.
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Include files
**/
require_once('../lib/fckeditor.php');
/**
* Smarty function plugin
* Requires PHP >= 4.3.0
* -------------------------------------------------------------
* Type: function
* Name: fckeditor
* Version: 1.0
* Author: gazoot (gazoot care of gmail dot com)
* Purpose: Creates a FCKeditor, a very powerful textarea replacement.
* -------------------------------------------------------------
* @param InstanceName Editor instance name (form field name)
* @param BasePath optional Path to the FCKeditor directory. Need only be set once on page. Default: /FCKeditor/
* @param Value optional data that control will start with, default is taken from the javascript file
* @param Width optional width (css units)
* @param Height optional height (css units)
* @param ToolbarSet optional what toolbar to use from configuration
* @param CheckBrowser optional check the browser compatibility when rendering the editor
* @param DisplayErrors optional show error messages on errors while rendering the editor
*
* Default values for optional parameters (except BasePath) are taken from fckeditor.js.
*
* All other parameters used in the function will be put into the configuration section,
* CustomConfigurationsPath is useful for example.
* See http://wiki.fckeditor.net/Developer%27s_Guide/Configuration/Configurations_File for more configuration info.
*/
function smarty_function_fckeditor($params, &$smarty)
{
if(!isset($params['InstanceName']) || empty($params['InstanceName']))
{
$smarty->trigger_error('fckeditor: required parameter "InstanceName" missing');
}
static $base_arguments = array();
static $config_arguments = array();
// Test if editor has been loaded before
if(!count($base_arguments)) $init = TRUE;
else $init = FALSE;
// BasePath must be specified once.
if(isset($params['BasePath']))
{
$base_arguments['BasePath'] = $params['BasePath'];
}
else if(empty($base_arguments['BasePath']))
{
$base_arguments['BasePath'] = '/FCKeditor/';
}
$base_arguments['InstanceName'] = $params['InstanceName'];
if(isset($params['Value'])) $base_arguments['Value'] = $params['Value'];
if(isset($params['Width'])) $base_arguments['Width'] = $params['Width'];
if(isset($params['Height'])) $base_arguments['Height'] = $params['Height'];
if(isset($params['ToolbarSet'])) $base_arguments['ToolbarSet'] = $params['ToolbarSet'];
if(isset($params['CheckBrowser'])) $base_arguments['CheckBrowser'] = $params['CheckBrowser'];
if(isset($params['DisplayErrors'])) $base_arguments['DisplayErrors'] = $params['DisplayErrors'];
// Use all other parameters for the config array (replace if needed)
$other_arguments = array_diff_assoc($params, $base_arguments);
$config_arguments = array_merge($config_arguments, $other_arguments);
$out = '';
if($init)
{
$out .= '<script type="text/javascript" src="' . $base_arguments['BasePath'] . 'fckeditor.js"></script>';
}
$out .= "\n<script type=\"text/javascript\">\n";
$out .= "var oFCKeditor = new FCKeditor('" . $base_arguments['InstanceName'] . "');\n";
foreach($base_arguments as $key => $value)
{
if(!is_bool($value))
{
// Fix newlines, javascript cannot handle multiple line strings very well.
$value = '"' . preg_replace("/[\r\n]+/", '" + $0"', addslashes($value)) . '"';
}
$out .= "oFCKeditor.$key = $value; ";
}
foreach($config_arguments as $key => $value)
{
if(!is_bool($value))
{
$value = '"' . preg_replace("/[\r\n]+/", '" + $0"', addslashes($value)) . '"';
}
$out .= "oFCKeditor.Config[\"$key\"] = $value; ";
}
$out .= "\noFCKeditor.Create();\n";
$out .= "</script>\n";
return $out;
}
?>
Yes/No Radio Buttons
Displays yes or no buttons.
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
**/
/**
* Smarty {html_yes_no} function plugin
*
* Type: function<br>
* Name: html_yes_no<br>
* Date: March 1, 2004<br>
* Purpose: Prints set of radio buttons side by side, essentially with value of<br>
* 1/0 [ true/false ], meant for Yes/No style options
* Input:
* - name = name of radio set
* - yes_label = Text for "yes" option [default: Yes]
* - no_label = Text for "no" option [default: No]
* - value = Currently selected radio button
* - default = default value, "yes" if missing [optional]
*
* Examples:<br>
*
* {html_yes_no name="is_visible" yes_label="Show this" no_label="Hide this" value=$is_hidden}
*
* @author Mark Hewitt <mark@formfunction.co.za>
* @version 0.1
* @param array
* @param Smarty
* @return string|null
*/
function smarty_function_html_yes_no($params, &$smarty)
{
if ( !isset($params['value']) && is_numeric($params['value']) )
{
$params['value'] = ( isset($params['default']) && $params['default']='no' ? 0 : 1 );
}
// detemrine CHECK state of the individual RADIO elements
$yes_state = ( isset($params['value']) && $params['value'] ? 'CHECKED' : '' );
$no_state = ( isset($params['value']) && !$params['value'] ? 'CHECKED' : '' );
// were labels given or must they default in?
if ( !isset($params['yes_label']) ) $params['yes_label'] = 'Yes';
if ( !isset($params['no_label']) ) $params['no_label'] = 'No';
// generate two radio buttons, first Yes button, the next to it separated by spaces
// the No button
$content = '<input type="radio" name="'.$params['name'].'" value="1" '.$yes_state.'>';
$content .= ' '.$params['yes_label'];
$content .= ' ';
$content .= '<input type="radio" name="'.$params['name'].'" value="0" '.$no_state.'>';
$content .= ' '.$params['no_label'];
return $content;
}
?>
Checkbox
Displays a single checkbox. May be enabled or disables based on the edit parameter.
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty {html_checkbox} function plugin
*
* Type: function<br>
* Name: html_checkbox<br>
* Purpose: Prints a single checkbox and a <labal> for it.
* Input:
* - heading = the <label> assoiated with the checkbox.
* - name = the name attribute of the checkbox
* - action = if not set or set to 'edit' the checkbox is enabled.
* - selected = if true the checkbox is checked.
*
* @author Justin Dearing <zippy1981@gmail.com>
* @version 0.1
* @param array
* @param Smarty
* @return string|null
*/
function smarty_function_html_checkbox($params, &$smarty) {
if (!isset($params['name'])) {
$smarty->trigger_error
('Parameter "name" must be set in smarty function html_checkbox',
E_USER_ERROR);
}
if (isset($params['heading'])) {
$content = "<label>{$params['heading']}</label>";
}
$content .= "<input name='{$params['name']}' type='checkbox' ";
$content .= isset($params['action']) && $params['action'] != 'edit' ? 'disabled ' : '';
$content .= $params['selected'] ? 'checked />' : '/>';
return $content;
}
?>
A read/write form
This one puts several plugins together. You pass it a value and how you want to display it. If you set action="edit" it displays a form field. If not it displays a span of text or a div depending on the paramaters passed to it. You can use this for example on a user profile page. This way the same template can be used to display a both a user profile and a user profile editor screen.
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
**/
/**
* Smarty function we abstract
**/
require_once('plugins/function.html_options.php');
require_once('../plugins/function.fckeditor.php');
require_once('../plugins/function.html_yes_no.php');
/**
* Smarty {ShowField} function plugin
*
* Type: function<br />
* Name: ShowField<br />
*
* Input:
* If $params[action] is set to edit return an input box
* names $params[name] with a value of $params[value]
* If $params[action] is set to display returns a span.
*
* @author Justin Dearing <zippy1981@gmail.com>
* @version 0.1
* @param array
* @param Smarty
* @return string|null
**/
function smarty_function_ShowField ($params, &$smarty) {
$params['heading'] .= ($params['required'] === true) ? "<span style='color: #ff0000'>(*)</span>" : '';
if ($params['problem'] == 'notset') {
$heading = "<label class='required'>{$params['heading']}: </label>";
}
else {
$heading = "<label>{$params['heading']}: </label>";
}
$value = $params['value'];
if (!isset($value) || $value === NULL || $value === 'NULL') {
$value = "";
}
switch ($params['action']) {
case 'edit':
switch ($params['editmode']){
case 'hiddeninput':
return "<input type='hidden' name='{$params['name']}' id='{$params['name']}' value='{$value}' />";
break;
case 'richtextarea':
$params['Value'] = $params['value'];
$params['InstanceName'] = $params['name'];
$content = smarty_function_fckeditor($params, $smarty);
break;
case 'textarea':
$rows = isset($params['rows']) ? $params['rows'] : 10;
$cols = isset($params['cols']) ? $params['cols'] : 40;
$content =
"<textarea name='{$params['name']}' " .
"rows={$rows} cols={$cols}>" .
$value .
"</textarea>";
break;
case 'options':
return smarty_function_html_options($params, $smarty);
break;
case 'yes_no':
return smarty_function_html_yes_no($params, $smarty);
break;
default:
$content =
'<input name="' .
$params[name] . '" value="' .
$value . '" ';
if (isset($params['size'])) {
$content = $content . 'size="' . $params[size] . '" ';
}
if (isset($params['maxlength'])) {
$content = $content . 'maxlength="' . $params[maxlength] . '" ';
}
if ($params['hidden'] == true) {
$content = $content . 'type="password" ';
}
$content = $content . '><br />';
break;
}
break;
case 'display':
default:
switch ($params['editmode']) {
case 'richtextarea':
case 'textarea':
$content = "<div class='TextBox'>{$value}</div>";
$heading = "<div class='TextBoxLabel'>$heading</div>";
break;
case 'yes_no':
$content = $params['display_value'] . '<br />';
break;
default:
$content = $value . '<br />';
break;
}
break;
}
if ($params[table_row] == true) {
$ret = "<tr><td>$heading</td><td>$content</td></tr>";
} else {
$ret = $heading . $content;
}
return $ret;
}
?>