{"id":1420,"date":"2018-01-19T19:22:49","date_gmt":"2018-01-19T19:22:49","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/smarty-php-template-engine-building-php-apps-in-a-flash\/"},"modified":"2018-01-19T19:24:46","modified_gmt":"2018-01-19T19:24:46","slug":"smarty-php-template-engine-building-php-apps-in-a-flash","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/smarty-php-template-engine-building-php-apps-in-a-flash\/","title":{"rendered":"Smarty PHP Template Engine: Building PHP Apps in a Flash"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Octavia Andreea Anghel<\/div>\n<div class=\"\">on August 10, 2011<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<p>Smarty is a template engine for PHP whose main goal is to facilitate a very useful way to separate the business logic from the presentation logic. This article introduces Smarty and demonstrates how to install the template, create an application from scratch and interact with a database. <\/p>\n<h2>Installing the Smarty Template Engine<\/h2>\n<p>The Smarty template engine can be downloaded from the <a href=\"http:\/\/www.smarty.net\/\" target=\"_blank\">official website<\/a>. At this site you will also find complete documentation that will help you to learn more about working with Smarty templates. <\/p>\n<p>To install and use Smarty you should go through the following steps:<\/p>\n<ol>\n<li>Unzip the Smarty archive to the proper directory. I put the Smarty library into the <code>D:\/Apache2.2\/htdocs\/php\/<\/code> path. You should see the structure below in the Smarty directory.\n<p><a href=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/Smarty1.jpg\" target=\"newFrame\"><br \/>\n<br \/><img decoding=\"async\" src=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/Smarty1.jpg\" width=\"400\"\/><br \/><font size=\"1\"><em>Click here for larger image<\/em><\/font><\/a><br \/>\n<br \/><font size=\"2\"><b>Figure 1.<\/b> The Structure of <em>Smarty\/libs<\/em> Folder<\/font><\/p>\n<\/li>\n<li>Now, to test if Smarty is correctly installed, you should put the code below into the <em>index.php<\/em> script. As you can see from the listing below, before checking if all the required working folders can be accessed (using the <code>testInstall()<\/code> method) , you should create a new Smarty variable:\n<div class=\"example\"><code><\/p>\n<pre>&lt;?php\n\n\/\/Put full path to Smarty.class.php\nrequire('D:\/Apache2.2\/htdocs\/php\/Smarty\/libs\/Smarty.class.php');\n\n\/\/Create new variable $smarty from the class Smarty\n$smarty = new Smarty();\n\n\/\/Verifies that all required working folders of the Smarty installation can \/\/be accessed\n$smarty-&gt;testInstall();\n\n?&gt; <\/pre>\n<p><\/code><\/div>\n<\/li>\n<li>To see the output of the test, write <em>http:\/\/localhost\/php\/Smarty\/demo\/index.php<\/em> into the browser. You should get something like this:\n<div class=\"example\"><code><\/p>\n<pre>Smarty Installation test...\nTesting template directory...\n.templates is OK.\nTesting compile directory...\n.templates_c is OK.\nTesting plugins directory...\nD:Apache2.2htdocsphpSmartylibsplugins is OK.\nTesting cache directory...\n.cache is OK.\nTesting configs directory...\n.configs is OK.\nTests complete.<\/pre>\n<p><\/code><\/div>\n<\/li>\n<\/ol>\n<h2>Creating a New Simple Smarty Application<\/h2>\n<p>In this section you will see how to create your own &#8220;Hello World&#8221; application. To do that, you should first create a new folder (I named it <code>Smarty_example<\/code>) in the <code>D:\/Apache2.2\/htdocs\/php\/<\/code> directory, set the template directory for use with different templates (as you will see below in the <em>index.php<\/em> script), assign a variable (in this case, <code>name<\/code>) and then compile and display the template file, <em>index.tpl<\/em>.<\/p>\n<div class=\"example\"><code><\/p>\n<pre>&lt;?php\n\n\/\/Put the full path to Smarty.class.php\nrequire('D:\/Apache2.2\/htdocs\/php\/Smarty\/libs\/Smarty.class.php');\n\n\/\/Create new variable $smarty from the class Smarty\n$smarty = new Smarty();\n\n\/\/Set the template directory used for different templates\n$smarty-&gt;setTemplateDir('\/ Smarty_example \/templates');\n\/\/Set the compile directory \n$smarty-&gt;setCompileDir('\/ Smarty_example \/templates_c');\n\/\/Set the cache directory\n$smarty-&gt;setCacheDir('\/ Smarty_example \/cache');\n\/\/Set the configs directory\n$smarty-&gt;setConfigDir('\/ Smarty_example \/configs');\n\n\/\/Set the name variable variables\n$smarty-&gt;assign('name', 'WORLD');\n\n\/\/Compile and display output of the template file\n$smarty-&gt;display('index.tpl');\n\n?&gt; <\/pre>\n<p><\/code><\/div>\n<p>The template file, <em>index.tpl<\/em>, looks like this:<\/p>\n<div class=\"example\"><code><\/p>\n<pre>&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt;Smarty&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    Hello, {$name}!\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p><\/code><\/div>\n<p>The output of the &#8220;Hello World&#8221; application is:<\/p>\n<p><a href=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/Smarty2.jpg\" target=\"newFrame\"><br \/>\n<br \/><img decoding=\"async\" src=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/Smarty2.jpg\" width=\"400\"\/><br \/><font size=\"1\"><em>Click here for larger image<\/em><\/font><\/a><br \/>\n<br \/><font size=\"2\"><b>Figure 2.<\/b> The Output of the First Simple Application &#8220;Hello World&#8221;<\/font><\/p>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"Octavia_Anghel060720114658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"Octavia_Anghel060720114658.html?page=2\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to install and use the Smarty template engine for PHP and how to interact with a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1420","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1420"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1420\/revisions"}],"predecessor-version":[{"id":2229,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1420\/revisions\/2229"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}