Call a Smarty tag from a UDT
Previous article Next articleFor my pneumatic tube website I *used* the Products module for CMS Made Simple as an image library. The module was great for this purpose, I can add images and can store additional information in custom fields. But for adding a single image to a page I needed a large module tag with parameters. If at some point I want to change something here I need to change all tags in the pages...
I searched and found a method to make this easier for me. The idea behind this is you create a simple User Defined Tag which contains the large module tag, with a single parameter you can call this UDT.
Note: You need to have a grasp of PHP knowledge to use this tutorial! If you don't have it, but still want to use this UDT then Google is your best friend. The feedback form below and the CMSMS forum isn't the place for these kind of questions!
How to use
First you need to have a clear picture what you want to make, especially what kind of parameter do you want. In my case I needed an ID parameter...
Create a new User Defined Tag and use a short and easy to remember name, in example "ShowProduct". In my case the content of the UDT is:
$smarty_data = "{Products action='details' productid='".$id."' detailtemplate='foo'}";
$smarty->display('eval:'.$smarty_data);
Instead of using the long module tag:
I now can use the short version in my pages:
A very efficient way of working, but you don't do this for a few module tag calls. In my case it is worth the trouble, because I use it a few hundred times :)
Other examples
Fetch the content of a Smarty tag from a UDT
An example posted by Hendrik, read the checkbox "This page is searchable" in the options tab of the page, and use it in the HTML template to hide the page from search engines (or not).
Create the UDT:
In the <head> area of your website, you can add the tag at the spot you want the robots - noindex line:
Comment Form
ReviewManager
ReviewManager
5 Comments
I added the example posted by Hendrik to the article
Thanks, Rolf
Thanks again for this, very helpful!
I actually needed to re-use the eval'd data so instead of "->display" I used "->fetch"
I used this to set the robots meta tag according to whether or not the "page searchable" attribute was set. if you got any use for it, here's the code:
/* -- CMSMS UDT: metasearchable -- */
echo '<meta name="robots" content="'.(( $smarty->fetch('eval:'."{page_attr key='searchable'}") == 1 ) ? 'index,follow' : 'noindex' ).'" />';
Hi,
Thanks for your advice.
It works just the way i wanted, this was what I was looking for and it solved the struggle with difficult tags in your website!
Thanks again!
Your website has been very helpful to me.
I was wondering if it is possible to set / assign a variable in one template that could then be available in another template. I have been working on this for a while now and can't seem to find a solution.
Any advice you can offer would be much appreciated.
Thank you!