Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
mini FAQ

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
xile
Smarty Rookie


Joined: 27 Apr 2003
Posts: 8
Location: United Kingdom

PostPosted: Sun Apr 27, 2003 1:36 pm    Post subject: mini FAQ Reply with quote

Well i've only been using smarty for a short time, and i've found these forums very useful, so i thought an FAQ may help others who are new to it like myself find the answers to their questions a lot quicker. So all please add to it Smile

Smarty doesn't work with PHP safemode on
    Open up the smarty.class.php file. Find the following line:
    [php:1:6e9bd4b5a4]var $use_sub_dirs = true;[/php:1:6e9bd4b5a4]
    and change it to:
    [php:1:6e9bd4b5a4]var $use_sub_dirs = false;[/php:1:6e9bd4b5a4]
    See Also: Warning: Smarty error: problem writing temporary file

When i use CSS with smarty i get errors about unknown commands
    Smarty is interprating the CSS as a smarty variable. To use CSS simply follow this format:
    Code:
    {literal}
    //CSS Code
    {/literal}

    See Also: Smarty wants to parse a javascript!

_________________
Only God Can Judge Me
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
boots
Administrator


Joined: 16 Apr 2003
Posts: 5613
Location: Toronto, Canada

PostPosted: Sun Apr 27, 2003 1:59 pm    Post subject: Reply with quote

Looping {section}
    You can access the item index, index_prev, index_next, iteration, first, last, rownum, loop and title.
    Code:
    {section name=test loop=$arr}
        {$smarty.section.test.index} : {$arr[test]}
    {/section}

    You can use section much like a for loop:
    Code:
    {section name=test loop=10}
        {$smarty.section.test.index}
    {/section}

    There is a shorthand, though it appears to be deprecated:
    Code:
    {section name=test loop=10}
        {%test.index%}
    {/section}

    See Also: section,sectionelse
Looping Associative Arrays {foreach}
    You can simply loop through an associative array with access to key and item values.
    Code:
    {foreach from=$arr key=arrkey item=arritem}
    {* assumes $arritem is a literal *}
    {$arrkey} = {$arritem}
    {/foreach}

    See Also: foreach,foreachelse
Advanced Looping
Referencing Arrays
    Associative arrays can be referenced using dot notation:
    Code:
    {$myarr.mykey.myvalue}
    {* equivalent to PHP $myarr['mykey']['myvalue'] *}

    Indexed arrays can be referenced using bracket notation:
    Code:
    {$myarr[0]}
    {* equivalent to PHP $myarr[0] *}

    More complicated accesses are possible, including dynamic access:
    Code:
    {$myarr[$foo.bar]}
    {* equivalent to PHP $myarr[$foo['bar']] *}

    {$myarr[$foo].bar}
    {* equivalent to PHP $myarr[$foo]['bar'] *}

    {$myarr.1.$bar}
    {* equivalent to PHP $myarr[1][$bar] *}

    NOTE: Some of these forms require relatively recent versions of Smarty.
    See Also: Embedding Vars in Double Quotes

The wiki has a FAQ page which can be edited by the community at large.


Last edited by boots on Tue Aug 10, 2004 4:44 pm; edited 7 times in total
Back to top
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 236
Location: Purdue University

PostPosted: Sun Apr 27, 2003 3:32 pm    Post subject: Reply with quote

wiki is a great tool
_________________
"Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5613
Location: Toronto, Canada

PostPosted: Sat May 24, 2003 8:25 am    Post subject: Reply with quote

@xile: I liked the conciseness of your message. I hope that you don't mind that I took the liberty to add two more links. I can imagine the list growing. Smile
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7265
Location: Lincoln Nebraska, USA

PostPosted: Wed Feb 02, 2005 11:01 pm    Post subject: Reply with quote

Warning: Smarty error: unable to read resource: "foo.tpl"

Smarty is unable to read the template file for one reason or another. The following instructions assume your smarty object is named $smarty.



    Troubleshooting Steps

    1) $smarty->template_dir

    By default, the value of $smarty->template_dir is templates, meaning that smarty will look for your template directory relative to your php_include path. It is highly recommended to set this to an absolute system filepath... relying on php_include path is error-prone and resource intensive. It is also common to use a path relative to the exucuting PHP script, such as $smarty->template_dir = "./templates"; or relative to this script such as $smarty->template_dir = dirname(__FILE__) . "/templates";

    2) the template file

    Be sure the template file exists under the template directory. For example, if you are calling $smarty->display("foo.tpl"); then Smarty will look for the template file in $smarty->template_dir . "/foo.tpl" (Smarty will automatically place a directory separator between the template directory and the filename if one is necessary.) Likewise, if you call $smarty->display("path/to/foo.tpl"); Smarty will expect that file to be in $smarty->template_dir . "/path/to/foo.tpl"

    3) file/directory existance and permissions

    OK, now for the test that should tell us everything. Try this script and see if the contents of your template file gets displayed:

    [php:1:e9c05cd2e3]<?php
    // setup smarty here
    $smarty = new Smarty();
    $smarty->template_dir = "/path/to/my/templates";

    // use your template filename, and be sure to put the "/" in there
    // if your template_dir does not have a trailing "/"
    $myfile = $smarty->template_dir . "/foo.tpl";
    echo "attempting to read $myfile\n";
    readfile($myfile);
    ?>[/php:1:e9c05cd2e3]

    If the above script fails with an error, this is certainly the problem. Double check that the file exists, then check the file permissions. For unix systems, you have to check both the file permissions and all directory permissions leading up to it. Files commonly have a chmod of 644 (-rw-rw-r--) and directories have a chmod of 755 (drwxr-xr-x). It is important that the user executing the script (either you from the command line or the php user via apache) have access to the template files. This is the most common problem, so check all those file permissions.

    Once you get that working, Smarty should be able to read the template file.

    These instructions exclude the MS operating system environment, maybe someone can add their own expertise?
Back to top
View user's profile Send private message Visit poster's website
Curtux
Smarty Rookie


Joined: 12 Jan 2006
Posts: 6

PostPosted: Thu Jan 12, 2006 9:26 pm    Post subject: Reply with quote

Just another way to use CSS

Code:

body{ldelim}
   background-color:#666666;
   {rdelim}
Back to top
View user's profile Send private message
qh8
Smarty n00b


Joined: 04 May 2006
Posts: 2

PostPosted: Thu May 04, 2006 1:56 am    Post subject: Reply with quote

Curtux wrote:
Just another way to use CSS

Code:

body{ldelim}
   background-color:#666666;
   {rdelim}

Code:

//javascript ,css
//smarty config
$smarty->left_delimiter  = '<{';
$smarty->right_delimiter = '}>';
Back to top
View user's profile Send private message
phpins33
Smarty Rookie


Joined: 19 Oct 2007
Posts: 6

PostPosted: Fri Oct 19, 2007 9:25 pm    Post subject: Reply with quote

Sorry I do not more about FAQ ,like how it install and we will work well
_________________
work at home
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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