Questions tagged [smarty]

Smarty is a templating engine for PHP. It allows for easy separation of application logic and display code, as well as simplifying reuse of templates.

Overview

Smarty templating engine for PHP lets easily separate application logic and presentation, the first being in PHP code and the other generally, but not always, in HTML. That way a separation of PHP and HTML code is encouraged.

Currently two versions of Smarty are maintained for legacy purposes that is compatible with PHP 7.2, and that is compatible with PHP 7.3 and PHP 7.4.

As of April 13th 2020, current Smarty releases are 3.1.35 and 2.6.31

Example

PHP script (example.php) :

require_once('../smarty/Smarty.class.php');
$se = new Smarty();

$se->assign('pi', 3.14159);
$se->assign(array(
    'title' => 'Hello World !',
    'today' => date('d/m/Y'),
));

$se->display('example.tpl');

Template (example.tpl) :

<!DOCTYPE html>
<html>
  <head>
    <title>Smarty example</title>
  </head>
  <body>
    <p>{$title}</p>
    <p>Pi value : {$pi}</p>
    <p>Today is : {$today}</p>
  </body>
</html>

Links:

4490 questions
8
votes
1 answer

round in Smarty shows wrong result

In my Smarty 3 template I have this code: {$a=8.34} {$b=8.33} {$a-$b|round:2} Expected result is: 0.01 But I receive this: 0.0099999999999998 Does anybody know how to fix this?
Yekver
  • 4,985
  • 7
  • 32
  • 49
8
votes
2 answers

smarty passing the value from {math} equatio to a variable

Have a nice day everyone, I have something to ask your hel, to better understand here is my code: {math equation=((($order_total-$commission)+$discount+$delivery_charge)*$rate)} I want that to be pass to another variable,in php I want to be like…
Noel Balaba
  • 183
  • 2
  • 11
7
votes
2 answers

smarty section loop

I am trying to get a loop for following... {$product.min_val} //2 {$product.max_val} //8 and i am trying following... {section name=val start=$product.min_val loop=$product.max_val step=0}
seoppc
  • 2,766
  • 7
  • 44
  • 76
7
votes
7 answers

How to make a java script array from php array?

Iam using PHP,Smarty configuration. Iam getting an array in PHP and sent it to tpl.I would like to make a javascript array in tpl from that php array. PHP ARRAY Array ( [0] => Array ( [dt] => 2011-12-02 [number]…
user1029108
  • 87
  • 1
  • 2
  • 3
7
votes
2 answers

Counter inside iterate smarty loop

I have this smarty code : {iterate from=fruits item=fruit} .... {/iterate} I want to have a counter inside this loop that accept a start value and increase by one until the loop continues. I should i use? i am not good in smarty. Thank you.
hd.
  • 17,596
  • 46
  • 115
  • 165
7
votes
2 answers

How to write my very own Smarty If-case

I am writing my own administration and, off course I am using Smarty. Now, what I would like to do is to add a access check function much similar to the {if} tag. What I would like to write is: {userAccess module='MyModule'…
Paul Peelen
  • 10,073
  • 15
  • 85
  • 168
7
votes
3 answers

Smarty - variable addition

I want to add a constant value to a variable in smarty. just like: {assign var='c' value='0'} $c=$c+1
RSK
  • 17,210
  • 13
  • 54
  • 74
7
votes
4 answers

Apply Smarty modifier on block output

I'm trying to apply a modifier (truncate, in my case) to the output of a block (a tr block, that is, a translation block). I don't have tr as a modifier because it's not convenient for HTML markup. I don't really know what kind of syntax I should…
Lazlo
  • 8,518
  • 14
  • 77
  • 116
7
votes
2 answers

Smarty "capitalize" modifier... always capitalizes "L"? ... and "P"?

Quite simply: I'm using Smarty and the |capitalize modifier. It works fine, but when I pass any word with l in it, it capitalizes it, even if it's not at the beginning of the word. What why? EDIT: Same happens with…
Lazlo
  • 8,518
  • 14
  • 77
  • 116
7
votes
2 answers

Smarty indentations and code format

can I make a good looking HTML with Smarty? I mean if I take this pattern (it's not a working code, just an example):
{{assign var="i" value="0"}} {{assign var="tab" value="0"}} {{foreach from=$contact.comments…
llamerr
  • 2,997
  • 4
  • 29
  • 40
7
votes
1 answer

Smarty registerPlugin function as argument

In Smarty, it is possible to register an plugin in this way: $smarty->registerPlugin("function","date_now", "print_current_date"); function print_current_date($params, $smarty) { if(empty($params["format"])) { $format = "%b %e, %Y"; } else…
tiefenb
  • 732
  • 3
  • 16
  • 32
7
votes
3 answers

Smarty: print a specific element of an array WITHOUT using foreach loop

{foreach from=$myArray item=item} {$item.attribute} {/foreach} instead of printing all attributes of each element of the array, I want to output only the 3rd element WITHOUT using a foreach loop, is it possible? I'm looking for something like…
evilReiko
  • 19,501
  • 24
  • 86
  • 102
7
votes
4 answers

Updating theme.yml in PrestaShop

So I'm trying to modify the layout of theme.yml in a PrestaShop theme. I comment out the following: hooks: modules_to_hook: displayFooter: # - ps_linklist # - ps_customeraccountlinks - ps_contactinfo And then I…
Jesse Lynn
  • 325
  • 5
  • 15
7
votes
6 answers

Smarty: how to use PHP functions?

Say I have the following in my TPL file: {$a} and I want to apply certain PHP native functions (e.g. strip_tags) to that Smarty variable. Is this possible within the TPL? If so, how?
StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441
7
votes
6 answers

Smarty permissions problems

Smarty is complaining about permissions. I've written a small mvc framework for a project I'm working on and I need to be able to render templates in each controller. I went ahead and followed the Smarty installation instructions, and set all of the…
afkbowflexin
  • 4,029
  • 2
  • 37
  • 61