• twitter image
  • facebook image
  • youtube image
  • linkedin image
Language: CMS Made Simple Czech CMS Made Simple France CMS Made Simple Spain CMS Made Simple Hungary CMS Made Simple Russia CMS Made Simple Netherlands

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Working with smarty templaes
PostPosted: Sat Jan 20, 2007 5:33 pm 
Offline
Dev Team Member
Dev Team Member
User avatar

Joined: Tue Oct 19, 2004 6:44 pm
Posts: 5954
Location: Fernie British Columbia, Canada
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:
{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:
{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:
{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
For quality help follow these instructions:
a) Think about the problem for an hour
b) research the problem for an hour
c) spend 1/2 an hour explaining it and providing as much information as you can muster
(too much information is okay, not enough information may get your question ignored).
--
if you can't bother explaining your problem well, why should we bother helping with it.
----------------
Don't make me angry..... you won't like me when I'm angry....


Top
 Profile  
 
 Post subject: Re: Working with smarty templaes
PostPosted: Tue Mar 13, 2007 2:33 pm 
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


Top
  
 
 Post subject: Re: Working with smarty templaes
PostPosted: Tue Mar 13, 2007 2:48 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Mon Apr 24, 2006 1:01 am
Posts: 811
Location: Deatsville, AL
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. :)


Top
 Profile  
 
 Post subject: Re: Working with smarty templaes
PostPosted: Tue Mar 13, 2007 2:59 pm 
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 :>


Top
  
 
 Post subject: Re: Working with smarty templaes
PostPosted: Tue Mar 13, 2007 3:03 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Mon Apr 24, 2006 1:01 am
Posts: 811
Location: Deatsville, AL
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. :)


Top
 Profile  
 
 Post subject: Re: Working with smarty templaes
PostPosted: Tue Mar 13, 2007 4:29 pm 
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:
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:
$mysmarty->assign("mail_to", htmlentities($sendto));


and created a page containing only {test}

But now I get
Code:
[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... :)


Top
  
 
 Post subject: Re: Working with smarty templaes
PostPosted: Tue Mar 13, 2007 4:47 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Mon Apr 24, 2006 1:01 am
Posts: 811
Location: Deatsville, AL
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. :)


Top
 Profile  
 
 Post subject: Re: Working with smarty templaes
PostPosted: Tue Mar 13, 2007 4:53 pm 
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 :)


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: paulbaker


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 post attachments in this forum

Search for:
Jump to:  
Arvixe - A CMSMS Partner