/*
* Smarty plugin
* -------------------------------------------------------------
* File: function.agots.php
* Type: function
* Name: agots
* Description: This TAG creates a "x minute ago" like timestamp.
*
* -------------------------------------------------------------
* @license GNU Public License (GPL)
*
* -------------------------------------------------------------
* Parameter:
* - ts = the email to fetch the gravatar for (required)
* -------------------------------------------------------------
* Example usage:
*
* <div>{agots ts="3434323"} ago </div>
*/
Code language: PHP (php)
In above header, the Name: attribute is the name of your custom function and the File: attribute is the file name where you are writing this function. $params
and &$smarty
. The parameters that we pass in custom functions can be read through the associative array $params. In our case the parameter will be timestamp (ts) so we read this parameter as $params['ts']
in our custom function. function smarty_function_agots($params, &$smarty) {
if(!isset($params['ts'])) {
$smarty->trigger_error("gravatar: neither 'email' nor 'default' attribute passed");
return;
}
//return $params['ts'];
// array of time period chunks
$chunks = array(
array(60 * 60 * 24 * 365 , 'year'),
array(60 * 60 * 24 * 30 , 'month'),
array(60 * 60 * 24 * 7, 'week'),
array(60 * 60 * 24 , 'day'),
array(60 * 60 , 'hour'),
array(60 , 'min'),
array(1 , 'sec'),
);
$today = time(); /* Current unix time */
$since = $today - $params['ts'];
// $j saves performing the count function each time around the loop
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
// finding the biggest chunk (if the chunk fits, break)
if (($count = floor($since / $seconds)) != 0) {
break;
}
}
$print = ($count == 1) ? '1 '.$name : "$count {$name}s";
if ($i + 1 < $j) {
// now getting the second item
$seconds2 = $chunks[$i + 1][0];
$name2 = $chunks[$i + 1][1];
// add second item if its greater than 0
if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
$print .= ($count2 == 1) ? ', 1 '.$name2 : " $count2 {$name2}s";
}
}
return $print;
}
[/code]
<h3>Step 3: Save the custom function in Plugins directory</h3>
We are ready to deploy our custom function to Smarty. All you have to do is just copy the PHP file function.agots.php to <strong>plugins</strong> directory in smarty installation directory. Just make sure that the name of the plugin file is same as the name specified in file header. The good practice is to have name as function.<custom_function>.php.
<h2>Calling the custom function</h2>
To call your new function, you simply reference it by its name within curly brackets. And if the function requires a parameter, like ours does, you’d call it like this:
<!-- wp:code {"language": "php"} --><pre class="wp-block-code"><code></code></pre><!-- /wp:code -->
{agots ts='2451254'}
Code language: PHP (php)
You may want to pass the value of smarty variable to our custom function. Above is a simple example to create your own custom function in smarty. You may extend this and add your own useful functions. You may want to bookmark this article and share it with others.Code language: PHP (php){agots ts=$myunixtimestamp}
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Very good one. Cool :-)
I want to add meta tag's keywords and description attributes dynamically.
First I done it within include.inc.php and assigned the variables in template files.
But after creating smarty function i can use it like:
function smarty_function_metatag($params, &$smarty) {
if(!isset($params['tmpl'])) {
$smarty->trigger_error("metatag: parameter 'tmpl' missing");
return;
}
else if(!isset($params['ttype'])) {
$smarty->trigger_error("metatag: parameter 'ttype' missing");
return;
}
$pfile = $params['tmpl'];
switch ($params['ttype']) {
case 'keywords':
$array_keywords = array(
'login'=>'Login,Extentia',
'faq'=>'FAQ,Extentia',
'news'=>'News,Extentia',
'contact'=>'Contact Us,Extentia',
'careers'=>'Careers,Extentia',
'forgot'=>'Forgot Password,Extentia'
);
if (array_key_exists($pfile, $array_keywords)) {
return $array_keywords[$pfile];
}
break;
case 'description':
$array_description = array(
'login'=>'Login to Extentia,Extentia',
'faq'=>'FAQ about Extentia,Extentia',
'news'=>'News about Extentia,Extentia',
'contact'=>'Contact Us,Extentia',
'careers'=>'Careers at Extentia,Extentia',
'forgot'=>'Forgot Password,Extentia',
);
if (array_key_exists($pfile, $array_description)) {
return $array_description[$pfile];
}
break;
}
}
Your post helped me a lot.
Thank you Viral Patel.
Use above function like:
Thanks...I couldn't remember the correct syntax. This is easier to understand than the smarty manual for this topic.
BTW, Section 2 of your example in the function definition, the ampersand is getting rendered as its "&' entity form which might confuse someone new to PHP/smarty wondering why your example won't work.
This is awesome, you have saved me so much time and didn't realize how easy it was to create Smarty plugin! Thank you.
i have listings.html, header.html
and created myfunction in
i created a function in libs/plugins
when i use it in listings.html is working but in header.html just give me an empty space. any ideea why?
Thanks
Nice man :)
How to write this one ?
[code language="php"]
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//echo $url;
$sell="http://www.sneakermarket.com/sellitems.html";
if($sell==$url)
{
echo $this->_tpl_vars['myobj']->getTpl('general','header1.tpl');
}
else
{
echo $this->_tpl_vars['myobj']->getTpl('general','header.tpl');
}
[/code]
I had to modify a shopping cart application built on smarty. Never used it before. With this tutorial I was quickly able to write a function to pull some info from the database and return it in the function. Many thanks.