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

get value count in an array with smarty

I have an array called $mydata that looks like this: Array ( [0] => Array ( [id] => 1282 [type] =>2 ) [1] => Array ( [id] => 1281 [type] =>1 ) [2] => Array ( [id] => 1266 …
Phil
  • 1,719
  • 6
  • 21
  • 36
13
votes
1 answer

Break/explode String in smarty

can some 1 tell how to break this sort of String in smarty what i want is "1" as price and "dollar" as currency type. "1|dollar" please reply.
temp-learn
  • 527
  • 2
  • 9
  • 31
13
votes
6 answers

How can I increment a Smarty variable?

I am not usually a Smarty guy, so I'm a bit stuck. I want to echo the index of an array, but I want to increment it each time I echo it. This is what I have...
    {foreach from=$gallery key=index item=image}
  • alex
    • 479,566
    • 201
    • 878
    • 984
13
votes
3 answers

Quitting Smarty to do it manually

I am facing the problem that I'm not really sure how to develop without a framework or a template engine. I started coding that way and now I want to go to basics. I used to work with this MVC schema, using Codeigniter and Smarty as a template…
Limon
  • 1,772
  • 7
  • 32
  • 61
13
votes
6 answers

smarty and date

i get date with: {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'} But how get 20 day after? If now: 2010 05 05 12:12:12, I wish to show 2010 25 05 12:12:12
lolalola
  • 3,773
  • 20
  • 60
  • 96
13
votes
4 answers

How to put HTML data into header of tcpdf?

I'm using the tcpdf library to generate the pdf document. I'm using smarty template engine to hold the data. Below is the script to put in the header data: // set default header data $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'PQR',…
PHPLover
  • 1
  • 51
  • 158
  • 311
12
votes
3 answers

Caching a PHP Array

My problem is im creating a large nested PHP array which is parsing information from multiple external sources. On the first return I would like to cache this data. Im pretty new to caching so don't really know what I should be looking for, any…
fl3x7
  • 3,723
  • 6
  • 26
  • 37
12
votes
2 answers

How can define variable inside smarty template file?

How can i define variable not in PHP but in .TPL? (smarty template file)
János
  • 32,867
  • 38
  • 193
  • 353
12
votes
2 answers

How to define a variable with random values in Smarty

I need to assign a variable which will be used to create the id label for some html elements. And it needs to be unique. I tried {assign var=unique_id value=`10|mt_rand:20`} and {math equation='rand(10,100)'} But I don't know how to use the…
Daniel Luca CleanUnicorn
  • 1,087
  • 1
  • 12
  • 30
12
votes
5 answers

How to position the text next to images properly in PDF?

I'm using PHP, Smarty and TCPDF library to generate the PDF copy of a document. The document contains images of mathematical expressions from WIRIS editor along with the text content. I'm having a problem positioning the text coming next to the…
PHPLover
  • 1
  • 51
  • 158
  • 311
11
votes
5 answers

Pure PHP/HTML views VS template engines views

I would like to know which approach is faster, using the pure PHP in the HTML files or using a template engines like Smarty,Twig, ... What I would particularly like to know is next: which is parsed faster, is the Smarty cache for example faster…
DarkSideOfTheMoon83
  • 702
  • 1
  • 8
  • 17
11
votes
2 answers

Smarty If URL Contains

Using Smarty Tags I'd like to determine if an URL contains a word, something like: {if $smarty.get.page contains "product.php"} ..... I know contains doesn't exist, but how could I easily go about writing something similar to achieve the above…
99823
  • 2,407
  • 7
  • 38
  • 60
11
votes
4 answers

How to convert float to int in smarty

As i am new to smarty, I am not at able to convert floating number to int. Ex: 12.234 => 12 please help me if u find any solution
sandeep
  • 2,862
  • 9
  • 44
  • 54
10
votes
3 answers

How to check iteration in smarty?

How can I check the current iteration for foreach and do something? {foreach $new_products as $product name=foo} {if $smarty.foreach.foo.iteration=5} Do it! {/if} {/foreach} This always return not checked
skywind
  • 892
  • 6
  • 22
  • 44
10
votes
5 answers

Smarty : evaluate a template stored in a PHP variable

i have a php variable which has html/smarty code in it $x='{$title}'; This data is fetched from database , i want to evaluate it using smarty and put the output into a php variable (to print it out or to save it to the…
Rami Dabain
  • 4,709
  • 12
  • 62
  • 106