Smarty (template engine)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
Smarty
DevelopersMonte Ohrt, Messju Mohr, Uwe Tews
Initial release2000 (2000)[1]
Repository
  • {{URL|example.com|optional display text}}Lua error in Module:EditAtWikidata at line 29: attempt to index field 'wikibase' (a nil value).
Written inPHP
Engine
    Lua error in Module:EditAtWikidata at line 29: attempt to index field 'wikibase' (a nil value).
    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 | edit source]

    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 | edit source]
    • Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).

    See also

    [edit | edit source]

    Lua error in mw.title.lua at line 392: bad argument #2 to 'title.new' (unrecognized namespace name 'Portal').

    References

    [edit | edit source]
    1. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    2. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    [edit | edit source]