Working with smarty templaes

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Working with smarty templaes

Post by calguy1000 »

I thought I'd post a few smarty tips here, and start a general area of discussion about how to work with smarty, and how to figure things out.  I'll start briefly, and maybe if I feel energetic, put this in a blog post later.

Smarty
Smarty is a very powerful PHP template engine.  There's lots that can be done in smarty if you just take the time and look. 
Smarty lives at http://smarty.php.net, and the documentation is quite extensive and available in multiple languages. Please read it, If you understand this you will get alot more power out of your CMS Made Simple based website.

Determining what variables are available to work with
There's a very convenient plugin in CMS to see the available smarty variables.  {get_template_vars}.  This plugin simply does a dump of all smarty variables known at that point.  To use it, simply place {get_template_vars} near the bottom of a content page, or in a specific function that you need to debug or work with.  Try it out.

Dumping arrays and objects
The {get_template_vars} plugin will not recursively dump out the values of Arrays, or Objects.  it will just tell you that a variable represents an array or an object.  But no fear, there is a way to see what's in them too.  You can use some php code inside of smarty.  For example {$variablename|print_r} will dump all of the contents of an array or an object, telling you the object members, or the array keys that can be used.

Here's something you could put inside the News summary template to see what you can do:

Code: Select all

{get_template_vars}
{$items|print_r}
Smarty Modifiers And Expressions
Smarty is like a programming language in and of itself (don't fear, it's relatively simple to learn.  it's like HTML on steroids).  You can place conditionals of any sort in your code, create your own variables, do string and date processing, and a whole bunch of other things.  Knowing Smarty language is a smart thing to do when working with CMS.

Smarty conditionals look something like this:

Code: Select all

{if $variable != value}
   Some Action or Text to be output
{else}
   Something Else
{/if}
Capturing a piece of a smarty template into a variable for further processing
It's possible to capture a piece of a smarty template into yet another smarty variable for futher testing, processing, and/or output.  This is all in the smarty documentation, but here's an example that I've answered more than once.

To capture the content of a non default CMS content block into a smarty variable to test to see if it is non emtpy, you could do something like this: 

Code: Select all

{capture assign="testvar"}{content block="block2"}{/capture}
{if $testvar != '' }
  {$testvar}
{/if}
Okay, that's my quick 30 second introduction into smarty, please post more of your questions or tips in here.  I'd be happy to read them.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
holodoc

Re: Working with smarty templaes

Post by holodoc »

Hi,

alright so far, I'm just wondering how to use smarty templates in the end then - or am I not supposed to use my own in cmsms?
I mean shall I create a new smarty object for my templates or can I use the existing  $smarty?
If so, do I simply put my templates in tmp/templates/* or might they be deleted there?
Unfortunately I couldn't find any hints on this searching the documentary :( (Smarty Tips is quite small yet ^^).

Thanks
Jens
User avatar
Elijah Lofgren
Power Poster
Power Poster
Posts: 811
Joined: Mon Apr 24, 2006 1:01 am
Location: Deatsville, AL

Re: Working with smarty templaes

Post by Elijah Lofgren »

holodoc wrote: Hi,

alright so far, I'm just wondering how to use smarty templates in the end then - or am I not supposed to use my own in cmsms?
I mean shall I create a new smarty object for my templates or can I use the existing  $smarty?
If so, do I simply put my templates in tmp/templates/* or might they be deleted there?
Unfortunately I couldn't find any hints on this searching the documentary :( (Smarty Tips is quite small yet ^^).

Thanks
Jens
For pages just edit the page templates in "Layout -> Templates"
Most modules have ways to edit their templates through the Admin Panel.

I wouldn't ever store templates in  tmp/templates/* because that is just a caching place and is sometimes cleared. Often modules will let you put them in modules/ModuleName/templates.

If you have a specific template question, I'll probably be able to better explain.

Have a great day,

Elijah
Note: I don't have time to take on any more projects. I'm quite busy. I may be too busy to reply to emails or messages. Thanks for your understanding. :)
holodoc

Re: Working with smarty templaes

Post by holodoc »

Hi,

well I tried to convert my page to the cms to see how it works in here and e.g. at one point I bypass a mail template through smarty to send it afterwards by php so I would have to save the mail template somewhere and fetch it from that page. I'm also not sure how to set the variables in the php code then since I don't know whether I can just use $smarty->assign or this might collide with the smartycode used by the cms if that makes sense :>
User avatar
Elijah Lofgren
Power Poster
Power Poster
Posts: 811
Joined: Mon Apr 24, 2006 1:01 am
Location: Deatsville, AL

Re: Working with smarty templaes

Post by Elijah Lofgren »

I've always used the existing $smarty. I guess if some vars were named the same there could be problems, but it can't hurt to try.
Note: I don't have time to take on any more projects. I'm quite busy. I may be too busy to reply to emails or messages. Thanks for your understanding. :)
holodoc

Re: Working with smarty templaes

Post by holodoc »

Alright, I tried to get a new instance of a Smarty object to have my separate template_dir.
So i created a user defined tag init_smarty

Code: Select all

require('../../files/config.php');
$mysmarty = new Smarty();
$mysmarty->template_dir = $smarty_template_dir;
$mysmarty->compile_dir = $smarty_compile_dir;
$mysmarty->cache_dir = $smarty_cache_dir;
$mysmarty->config_dir = $smarty_config_dir;
and added {init_smarty} to top of my template used. Working so far.

Then I created another user defined tag test
just containing

Code: Select all

$mysmarty->assign("mail_to", htmlentities($sendto));
and created a page containing only {test}

But now I get

Code: Select all

[Tue Mar 13 16:18:23 2007] [error] [client 127.0.0.1] PHP Fatal error:  Call to a member function assign() on a non-object in /portal_cms/html/cmsmadesimple/lib/content.functions.php(669) : eval()'d code on line 1, referer: http://localhost:1337/cmsmadesimple/index.php?page=user_defined_tags
For me this means, $mysmarty is no Smarty object but appearently I created it before so where is it then :>

Or is this a completely wrong way to do it? It's my first cms and I'm just used to doing all on my own :( So if that doesn't fit into a cms what I'm trying to do please tell me e.g. if I would have to write my own module or whatever... :)
User avatar
Elijah Lofgren
Power Poster
Power Poster
Posts: 811
Joined: Mon Apr 24, 2006 1:01 am
Location: Deatsville, AL

Re: Working with smarty templaes

Post by Elijah Lofgren »

If you're using templates, that I would create a module. What you are doing seems harder than making a module.

Some pages here might help:
http://wiki.cmsmadesimple.org/index.php ... e_Tutorial
http://wiki.cmsmadesimple.org/index.php ... ng_Modules

Otherwise just come into the #cms channel on irc.freenode.net and we should be able to help. :)

Hope this helps,

Elijah
Note: I don't have time to take on any more projects. I'm quite busy. I may be too busy to reply to emails or messages. Thanks for your understanding. :)
holodoc

Re: Working with smarty templaes

Post by holodoc »

Ah of course the php parts are split so my $mysmarty must be global, seems to work then, but I'll check the module thing - thanks for your help so far :)
Post Reply

Return to “Modules/Add-Ons”