PHP Coding Tips

Showing posts with label smarty. Show all posts
Showing posts with label smarty. Show all posts

Thursday, March 28, 2013

Sample smarty loop with images

If you want to print several images on your site, you must assign array of those images to smarty variable. Then, you can iterate over this array in smarty and show img tags for each image. Rember about good path:{foreach from=$sm_photos item=item} <img src="/content/photophoto/1/{$item.file}"> {/foreach}If your path will not target image file, you can get 404 error, but typically image will be just hidden

Wednesday, March 20, 2013

How to do SMARTY foreach loop to present array values from PHP?

SMARTY is a very good template engine that helps you decompose application view and model. If you want to create a SMARTY foreach loop, you need to have assigned array first. If you want only to present array values, then it can be a usual PHP array without special keys. But SMARTY foreach, same as foreach in PHP, can present keys for array items. The basic construction of foreach loop in smarty, together with PHP array assigment to present in view, you can see below: // In PHP: $array = new array( 'key1' => 'val1' , 'key2' => 'val2' ); $smarty->assign('someAssignedArray' , $array ); // In SMARTY: {foreach from=$someAssignedArray key=keyOfItem item=itemValue} The key of this item is: {$keyOfItem}<br> The value of this item is: {$itemValue}<br> {/foreach}As you can see, we have to have defined array first. Example above shows custom keyed array, but this array can also have standarized default keys like 0,1,2,3,4 etc. The first step is to assign our array to SMARTY variables, because SMARTY doesn't have access to global PHP scope in nice way. So we must define a custom name for this variable -in example above i named it 'someAssignedArray'. So, the array wich in PHP exeist as $array, will exist in smarty as $someAssignedArray.
Now, you can write a foreach loop in your tpl file. The basic atribute of SMARTY foreach tag is from. This attribute says, from wich array you want to pick up all items for iteration. Remeber that this variable must have a dollar sign at begining.
Second required parameter in this loop construction is item. Item refers to name of variable, wich will have a value from array inside the loop. Key attribute works in the same way - it will contains key for item value in single loop iteration. There is some important thing - remeber that you create a new SMARTY varaibles here, so do not use dollar sign.
Inside the foreach loop, you can access your array item value and key by varaible names you specified as foreach tag attributes, this time begininnig with a dollar sign.
Of course, if yours item value is an array, nothings stop you to provide a nested foreach loop, with new key and value variables. This is useful thing.
If you want to print your array in fastest way, only for debug etc. you can try to use print_r function in your SMARTY tpl file. I wrote some nice tutorial about using print_r in SMARTY, so check itd.
If you want to provide additional filtering when iterating over items, see my tutorial about modifiers here.

Wednesday, January 30, 2013

Best way to secure e-mail in SMARTY template and CSS

See the best way to secure your e-mail from spambots. Just rearrange text flow to reverse and reverse an email with strrev() function:<span style="unicode-bidi:bidi-override; direction: rtl;"> {$sm_item.email|strrev} </span> Now you have your e-mail backwarded in page source, but displayed properly

Wednesday, August 8, 2012

How to pass value to included smarty template

You can pass a variable value to a smarty template while including it. Just type name of a variable as parameter and value in quotas:{include file="fielderror.tpl" fieldname="albums"}Now in fielderror.tpl you can access this variable just by {$albums}

Tuesday, August 7, 2012

print_r in smarty - How to print your array contents in smarty template.

You can print a array contents like in php print_r() function in smarty tempalte. The common mistake is to forget about second print_r parametr for returning it's output. This parameter have to be set to true. If you forget to set it to true, your array contents propably will be somewhere at begining of a content. This is the solution:
{$myArray|@print_r:true}
Notice the @ symbol that provides parsing argument as array, not as string.

Filter smarty variable in .TPL file for new lines to br tag

You can filter any smarty variable by using allowed php function as a modifier. For example to put BR tags instead new lines you can do this in smarty template:{$content|nl2br}Good way to output filter or (if you provide own modifier) you can format dates etc.

Smarty variable value in quotation mark

if you want to insert smarty variable value into string that is already in smarty "variable" (function call), just append it with `......` like shown in this example:{$obj->myMethod("This is my value: `$value`")}Usefull for owne link-preprocessor/rewriter