{"id":7499,"date":"2014-06-19T15:48:35","date_gmt":"2014-06-19T15:48:35","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/06\/19\/access-smarty-template-collection-of-common-programming-errors\/"},"modified":"2014-06-19T15:48:35","modified_gmt":"2014-06-19T15:48:35","slug":"access-smarty-template-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/06\/19\/access-smarty-template-collection-of-common-programming-errors\/","title":{"rendered":"Access Smarty Template-Collection of common programming errors"},"content":{"rendered":"<p>Short answer, as you need to go up one directory from your testpage.php to get to the directory that contains your smarty directory, like you&#8217;ve done for the header.php include you need to do the same for the smarty include directories.<\/p>\n<pre><code>$smarty-&gt;setTemplateDir('..\/smarty\/templates');\n<\/code><\/pre>\n<p>One way of doing this nicely is defining how to get to the root directory of your project and then using that in the include.<\/p>\n<p>e.g. in testPage.php<\/p>\n<pre><code>define(\"PATH_TO_ROOT\", \"..\/\");\n<\/code><\/pre>\n<p>and then in header.php<\/p>\n<pre><code>$smarty-&gt;setTemplateDir(PATH_TO_ROOT.'smarty\/templates');\n$smarty-&gt;setCompileDir(PATH_TO_ROOT.'smarty\/templates_c');\n$smarty-&gt;setCacheDir(PATH_TO_ROOT.'smarty\/cache');\n$smarty-&gt;setConfigDir(PATH_TO_ROOT.'smarty\/configs');\n<\/code><\/pre>\n<p>This makes it then be trivial to setup the Smarty directories from another <code>PHP<\/code> file that may be in another location. e.g. in a directory called &#8220;tests\/webtests\/frontend&#8221; you could define the PATH_TO_ROOT as &#8220;..\/..\/..\/&#8221; and the calls to setup Smarty would still work.<\/p>\n<p>You could also make header.php check that PATH_TO_ROOT is defined, to prevent it from being called directly.<\/p>\n<p>As a side note, you might want to consider not having the templates_c and cache directory under the Smarty directory, but instead create a separate directory elsewhere to write data that is generated (and so potentially vulnerable to injection attack). For my projects I have a &#8216;var&#8217; directory located off the projects root directory, which holds all the directories for log files, caches, generated templates etc. Everything in the sub-directory from &#8216;var&#8217; is considered &#8216;unsafe&#8217; which makes thinking about what is safe and what isn&#8217;t much easier.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer, as you need to go up one directory from your testpage.php to get to the directory that contains your smarty directory, like you&#8217;ve done for the header.php include you need to do the same for the smarty include directories. $smarty-&gt;setTemplateDir(&#8216;..\/smarty\/templates&#8217;); One way of doing this nicely is defining how to get to the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7499","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7499"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7499\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}