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

custom smarty function to 'get' and 'replace' (plugin)

 
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 -> General
View previous topic :: View next topic  
Author Message
shady123
Smarty n00b


Joined: 05 Sep 2013
Posts: 1

PostPosted: Fri Sep 06, 2013 2:52 am    Post subject: custom smarty function to 'get' and 'replace' (plugin) Reply with quote

I am fairly new to Smarty and I am facing a little problem in writing a function for a plugin where I'd like to fetch a value from the smarty tag inserted inside any page and then I want that value to be added inside the code in the plugin file. To be precise, for example, this is going to be my Smarty tag which i'll insert inside a page:

{gallery link="../images/ball.png"}

Now what I want is that, I want the image link, which I've added inside the link="" tags, to be inserted inside the href="" and the src="" tags in the following code which is going to be inside the plugin file:

<a class="fancybox" rel="gallery1" href="IMAGE LINK FROM SMARTY TAG ABOVE"><img src="IMAGE LINK FROM SMARTY TAG ABOVE" alt="" width="200" height="200" /></a>

So basically, if I use the following tag:

{gallery link="../images/car.png"}

I want this to be echo'd out on the page:

<a class="fancybox" rel="gallery1" href="../images/car.png"><img src="../images/car.png" alt="" width="200" height="200" /></a>

After a lot of research, I found and read a tutorial at last and I've learned how to create a Smarty Plugin but I am not sure how to write the correct function in order to achieve the functionality which I want.

Looking forward to hear from you guys. Thank you.
Back to top
View user's profile Send private message
IT_Services
Smarty Rookie


Joined: 30 Sep 2013
Posts: 9

PostPosted: Mon Sep 30, 2013 10:55 am    Post subject: Reply with quote

Hi shady123,

have a look at this example (14.39): http://www.smarty.net/docs/en/api.register.plugin.tpl#idp16782656

In your case the function should look like this:

Code:
$smarty->registerPlugin("function","gallery", "my_gallery_function");

function my_gallery_function($params, $smarty)
{
  if(!empty($params["link"])) {
   return '<a class="fancybox" rel="gallery1" href="' . htmlspecialchars($params["link"]) . '"><img src="' . htmlspecialchars($params["link"]) . '" alt="" width="200" height="200" /></a> ';
  }

return '';
}


Regards
Florian
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 -> General 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