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
186
votes
14 answers

How to Debug Variables in Smarty like in PHP var_dump()

I have some variables inside a template and I don't know where I assigned them. I need to know what is inside a particular variable; for instance, say I have a variable in smarty called member. I tried with {debug} but it didn't work, and no popup…
streetparade
  • 32,000
  • 37
  • 101
  • 123
110
votes
10 answers

What are .tpl files? PHP, web design

Someone wants me to redesign a site run in PHP (VideoCMS). But when I asked him to send me the source he has given me *.tpl files instead of *.php. There is some code inside them: {include file='header.tpl' p="article"}
Dan
  • 55,715
  • 40
  • 116
  • 154
61
votes
25 answers

Why should I use templating system in PHP?

Why should I use templating system in PHP? The reasoning behind my question is: PHP itself is feature rich templating system, why should I install another template engine? The only two pros I found so far are: A bit cleaner syntax…
Josef Sábl
  • 7,538
  • 9
  • 54
  • 66
45
votes
4 answers

How do you increment an assigned variable in smarty without displaying it

So I have an assigned variable in smarty: {assign var=number value=0} Now I can increment it using {$number++} or {++$number} Which is exactly what I need, only problem is, it displays the value of $number on the page. Is there a way I can…
Francis Lewis
  • 8,872
  • 9
  • 55
  • 65
43
votes
3 answers

Smarty local variable concatenation with string

How to assign a local template variable with a string concatenated just like below: {$yes_src=const1.'yes'.const2} to be used below in the code in the manner {$yes_src}. By the way I am looking for a job as PHP developer :)
algorytmus
  • 953
  • 2
  • 9
  • 28
41
votes
5 answers

How to assign an array within a smarty template file?

I was wondering if it was possible to assign an array to a variable within a Smarty template file? I have tried this {assign var='file' value = array('dir','doc','exe')} But when I print out the array it produces…
Jenski
  • 1,458
  • 1
  • 18
  • 29
40
votes
3 answers

How can I get current URL using smarty

How to get URL using Smarty similar to window.location in JavaScript ? I did this {$smarty.server.PHP_SELF} but it's not working.
user1187
  • 2,116
  • 8
  • 41
  • 74
37
votes
4 answers

Print all variables available in a Smarty template

How do you print all variables available in the context of a Smarty template? Something like the Django debug trace that lists everything being passed. Thanks
Lorenzo
  • 4,558
  • 11
  • 44
  • 54
36
votes
3 answers

Formatting numbers with thousands separator Smarty PHP

Im trying to format numbers with thousands separator Smarty. So for example 1000 becomes 1,000. Thanks.
babadbee
  • 863
  • 5
  • 14
  • 18
31
votes
4 answers

How can I check if a variable exists in Smarty?

I'm using Smarty template engine. I'm doing a simple login page. I set a variabile named error with a message if there are some problems, but IF NOT I get: Notice: Undefined index: error How could I check if this variable exists? I only do: {if…
Dail
  • 4,622
  • 16
  • 74
  • 109
31
votes
4 answers

Is it possible to format a number to 2 decimal places with Smarty PHP

Is it possible to format a number to 2 decimal places with Smarty PHP? Thanks.
plosww1
  • 343
  • 1
  • 3
  • 7
30
votes
3 answers

{if not isset} Smarty

what is the code for smarty for if (!isset($var)){ ? if using {if $x eq '5'} when $x is not defined in smarty , it gives an error function call 'get_template_vars' is unknown or deprecated. . this is what i believe so far as i lost hope in trying to…
Rami Dabain
  • 4,709
  • 12
  • 62
  • 106
29
votes
3 answers

Concatenation in smarty

I want to assign the value obtained from the concatenation of these two variables with a string. {assign var="url" value="{$WS_PATH}aircraft_images/{$images[i].image}"} Please let me know how can we do this in smarty.
user1163513
  • 4,087
  • 7
  • 24
  • 25
25
votes
1 answer

Get total number of items in array

How can I get the total number of items in an array using Smarty? Can I use {$smarty.foreach.foo.total}?
conbask
  • 9,741
  • 16
  • 57
  • 94
23
votes
5 answers

Create an array in smarty template?

I need to create a new array from other one dimensional array in smarty template. So, what are the best possibilities to create an array in template file. Thanks, Sachin
sachin sawant
  • 231
  • 1
  • 2
  • 3
1
2 3
99 100