|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
giaur Smarty n00b
Joined: 03 Apr 2004 Posts: 2 Location: Poland
|
Posted: Sat Apr 03, 2004 9:37 am Post subject: new version of function.gradient |
|
|
Hi,
i've changed the function.gradient plugin (originaly by Lucas Baltes) so it uses class/style and does not use font (it's deprecated, isn't it ?) but span indstead.
Using class attribute is imho better, because there is no sense to type same font-size and face for every letter. Now it should also validate http://validator.w3.org
I've also changed if/else for ?: and cut some unnecessary code so the plugin is smaller.
Hope you find the changes usefull.
The example should explain how to use it.
If something is wrong please let me know. I'm new to smarty plugins developement.
giaur
http://www.giaur.qs.pl
[php:1:1bb7b501b8]<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: gradient
* Version: 1.1
* Date: April 3, 2004
* Author: Lucas Baltes <lucas at thebobo dot com>
* class and style and 'if' for '?:' change
* by giaur <giaur at qs dot pl> http://www.giaur.qs.pl
* License: GPL (http://www.gnu.org/)
* Credits: <kristoffer at caveo dot se> for his posting in
* the php manual (dechex function, user notes)
* Purpose: apply a color gradient to a string of text.
* Install: drop into the plugin directory
* Required input:
* text = string of text to colorize
*
* Optional input:
* class = css class - set font-family, size etc.
* start = rgb colors to start with,
* separated by comma's (ie '255,255,0')
* end = rgb colors to end with,
* separated by comma's (ie '255,255,0')
*
* Examples: {gradient text="this is a gradient"}
* {gradient text="this is a gradient" }
* {gradient text="this is a gradient" class="my_class" start="255,255,0" end="0,0,255"}
* the whole file may look like this:
* {literal}
* <style type="text/css">
* span.fancy_text {
* font-size: 180%;
* font-family: Tahoma, Verdana, sans-serif;
* }
* </style>
* {/literal}
* {gradient text="gradient text gradient text" class="fancy_text" start='0,0,200' end='40, 40, 120'}
* -------------------------------------------------------------
*/
function smarty_function_gradient($params, &$smarty)
{
extract($params);
if (empty($text)) {
$smarty->trigger_error("gradient: missing 'text' parameter");
return;
} else {
$text = str_replace("\n", "", $text);
$levels = strlen($text);
}
$start = ($start != "") ? explode(",", $start) : array(0,0,255);
$end = ($end != "") ? explode(",", $end) : array(255,0,0);
for ($i=1;$i<=$levels;$i++)
{
for ($j=0;$j<3;$j++)
{
$buffer[$j] = $start[$j] - $end[$j];
$buffer[$j] = floor($buffer[$j] / $levels);
$rgb[$j] = $start[$j] - ($buffer[$j] * $i);
if ($rgb[$j] > 255) $rgb[$j] = 255;
$rgb[$j] = dechex($rgb[$j]);
$rgb[$j] = strtoupper($rgb[$j]);
if (strlen($rgb[$j]) < 2) $rgb[$j] = "0$rgb[$j]";
}
$style = "style=\"color: #" . $rgb[0].$rgb[1].$rgb[2]."\"";
$output .= "<span class=\"".$class."\" ".$style.">".$text[$i-1 ] . "</span>";
}
echo $output;
}
/* vim: set expandtab: */
?>[/php:1:1bb7b501b8] |
|
Back to top |
|
RoVeRT Smarty n00b
Joined: 17 Mar 2004 Posts: 2
|
Posted: Sun Apr 04, 2004 4:57 am Post subject: |
|
|
You can reduce amount of redundent text needed to send more if you wrap all the <spans> in another for the class name. I get about 25% average less text when modified.
Code: | $style = "style=\"color: #" . $rgb[0].$rgb[1].$rgb[2]."\"";
$output .= "<span ".$style.">".$text[$i-1 ] . "</span>"; /* change */
}
$output = "<span class=\"".$class."\">$output</span>"; /* add */
echo $output; |
|
|
Back to top |
|
|
|
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
|