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

Check if key exists in smarty array

I have an array that looks like this: array( 'name1' => array('city1', 'city2', 'city3'), 'name2' => array('city1', 'city4'), 'namen' => array('city1', 'city7', 'cityn') ); Which is passed to smarty as : $my_names There is a loop in my…
Hommer Smith
  • 26,772
  • 56
  • 167
  • 296
9
votes
4 answers

Undefined font: In Fpdf

I'm New to Fpdf library, i need to create a pdf from data base in smarty. i have checked the data from data base is fine, when pass the font name the below error was show Warning: in_array() expects parameter 2 to be array, null given in…
mohan
  • 453
  • 1
  • 5
  • 17
9
votes
3 answers

Smarty PHP clashing with AngularJS

How do I stop Smarty throwing an error when I'm using AngularJS in the same template. I have a Smarty page template with this code:
  • {{i}}

  • And I'm getting a blank page when I view in a…
    Pete
    • 4,542
    • 9
    • 43
    • 76
    9
    votes
    3 answers

    Should I use smarty template engine with MVC like Yii?

    I used smarty in past, in x-cart, jobberbase and jomestate component of Joomla and some other Joomla components as well. I saw that things were very much tightly coupled in x-cart and many things seems to be more complicated than they should be in…
    Hafiz
    • 4,187
    • 12
    • 58
    • 111
    8
    votes
    11 answers

    how to use Smarty better with PHP?

    I found that using Smarty with PHP, sometimes extra time will need to be used for 1) using quite different syntax than PHP itself 2) need to check small cases, because documentation doesn't give finer details, such as for…
    nonopolarity
    • 146,324
    • 131
    • 460
    • 740
    8
    votes
    3 answers

    PHP filemtime function - "stat failed for"

    I have a problem with PHP filemtime function. In my webapp I use Smarty template engine with caching option. In my webapp I can do some actions which generate error, but lets focus on only one action. When I click link on page some content is…
    lbednaszynski
    • 678
    • 2
    • 12
    • 24
    8
    votes
    3 answers

    disable smarty notice

    I would like to disable smarty-s notices. This exactly: Notice: Undefined variable: xy Because I know that some variables are undefined and in some cases I don't even want to define them. BUT I don't want to disable other PHP notices. Thanks for…
    user669677
    8
    votes
    2 answers

    Smarty and Kohana

    Is there a standard "official" way of using Smarty with Kohana 3? I see there are some options that seem less than ideal and will probably break when either Smarty or Kohana's minor version number increases. (as a side question, is it a good idea to…
    cambraca
    • 27,014
    • 16
    • 68
    • 99
    8
    votes
    10 answers

    Smarty benchmark, anyone?

    I am considering Smarty as my web app templating solution, and I am now concerned with its performance against plain PHP. The Smarty site says it should be the same, however, I was not able to find anyone doing real benchmarking to prove the…
    gnosis
    • 929
    • 3
    • 14
    • 24
    8
    votes
    4 answers

    JavaScript variable and Smarty

    I am trying to do some JavaScript with Smarty and using JavaScript variable and checking if the value is contained within a Smarty array. Below is the code that I have: if (object.data[i].Application === selectedApplication || {if…
    Boardy
    • 35,417
    • 104
    • 256
    • 447
    8
    votes
    3 answers

    How to fix error "Fatal error: Uncaught --> Smarty: unable to write file"?

    I migrated Prestashop from localhost to website domain using digitalocean, but when I open this website I get this error Fatal error: Uncaught --> Smarty: unable to write file…
    M. Johnson
    • 839
    • 6
    • 13
    • 22
    8
    votes
    3 answers

    How to get COOKIE in Smarty

    When User already login, i want to show some link in page <{if(!empty($_COOKIE['USER']))}> TEXT A <{else}> TEXT B <{/if}> but does not work Found the answer myself,…
    prieku
    • 145
    • 2
    • 2
    • 8
    8
    votes
    5 answers

    How to fetch the data and display it in a browser simultaneously using PHP and Smarty?

    I'm using PHP, MySQL, Smarty, jQuery, AJAX, etc. for my website. Currently, I'm fetching a large amount of data (matching question IDs) from the MySQL database, do processing on it, assigning this data to the Smarty template and printing it on a…
    PHPLover
    • 1
    • 51
    • 158
    • 311
    8
    votes
    6 answers

    How to highlight tpl file?

    I'm using NOTPAD++ and I wonder how I can highlight codes in tpl files. I googled online and there are answers but none of them works. For intance, http://sourceforge.net/p/notepad-plus/discussion/331753/thread/3067a53d/ Any ideas? Or any other…
    Run
    • 54,938
    • 169
    • 450
    • 748
    8
    votes
    2 answers

    How to parse/decode JSON object in smarty template?

    I have the following code in my template file: {foreach from=$items item=entry}
        {$entry->nb_persons|@print_r}
      
    {/foreach} The output is (json…
    cookie669
    • 83
    • 1
    • 1
    • 3