Page 1 of 1

Adding Your Own Functionality To Smarty w/ Plugins Rate Topic: -----

#1 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Post icon  Posted 16 April 2012 - 07:21 PM

Before you can grasp this tutorial you must have some basic knowledge of Smarty. I suggest you visit my previous tutorial on Smarty. Please note that I will be using the same file structure in this tutorial as the previous tutorial.

Plug-ins
Plug-ins can be used to add additional functionality to software and Smarty has provided a way for developers to create and add their own plug-ins. Smarty plug-ins are useful in the instances where you need to reuse code in multiple template files or need to avoid adding PHP directly to a template. Plug-ins can be a template function, modifier, compiler function or a block function.

Template Functions
Let's create a template function that will allow us to detect what browser the client is using and display an appropriate message.
Firstly you must create a new file in this directory: <Smarty root directory>\classes\plugins. Within this directory create a file called function.browser_detect.php. Smarty references functions by the name of the file so it is very important you name it exactly that.

Secondly within your newly created file place this code:
<?php
	/**
	 * Smarty {browser_detect} plugin
	 *
	 * type: function
	 * name: browser_detect
	 * purpose: Detect the client's browser. Not the most accurate function but this is solely
	 *          solely for example purposes.
	 */
	function smarty_function_browser_detect(array $parameters, Smarty_Internal_Template $smarty)
	{
		$message = NULL;
		
		if(stripos($_SERVER['HTTP_USER_AGENT'], 'chrome'))
			$message = 'Chrome is pretty nice.';
		elseif(stripos($_SERVER['HTTP_USER_AGENT'], 'firefox'))
			$message = 'Firefox is not bad.';
		elseif(stripos($_SERVER['HTTP_USER_AGENT'], 'safari'))
			$message = 'Safari has a sexy UI.';
		elseif(stripos($_SERVER['HTTP_USER_AGENT'], 'msie'))
			$message = 'Internet Explorer has it\'s ups and downs';
		else
			$message = 'Your browser isn\'t in this list.';
		
		return $message;
	}
?>



To call this function add {browser_detect} to your template file. e.g.
{block name=body}
	<p>Here's what we have to say about your browser: <strong>{browser_detect}</strong></p>
{/block}



Smarty's functions all require a specific format. Each template function name must start with smarty_function_ and then the name of the function. It should also accept two parameters, the first an array and the second the Smarty_Internal_Template object.
  • Array $parameters - This associative array is used to store the parameters being passed to the function from the template. e.g.
    {browser_detect output_results=true}
    
    

    Our parameter here is output_results so therefore to reference that without our browser_detect function we must use the following:
    $parameters['output_results'];
    
    

  • Smarty_Internal_Template $smarty - This object is the Smarty object and it is passed by reference. This is useful in the instances where you need to assign values to placeholders from within your function.


Your function can do one of two things with it's results: return it or assign it to a placeholder. Our example returned its results. To assign our results to a placeholder consider the following:
//replace the return statement with this
$smarty->assign('message', $message);


In our template file you'll need this also.
{block name=body}
	<!-- call our function -->
        {browser_detect}
 
	<p>Here's what we have to say about your browser: <strong>{$message}</strong></p>
{/block}



There's more than one way to register a Smarty function. Smarty provides developers with a dynamic way of doing so with it's registerPlugin() function. Here's a quick example.
$smarty->registerPlugin('function', 'greeting', 'say_hello_world');
function say_hello_world(array $parameters, Smarty_Internal_Template $smarty)
{
      return 'Hello World';
}



Modifiers
Modifiers are plug-ins that allow the developer to 'modify' or process some content before displaying it. Modifiers can be applied on tags or placeholders.

We're now going to create a modifier that will hash some output with the desired algorithm and key. Creating the modifier file is similar to that of the function plug-in. Within the plugins folder mentioned earlier create a file named modifier.hash.php. Place this code within that file:
<?php
        function smarty_modifier_hash($string, $algorithm, $key)
	{
		return hash_hmac($algorithm, $string, $key);	
	}
?>



Every modifier function must has smarty_modifier in front of it's name just like smarty_function. A modifier function can take any amount of parameters but the first is a must. The first parameter is the string that will be modified. If we assign the text Hello World to the placeholder $text ($smarty->assign('text', 'Hello World');) then in our template file we will need this code to apply our hash modifier.
{$text|hash}

Since our hash modifier expects the algorithm and key we must also specify them.
{$text|hash:'MD5':'This is the key'}


Block Functions
Block functions are the functions that are executed on the contents of a block. A block function is created the same way a Smarty function and modifier are created.

Within the plugins folder create a file called block.nl2br.php. Add the following code:
<?php
	function smarty_block_nl2br(array $parameters, $content, Smarty_Internal_Template $smarty, &$repeat)
	{
		if($repeat == FALSE && isset($content))
		{
			return nl2br($content);
		}
	}
?>



This block function is called {nl2br}{/nl2br}. Basically what it does is converts all break lines to their HTML entity which is <br>. Let's break down the parameters:
  • array $parameters - Parameters to be passed from the template
  • mixed $content - String located between the opening and closing tags.
  • Smarty_Internal_Template $smarty - Instance of the Smarty object
  • boolean &$repeat - Boolean that lets you know whether you're handling the call on the opening or closing tag. Block functions are called twice, when the tag is opening and when it is closing. It defaults to TRUE on the opening tag but $content is NULL. It then defaults to FALSE on the closing tag while $content will contain the string to be manipulated.

This post has been edited by codeprada: 16 April 2012 - 07:23 PM


Is This A Good Question/Topic? 2
  • +

Replies To: Adding Your Own Functionality To Smarty w/ Plugins

#2 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Posted 17 April 2012 - 01:56 PM

Great tutorial! Plugins are an excellent way to harness the power of the template engine. I wish I had a tutorial this detailed when I first muddled my way through creating a plugin for Smarty :)
Was This Post Helpful? 0
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Posted 17 April 2012 - 03:55 PM

@Jstall Thanks! You can do almost anything a template is capable of with Smarty. The thing is there's still a lot to learn about it.

Glad you found the tutorial useful.
Was This Post Helpful? 0
  • +
  • -

#4 clod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-July 12

Posted 05 February 2013 - 12:23 AM

Here is one more good tutorial for Smarty:
http://www.youtube.c...h?v=w_EequZIny8
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1