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

modifier.highlight_char.php

 
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
formfunction
Smarty n00b


Joined: 04 Mar 2004
Posts: 3
Location: Cape Town, South Africa

PostPosted: Mon Mar 08, 2004 11:26 am    Post subject: modifier.highlight_char.php Reply with quote

Hi all,

I noticed alot of people post plugin contributions here, so I thought I'd add one of mine as well, hope thats acceptable in this forum!!

Quite simple, just lets you apply a nice style to the first letter of a string, useful for page titles, or fancy paragraphs, etc.

HTH,
Mark

Summary

This modifier wraps the first character of the input string with a css class using by default a span tag. This is typically used when one wants to alppy a special formatting to things like page headers, etc. The modifier allows you to control what css class if used, and what tag is used. You can also opt to wrap with just an HTML tag and ignore the css class.


Example

Code:

// where $x = "Hallo world"

Smarty code:
    {$x|highlight_char:myFancyStyle}

Output:
    <span class="myFancyStyle">H</span>allo world


Smarty code:
    {$x|highlight_char:myFancyStyle:div} 

Output:
    <div class="myFancyStyle">H</div>allo world


Smarty code:
    {$x|highlight_char:none:b}   {* using "none" as the css class causes modifier to ignore this option}

Output:
    <b>H</b>allo world


Known Issues

None


Code

Code:

<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty {highlight_char} modifier plugin
 *
 * Type:     modifier<br>
 * Name:     highlight_char<br>
 * Date:     March 8, 2004<br>
 * Purpose:  highlight first character of the input string with css class<br>
 * Input:
 *         - string = string whose first char should be wrapped in the given class
 *
 * Examples:<br>
 * <pre>
 * {$xxx|highlight_char:big_font}
 * Output:
 *   <span class="bigfont">x</span>xx
 *
 * {$xxx|highlight_char:none:b}    {* you cannot leave a param empty, so none is no class *}
 * Output:
 *   <b>x</b>xx
 *
 * </pre>
 * @author Mark Hewitt <mark at formfunction dot co dot za>
 * @version  1.0
 * @param string $string Text to modify
 * @param string $css_class CSS classname to put in span tags, use 'none' to ignore
 * @param string $tag_name (Optional) Tag to wrap first char in
 * @param Smarty
 * @return string|null
 */
function smarty_modifier_highlight_char($string,$css_class,$tag_name="span")
{                                  
   // strip whitespace off the front of the string, and make sure
   // there are characters in the string, we only return valid characters...
   if ( ($s = trim($string)) != '' )
   {            
      $html = "<$tag_name";
      if ( $css_class != 'none' ) $html .= ' class="'.$css_class.'"';
      $html .= '>'.substr($s,0,1)."</$tag_name>".substr($s,1);
      return $html;
   }
   else
   {   
      return '';
   }   
      
}

?>

_________________
Technical Manager,
FormFunction
http://www.formfunction.co.za
Bespoke Application Solutions
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Adar
Smarty Regular


Joined: 27 May 2004
Posts: 58

PostPosted: Fri May 28, 2004 12:13 am    Post subject: Reply with quote

nice one Smile
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri May 28, 2004 6:20 am    Post subject: Reply with quote

Hi formfunction!

All contributions are certainly welcome--after all, this is a site for the community of users! Also feel free to post your plugin at the plugin page at the wiki.

Cheers and thanks!
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