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
23
votes
1 answer

How to convert an array into comma separated strings in smarty template?

I've an array titled $preview_data assigned to smarty template as follows: Array ( [applicable_states] => Array ( [0] => 1 [1] => 3 [2] => 4 [3] => 10 [4] => 11 ) ) Now…
PHPLover
  • 1
  • 51
  • 158
  • 311
21
votes
3 answers

Smarty : substr a variable

How can I print the first n characters of a smarty variable, e.g. the first 30 characters of {$var}?
Rami Dabain
  • 4,709
  • 12
  • 62
  • 106
21
votes
6 answers

Smarty integration into the CodeIgniter framework

A Little Background Information: I've been looking at a few PHP framework recently, and it came down to two. The Zend Framework or CodeIgniter. I prefer CodeIgniter, because of its simple design. It's very bare bone, and it is just kept simple. The…
paan
  • 7,054
  • 8
  • 38
  • 44
20
votes
6 answers

Howto generate json with smarty?

In Smarty, is there a standard function or an easy way to generate json from an array, as json_encode() does in php? I could not see it in Smarty documentation, wanted to ask here.
Sinan
  • 11,443
  • 7
  • 37
  • 48
19
votes
3 answers

Is there a Switch case in smarty?

I am working on a website using Smarty. I have searched the web to find out whether or not I can use switch case with Smarty. But i cannot able to find any useful links for this. Is it possible to use Switch case in Smarty? if so how?
balanv
  • 10,686
  • 27
  • 91
  • 137
19
votes
1 answer

How to auto load a smarty plugin

We have a plugin for a PHP script with Smarty template engine, It can be used in template files with {plugin_name} but that requires making sure this is on every single template file, the question is: is there a way to auto-load this plugin…
Vladimir
  • 1,602
  • 2
  • 18
  • 40
19
votes
6 answers

How to find last index of foreach loop in smarty

How to get last index value of foreach loop in smarty,i m new for smarty I have used this code but its not working {foreach from=$cityList key=myId item=i name=foo} {$i.location_name}{if $main_smarty.foreach.foo.last}
{else}-{/if} …
Bhanu Prakash Pandey
  • 3,805
  • 1
  • 24
  • 16
19
votes
3 answers

Checking for PHP session without starting one?

Is it possible to check for a session with out starting one? The reason that I ask is, the app I am developing has an integrated admin interface. So when an admin is logged in they browse the same pages as the users to make their edits. Fields and…
user73119
  • 2,413
  • 3
  • 21
  • 21
18
votes
2 answers

Concatenation string with variable in smarty

I need in smarty template to transmit to function concatenated string constant with variable value, How can i do it? some example code: {$obj->calledFunc('string const').$var} but . operator doesn't work
hippout
  • 960
  • 4
  • 13
  • 24
18
votes
7 answers

Smarty plugin for NetBeans

I am looking plugin for work with Smarty in NetBeans. I need coloring of my code and normal syntax analysys. I could find only this plugin: Smarty Editor, but I could not make it work. Can you tell me about another plugin for Smarty or teach me…
AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
17
votes
4 answers

Smarty: How to reference to the associative array index

Array $imagelist: Array ( [additional] => Array ( [count] => 2 [image] => Array ( [nokia_e61_1.jpg] => Array ( [name_body] => nokia_e61_1 [name_ext] => jpg ) …
Vlad
14
votes
2 answers

Smarty Modifier filesize

I am using Smarty and one of my section shows file names, including dates, file size, last access etc... I want to display the size of the file in K if less then 1024, in Mb if less than 1048576 etc... The data (file info) comes from the database…
Derek
  • 1,055
  • 9
  • 18
14
votes
4 answers

Using PHP code in Smarty tpl FIle

I am new to smarty and I want to use php code in template file i-e tpl file. I have seen the documentation and searched on google but could not find how to use php code they say we need to configure smarty to allow php execution but could not find…
jawad waheed
  • 308
  • 1
  • 7
  • 16
14
votes
8 answers

Smarty - foreach loop 10 times and stop

Im using the following Smarty code: {foreach from=$entries key=i item=topic} {if $topic.topic_style == question}
  • {$topic.title}
  • {/if} {/foreach} How can i do the {foreach} a…
    CLiown
    • 13,665
    • 48
    • 124
    • 205
    14
    votes
    7 answers

    show a smarty variable with html content

    I have a smarty variable with html content in it like: $html="Content
    etc etc" . I try to show it html-formatted. When showing it like {$html} only plain text appears without formatting. I try like: {$html|unescape} but then…
    user985409
    • 1,355
    • 3
    • 9
    • 18