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

Ajax how to load some scripts

I have the following code, that loads to an unerdored list content, depending on how much span there is with information: $(document).on('click','#addplaylisttolist',function(){ myPlaylist.pause(); myPlaylist.remove(); …
Nmaster88
  • 1,405
  • 2
  • 23
  • 65
10
votes
2 answers

How to set HTML code in "srcdoc" attribute of iframe?

As shown in below picture I am using iframe inside div#email_content to show exact preview of email content. I am trying to use srcdoc attribute to load email content in…
aagjalpankaj
  • 1,160
  • 1
  • 15
  • 25
10
votes
1 answer

how to make smarty's display function return to a variable

In smarty is it possible or is there any kind of function to render a template and return the result? for example: $rendered_content = $smarty->render("content.html")
user327396
  • 381
  • 2
  • 7
10
votes
3 answers

Smarty in_array value

I have array like this { "sCode":"05", "sCodeName":"critical_tight_connection", "iSeverity":1, "aData":{ "iLevelOfDetailt":2, "iDuration":35, "sLabel":"Labai trumpas pers\u0117dimas, 35 min.", …
Itsmeromka
  • 3,621
  • 9
  • 46
  • 79
9
votes
3 answers

Smarty: check if variable is in array

I'm using php with smarty. In php I have two arrays: $code = Array ( [n_id] => 1 [t_code] => ABC123 [t_description] => Test code [b_enabled] => Yes [n_type] => 3 [dt_start] => [dt_end] => [n_min_req_gbp] => 0 …
Aleks G
  • 56,435
  • 29
  • 168
  • 265
9
votes
1 answer

Commenting/skipping markup with Smarty

Is there a smarty tag that allows skipping content in a template without the enclosed code showing up in the rendered HTML page?
James P.
  • 19,313
  • 27
  • 97
  • 155
9
votes
4 answers

About ![CDATA[, what is it?

Possible Duplicate: what is the meaning of CDATA What is, and when is used the ![CDATA[ string/variable/constant? (I don't know what is it)
ibito
  • 161
  • 2
  • 8
9
votes
4 answers

Smarty getting substring of a var with strpos as start and strlen as end

I am having issue formatting variables in smarty. I was wondering what is the best way to do it. Basically i have a string "ABC | DEFGH" i want smarty to get the substring of "DEFGH" How would i go about doing…
KJYe.Name
  • 16,969
  • 5
  • 48
  • 63
9
votes
4 answers

What is the best way to handle recursion in smarty?

I found a couple of ways to handle recursion in Smarty, mostly based on including templates into themselves, which seems like ridiculous waste of resources. I found one solution, by Messju over at Smarty that seemed to be just right - but it is not…
Jacob Hansson
  • 149
  • 3
  • 7
  • 19
9
votes
0 answers

WebStorm Smarty highlighting

Is there an way to make WebStorm highlight Smarty templates? Either through plugin or some other means? Also note, I need this for WebStorm, not PhpStorm or some other version; I don't need autocomplete or anything else -- just highlights. Files…
Miroslav Saracevic
  • 1,446
  • 1
  • 13
  • 32
9
votes
3 answers

How do I check to see if a Smarty variable is already assigned?

How do I check to see if a particular value has already been assigned to Smarty and if not assign a (default) value? Answer: if ($this->cismarty->get_template_vars('test') === null) { $this->cismarty->assign('test', 'Default value'); }
GloryFish
  • 13,078
  • 16
  • 53
  • 43
9
votes
6 answers

Find where a variable is defined in PHP (And/or SMARTY)?

I'm currently working on a very large project, and am under a lot of pressure to finish it soon, and I'm having a serious problem. The programmer who wrote this last defined variables in a very odd way - the config variables aren't all in the same…
Jon
  • 305
  • 3
  • 20
  • 45
9
votes
6 answers

Smarty foreach counter , reset after 3 element

I want to create foreach smarty loop with counter and 3 "if" conditions inside. After my counter value is more than 3 I want to reset counter value and get back to first condition of If This is my code {foreach $itemscollection as $singleitem…
woj_jas
  • 1,092
  • 7
  • 23
  • 50
9
votes
3 answers

Best way to use Multiple Pages on Smarty

Is this the most effective way to use smarty with multiple pages?: if (empty($_GET[page])) { $template = "home.tpl"; $smarty->assign('pagename', ' - Home'); } else { $page = $_GET["page"]; switch ($page) { case "home": …
user2079701
9
votes
1 answer

smarty for loop unrecognised tag

I just started using Smarty today so I may be doing this completely wrong, but I can't seem to find a solution to the problem. I'm trying to get a for loop to iterate 4 times, incrementing $i on each iteration. Pretty standard stuff. According to…
Kaizen9001
  • 510
  • 1
  • 4
  • 11