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
6
votes
2 answers

Get first Smarty Array-Element

How can i get the first smarty array element ? Actually i know how to do this... but i have following issue. I get a array passed that looks like this [DATA] => Array ( [12] => Array ( …
Dwza
  • 6,494
  • 6
  • 41
  • 73
6
votes
4 answers

Call to a member function on a non-object

I'm working through Practical Web 2.0 Appications currently and have hit a bit of a roadblock. I'm trying to get PHP, MySQL, Apache, Smarty and the Zend Framework all working correctly so I can begin to build the application. I have gotten the…
Matt
  • 523
  • 1
  • 5
  • 13
6
votes
2 answers

How to use str_replace in Smarty

I'm using PHP smarty to declare a link : <{foreach item=list key=num from=$product}>
  • <{$list.somename}>
  • <{/foreach}>` and the resulting link when mouseover is : "http:/domain/some…
    prieku
    • 145
    • 2
    • 2
    • 8
    6
    votes
    3 answers

    smarty convert string to number

    I need to do deviding: {math equation="x/y" x=$x y=$y} // $x = '2', $y = '3' How to convert strings to numbers exactly in Smarty, coz I don't have access to proprietary code of CMS? Thanx! UPD: Smarty version: 2.6.18
    Sergey Kudryashov
    • 713
    • 2
    • 9
    • 30
    6
    votes
    5 answers

    How to generate year in Smarty?

    How can i generate a select list with the given year till this year? i did this {assign var=thisyear value=$smarty.now|date_format:"%Y"} {if !$firstyear} {assign var=firstyear value="2003"} {/if} {if !$loop}{assign var=loop…
    streetparade
    • 32,000
    • 37
    • 101
    • 123
    6
    votes
    2 answers

    how to print an array in smarty?

    I have an array: $team_details = Array ( [id] => 1 [name] => doge_finder [total_rewards] => 52.00524500 [desciption] => team is only of doge miners [created_by] => 20 ); /* assigning to a smarty template */ $smarty->assign("team_record",…
    codeBYmahesh
    • 163
    • 2
    • 5
    • 15
    6
    votes
    2 answers

    Break in Smarty's / Dwoo's foreach

    About break foreach ( $data as $k => $v ) { if ( $k == 4 ) { break; } } every one knows. Is there something similar in Smarty's or Dwoo's {foreach} function ?
    hsz
    • 148,279
    • 62
    • 259
    • 315
    6
    votes
    10 answers

    DOMPDF: Unable to stream pdf: headers already sent

    I am generating php page using smarty template based on html form input and uploaded files. Using dompdf, I want to save the generated page as a pdf file. When the user submits the multipart/form-data, data is posted to itself. Then it undergoes…
    sridhar
    • 1,321
    • 5
    • 17
    • 26
    6
    votes
    4 answers

    Smarty outputs blank page

    SOLVED: Error in template file I have Smarty setup like this: require_once 'smarty/Smarty.class.php'; $smarty = new Smarty(); $smarty->compile_dir = $compile_dir; $smarty->template_dir = $tpl_dir; That's all I should need for now... I have Smarty…
    TheMagician
    • 1,846
    • 7
    • 20
    • 26
    6
    votes
    3 answers

    URL encode with smarty

    I want to encode an url with smarty. $var contains my datas url
    user2261430
    6
    votes
    5 answers

    Smarty how to get a first index from foreach?

    Construction is this: {if !empty($userObjects)}