Question

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 this 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.

Was it helpful?

Solution

Roughly, it would be something like this (not tested):

function.gallery.php in your plugin folder

function smarty_function_gallery($params, $template)
{
    if (empty($params['link'])) {
        trigger_error("gallery: missing 'link' parameter",E_USER_WARNING);
        return;
    } else {
        $link = $params['link'];
    }

   return '<a class="fancybox" rel="gallery1" href="'.$link.'"><img src="'.$link.'" alt="" width="200" height="200" /></a>';
}

although I think it's a too specific piece of html to make a plugin just for that

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top