{"id":22,"date":"2008-05-07T03:25:00","date_gmt":"2008-05-07T07:25:00","guid":{"rendered":"http:\/\/www.searchenginegenie.com\/programming-blog\/smarty-for-programmers\/"},"modified":"2012-12-11T04:58:19","modified_gmt":"2012-12-11T08:58:19","slug":"smarty-for-programmers","status":"publish","type":"post","link":"https:\/\/www.searchenginegenie.com\/programming-blog\/smarty-for-programmers\/","title":{"rendered":"Smarty for Programmers"},"content":{"rendered":"<p><span>The advantage for programmers is that they write the code in a PHP file without having to combine the instructions with HTML. Also, if the designer changes the layout of a page the programmer doesn&#8217;t have to change the code to suit the new layout since the functionalities won&#8217;t change. You perform your work in your files, assign to the templates all the values needed to print on the site and go out for a beer.<\/span><\/p>\n<p>The PHP file you need to include the Smarty class <code><span style=\"font-size: 10;\">require 'Smarty.class.php'. <\/span><\/code><span>After that you instantiate the smarty with <\/span><code><span style=\"font-size: 10;\">$smarty = new Smarty. <\/span><\/code><\/p>\n<p>To assign a variable to the template you need to<code><span style=\"font-size: 10;\"> $smarty-&gt;assign('UserName', 'Jeff Adams'). <\/span><\/code><span>After everything is finished you call the method and to display the template<\/span><code><span style=\"font-size: 10;\"> $smarty-&gt;display('index.tpl').<\/span><\/code><\/p>\n<p>A sample code like this (index.php) :<\/p>\n<p>[php]<br \/>\n&lt;code&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;&lt;php &lt;\/span&gt;<br \/>\n &lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;require &lt;\/span&gt;&lt;span style=&quot;color: #dd0000; font-size: 10;&quot;&gt;&#8217;Smarty.class.php'&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;; &lt;\/span&gt;&lt;\/code&gt;&lt;span&gt;<br \/>\n &lt;\/span&gt;&lt;code&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;$smarty &lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;= new &lt;\/span&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;Smarty&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;; &lt;\/span&gt;&lt;\/code&gt;&lt;span&gt;<br \/>\n &lt;\/span&gt;&lt;code&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;$smarty&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;-&gt;&lt;\/span&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;assign&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;(&lt;\/span&gt;&lt;span style=&quot;color: #dd0000; font-size: 10;&quot;&gt;&#8217;Username'&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;, &lt;\/span&gt;&lt;span style=&quot;color: #ff8000; font-size: 10;&quot;&gt;&#8217;Jeff Adams&#8217;); &lt;\/span&gt;&lt;\/code&gt;&lt;span&gt;<br \/>\n &lt;\/span&gt;&lt;code&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;$smarty&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;-&gt;&lt;\/span&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;display&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;(&lt;\/span&gt;&lt;span style=&quot;color: #dd0000; font-size: 10;&quot;&gt;&#8217;index.tpl'&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;); &lt;\/span&gt;&lt;\/code&gt;&lt;span&gt;<br \/>\n &lt;\/span&gt;&lt;code&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;?&gt; &lt;\/span&gt;&lt;\/code&gt;<\/p>\n<p>[\/php]<\/p>\n<p><span><br \/>\n<\/span><\/p>\n<p>The template (index.tpl) like this:<\/p>\n<p>[php]<br \/>\n&lt;code&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;&lt;&lt;\/span&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;html&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;&gt; &lt;\/span&gt;&lt;\/code&gt;&lt;span&gt;<br \/>\n &lt;\/span&gt;&lt;code&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;&lt;&lt;\/span&gt;&lt;\/code&gt;&lt;code&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;body&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;&gt; &lt;\/span&gt;&lt;\/code&gt;&lt;span&gt;<br \/>\n &lt;code&gt; &lt;\/code&gt;&lt;\/span&gt;&lt;code&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;Welcome &lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;{&lt;\/span&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;$Username&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;} &lt;\/span&gt;&lt;\/code&gt;&lt;span&gt;<br \/>\n &lt;code&gt;&lt;!&#8211;&lt;\/code&#8211;&gt;&lt;\/code&gt;&lt;\/span&gt;&lt;code&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;&lt;&lt;\/span&gt;&lt;\/code&gt;&lt;code&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;body&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;&gt; &lt;\/span&gt;&lt;\/code&gt;&lt;span&gt;<br \/>\n &lt;code&gt;&lt;!&#8211;&lt;\/code&#8211;&gt;&lt;\/code&gt;&lt;\/span&gt;&lt;code&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;&lt;!&#8211;&lt;\/span&#8211;&gt;&lt;\/span&gt;&lt;\/code&gt;&lt;code&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;&lt;\/&lt;\/span&gt;&lt;\/code&gt;&lt;code&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;html&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;&gt;&lt;\/span&gt;&lt;\/code&gt;<\/p>\n<p>[\/php]<\/p>\n<p>Also create an array in PHP an pass it to the template:<\/p>\n<p>[php]<br \/>\n&lt;code&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;$tmplate &lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;= array ( &lt;\/span&gt;&lt;span style=&quot;color: #ff8000; font-size: 10;&quot;&gt;&#8217;UID&#8217;=&gt; &#8217;10&#8217;, &amp;&#8217;Name&#8217; =&gt; &#8216;Jeff Adams&#8217;, &#8216;Address&#8217;=&gt;&#8217;Home address&#8217;); &lt;\/span&gt;&lt;\/code&gt;&lt;span&gt;<br \/>\n &lt;\/span&gt;&lt;code&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;$smarty&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;-&gt;&lt;\/span&gt;&lt;span style=&quot;color: #0000bb; font-size: 10;&quot;&gt;assign&lt;\/span&gt;&lt;span style=&quot;color: #007700; font-size: 10;&quot;&gt;(&lt;\/span&gt;&lt;span style=&quot;color: #ff8000; font-size: 10;&quot;&gt;&#8217;info&#8217;, $tmplate);&lt;\/span&gt;&lt;\/code&gt;<\/p>\n<p>[\/php]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The advantage for programmers is that they write the code in a PHP file without having to combine the instructions with HTML. Also, if the designer changes the layout of a page the programmer doesn&#8217;t have to change the code to suit the new layout since the functionalities won&#8217;t change. You perform your work in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,1],"tags":[108],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-programmer","category-programming","tag-advantage-for-programmers"],"_links":{"self":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":6,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":590,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/22\/revisions\/590"}],"wp:attachment":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}