Posts

Showing posts with the label smarty

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

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. ...

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

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}

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