
If you have already read my basic Smarty tutorial, then it is enough to work with it and get help in small parts from the official documentation of Smarty. I have been actually working this way. However, I came to realize that, often, I forget or have issues with some basic implementation. And searching for similar things again and again often. Thus, I have decided to make a list that will include some common smarty examples and tips so that it becomes helpful for myself and hopefully for my readers as well :). Even you may find a few tips useful in such a way that you never thought possible or searched with them. But here, you will learn to implement them efficiently. Most of the facts here are based on Smarty version 3.x, which I strongly recommend. So, here we go:
Apply PHP functions On Smarty Variable:
Do you sometimes feel that you need to run some PHP functions, either core or custom, that you wrote yourself on smarty variable/data? I definitely feel it often. We can do this easily, and I found a couple of different ways to do this kind of task:
Using {php} tag: If you are using the smarty 2. x version, then you will be able to use the ‘{php}{/php}’ tag to use core php code as you do on regular PHP files. But, in Smarty version 3.x, this has been deprecated and strongly discouraged. I am also personally not a fan of this, as it may mislead some developers to be dirty and write several PHP codes in it, which a template file doesn’t deserve. Moreover, to me, it really breaks the meaning of using smarty.
Use php functions inside a conditional statement: If you are using conditional statements like if and inside that, you want to apply a PHP function like ‘strstr,’ ’empty,’ etc., then you can apply it usually without any problem. A small code example is:
{if strstr($string1,$string2)}
string matching found
{/if}
Code language: PHP (php)
Apply PHP Function On a Smarty variable: Applying a PHP function on a Smarty variable can be done in a similar way as the Smarty functions. Example:
We have currently {$smarty_array_variable|count} entries in our record.
Code language: PHP (php)
Notice that the above ‘count()’ PHP function takes one parameter, and thus, by default, the smarty data is passed as so. Now, what about if a function takes two parameters? Use the same way Smarty passes extra parameters as below:
{"a long test string"|strstr:"test"}
Code language: JavaScript (javascript)
Foreach Or Section, Which To Use?
Well, if you have seen some basic examples, then you might get confused about which one to use in what type of cases. Let me make you clear about this:
- Most of the time, try your best to avoid ‘section’, use ‘foreach’. It is recommended officially as well.
- ‘section’ can’t deal with associative array index. So, if you are dealing with such array, forget about using ‘section’, ‘foreach’ is your only choice.
- Suppose you are showing a list of members, including their country, city, and zip code. For basic implementation, you may have retrieve those info in separate arrays like ‘$countries’,’$cities’ and ‘$zips’. Now you just loop over the members’ array, retrieve the country/city/zip ID from each member’s data and show them. In such cases, you can consider using ‘section’ or a zip code API. If you need to use the loop data later on some purpose, like whether certain data has been shown in a loop or not, total number of loops iterated etc, then go for section, you won’t get such support in foreach by default, but will in ‘section’. See section function documentation for details.
- If you want to loop in a more incremental way , such as accessing only odd rows/even rows or skipping every 5 data or something like that, go for ‘section’ instead of ‘for each’ for better efficiency.
Smarty Example For Incrementing Variable Value:
In many cases, you may need to use a smarty variable, increment its value, assign it to itself or another one, etc. You will be able to do so in the following code example:
{assign var=myvar value=1}
{assign var=myvar value=$myvar+1}
//alternative way
{assign name=myvar value=1}
{capture assign=myvar}{$myvar+1}{/capture}
Code language: PHP (php)
Using Caching In Smarty:
Well, it is natural to use caching in Smarty. The following code snippet will show you the basic caching operations.
//set cache life time to 5 minutes
$smarty->setCaching(Smarty::300);
// clear out all currently available cache
$smarty->clearAllCache();
//check whether a cache for a template available or not
$smarty->isCached('template.tpl')
Code language: PHP (php)
If you still have some parts of the template which aren’t to be cached while Smarty caching is enabled already, you can use the “{nocache}{/nocache}” tag to get it worked. Data shown inside this tag won’t be cached.
Write Inline JavaScript In Smarty Template:
If you wish to write some inline JavaScript functions inside a Smarty template file, you won’t be able to do it straightforwardly if you are using “{“/”}” symbols, as these are part of Smarty’s directives as well. So, what to do?
Smarty gives a handy use case for this, to simply put an extra “{literal}{/literal}” tag around your JavaScript code. Here is an example:
{literal}
<script language="javascript">
function test_method() {
//do something
}
</script>
{/literal}
Code language: HTML, XML (xml)
Final Words:
This is an ongoing list, and I will add more in the future. I hope the above-mentioned smarty examples and tips will help you to some extent. If you have any suggestions/comments, questions or more tips to contribute, feel free to let me know. Happy smarty programming 🙂
Discover more from CODESAMPLEZ.COM
Subscribe to get the latest posts sent to your email.
thank u for your explanations. how to code in smarty when i wanna increment page view count +1 whenever the page is loaded and the incremented value is updated to mysql database, how to code that in php mysql and smarty tpl? would u help me to code this? thanks in advance