Jump to content

Smarty (template engine): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
Adding local short description: "Web template system", overriding Wikidata description "Web template system in PHP"
 
(18 intermediate revisions by 16 users not shown)
Line 1: Line 1:
{{Short description|Web template system}}
{{About||the 1934 film|Smarty (film)|the Broadway musical originally titled Smarty|Funny Face (musical)|similarly named items|Smarties (disambiguation)}}
{{About|a web template system|other uses|Smarty (disambiguation){{!}}Smarty|similarly named items|Smarties (disambiguation)}}

{{Infobox software
{{Infobox software
| name = Smarty
| name = Smarty
| logo = [[Image:Smarty-logo.png]]
| logo = Smarty-logo.png
| developer = Monte Ohrt, Messju Mohr, Uwe Tews
| developer = Monte Ohrt, Messju Mohr, Uwe Tews
| programming language = [[PHP]]
| programming language = [[PHP]]
Line 8: Line 10:
| license = [[GNU Lesser General Public License|LGPL]]
| license = [[GNU Lesser General Public License|LGPL]]
| website = {{URL|www.smarty.net}}
| website = {{URL|www.smarty.net}}
| latest release version = {{wikidata|property|edit|reference|P348}}
| latest_release_version = 3.1.30
| latest release date = {{start date and age|{{wikidata|qualifier|P348|P577}}}}
| latest_release_date = {{Release date and age|2016|08|14|df=yes}}<ref>https://github.com/smarty-php/smarty/blob/v3.1.30/change_log.txt</ref>
| latest_preview_version =
| latest_preview_version =
| latest_preview_date =
| latest_preview_date =
Line 19: Line 21:


== Smarty example ==
== Smarty example ==
Since Smarty separates PHP from HTML, there are two files — one contains the presentation code: an HTML template, including Smarty variables and tags - <source lang="smarty" enclose="none">{$title_text|escape} {$body_html}</source> - which might look like this:
Since Smarty separates PHP from HTML, there are two files — one contains the presentation code: an HTML template, including Smarty variables and tags - <syntaxhighlight lang="smarty" inline>{$title_text|escape} {$body_html}</syntaxhighlight> - which might look like this:
<source lang="html+smarty">
<syntaxhighlight lang="html+smarty">
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<html lang="en">
Line 32: Line 34:
</body> <!-- this is a little comment that will be seen in the HTML source -->
</body> <!-- this is a little comment that will be seen in the HTML source -->
</html>
</html>
</syntaxhighlight>
</source>


The [[business logic]] to use the Smarty template above could be as follows:
The [[business logic]] to use the Smarty template above could be as follows:
<source lang="php">
<syntaxhighlight lang="php">
define('SMARTY_DIR', 'smarty-2.6.22/');
define('SMARTY_DIR', 'smarty-2.6.22/');
require_once(SMARTY_DIR . 'Smarty.class.php');
require_once(SMARTY_DIR . 'Smarty.class.php');
Line 47: Line 49:


$smarty->display('index.tpl');
$smarty->display('index.tpl');
</syntaxhighlight>
</source>


==Further reading==
==Further reading==
Line 53: Line 55:


== See also ==
== See also ==
{{Portal|Free software}}
{{Portal|Free and open-source software}}


* [[Comparison of web template engines]]
* [[Comparison of web template engines]]

Latest revision as of 10:11, 24 January 2023

Smarty
Developer(s)Monte Ohrt, Messju Mohr, Uwe Tews
Stable release
5.4.2[1] Edit this on Wikidata / 21 November 2024; 28 days ago (21 November 2024)
Repository
Written inPHP
TypeTemplate Engine
LicenseLGPL
Websitewww.smarty.net

Smarty is a web template system written in PHP. Smarty is primarily promoted as a tool for separation of concerns.[2] Smarty is intended to simplify compartmentalization, allowing the front-end of a web page to change separately from its back-end. Ideally, this lowers costs and minimizes the efforts associated with software maintenance.

Smarty generates web content through the placement of special Smarty tags within a document. These tags are processed and substituted with other code. Tags are directives for Smarty that are enclosed by template delimiters. These directives can be variables, denoted by a dollar sign ($), functions, logical or loop statements. Smarty allows PHP programmers to define custom functions that can be accessed using Smarty tags.

Smarty example

[edit]

Since Smarty separates PHP from HTML, there are two files — one contains the presentation code: an HTML template, including Smarty variables and tags - {$title_text|escape} {$body_html} - which might look like this:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8">
   <title>{$title_text|escape}</title>
</head>

<body> {* This is a little comment that won't be visible in the HTML source *}
{$body_html}
</body> <!-- this is a little comment that will be seen in the HTML source -->
</html>

The business logic to use the Smarty template above could be as follows:

define('SMARTY_DIR', 'smarty-2.6.22/');
require_once(SMARTY_DIR . 'Smarty.class.php');

$smarty = new Smarty();
$smarty->template_dir = './templates/';
$smarty->compile_dir = './templates/compile/';

$smarty->assign('title_text', 'TITLE: This is the Smarty basic example ...');
$smarty->assign('body_html', '<p>BODY: This is the message set using assign()</p>');

$smarty->display('index.tpl');

Further reading

[edit]
  • Hasin Hayder; J. P. Maia; Lucian Gheorghe (2006). Smarty PHP Template Programming And Applications. ISBN 978-1-904-81140-4.

See also

[edit]

References

[edit]
  1. ^ "Release 5.4.2". 21 November 2024. Retrieved 29 November 2024.
  2. ^ Parr, Terence John (2004). Enforcing strict model-view separation in template engines. Proceedings of the 13th international conference on World Wide Web. ISBN 1-58113-844-X.
[edit]