|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
u3 Smarty n00b
Joined: 12 Oct 2005 Posts: 2
|
Posted: Wed Oct 12, 2005 5:08 am Post subject: how to set directory structure and config file |
|
|
hello masters, sorry for newbie question:
I'm trying to develope my new site using smarty (formerly using usual php function + adodb), here's my dir structure:
Code: |
/webroot
/html (document root)
/smarty
/inc
/html contains sub dir like /news, /forum, /guestbook etc
/smarty will contain sub dir for every application in there I will create templates, templates_c, cache, configs
/inc contain myconf.php
myconf.php
<?php
require('Smarty.class.php');
class mySmarty extends Smarty {
function Smarty_Main()
{
$this->Smarty();
$this->template_dir = '/webroot/smarty/main/templates/';
$this->compile_dir = '/webroot/smarty/main/templates_c/';
$this->config_dir = '/webroot/smarty/main/configs/';
$this->cache_dir = '/webroot/smarty/main/cache/';
$this->caching = true;
$this->assign('app_name', 'Main Application');
}
function Smarty_News()
{
$this->Smarty();
$this->template_dir = '/webroot/smarty/news/templates/';
$this->compile_dir = '/webroot/smarty/news/templates_c/';
$this->config_dir = '/webroot/smarty/news/configs/';
$this->cache_dir = '/webroot/smarty/news/cache/';
$this->caching = true;
$this->assign('app_name', 'News Application');
}
....and so on...
}
?>
/html/index.php
<?php
require('../inc/myconf.php');
$smarty = new mySmarty;
$smarty->Smarty_Main();
...and so on...
?>
/html/news/index.php
<?php
require('../inc/myconf.php');
$smarty = new mySmarty;
$smarty->Smarty_News();
...and so on...
?>
|
my questions are:
- am I doing right ?
- should I create sub dir in /smarty for every application (news, forum, guetbook, etc) because it will be so many sub directory in smarty and I've to add in myconf.php for every application
- is there any simple method than I've done ?
thanks |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Oct 12, 2005 1:55 pm Post subject: |
|
|
You could probably get away with one class:
[php:1:78f345d517]$smarty = new Smarty('appname');[/php:1:78f345d517]
Where the constructor would setup the appropriate directory names, var assignements, etc. This would avoid all the separate class definitions.
Other than that, it would be a good idea to keep the /smarty/ directory out of document root if possible. |
|
Back to top |
|
u3 Smarty n00b
Joined: 12 Oct 2005 Posts: 2
|
Posted: Thu Oct 13, 2005 2:44 am Post subject: |
|
|
thanks, very usefull for me
what is the advantage placing /smarty out of document root ? any security concern about that ?
thanks |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Oct 13, 2005 1:48 pm Post subject: |
|
|
You should leave PHP/Smarty files outside of document root that don't need direct browser access. It is possible for a browser to access Smarty libarary files, template files and compiled template files directly otherwise. You could also use .htaccess in Apache to disallow direct access. There are no known problems/exploits if you leave it wide open, but it is just good habit to do this. How you set it up is your choice. |
|
Back to top |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Sat Dec 10, 2005 11:18 pm Post subject: |
|
|
Hi,
similar to mohrt's suggestion using some 'myapp' switch, I subclassed Smarty (to do some more tricks) and feed it with an $option array, containing all the directories and settings (and more) already prepared and ready to rumble.
This allows me to add as many setups as I want without changing the PHP code everytime I need some special configuration.
The array comes from a regular .INI file and has sections for the various "hosts" ("applications") of a particular domain the site(s) runs at, e.g. my local PC, a "live" test-domain or the final website.
I added an intermediate parser (not shown here) to allow the use of PHP variables and reference inside the INI, so it allows me to refer to values from other (upper) sections in the same INI - sort of an "inheritance" feature, as you'll read further below.
The procedure looks roughly like this:
[php:1:ff13d3b2b0]$pb_ini = parse_ini_file('pagebuilder.ini', true);
// do some tricks and parsing with the resulting assoc. array
process_ini($pb_ini);
// create a subclassed Smarty [Page]
$Page = &new Page($pb_ini['config']['root_dir'], $pb_ini['config']);
$Page->display("blabla.tpl");[/php:1:ff13d3b2b0]
Example of 'pagebuilder.ini' (reduced, explained below)
Code: | [global]
; Top Level Domain-Name w/o hostname
domain = example.com
; known hosts with different config
hosts = www,local,foobar
; --------------------------------------------------
; settings for www.example.com
[config]
hostname = www
root_dir = {DOCUMENT_ROOT}/../
template_dir = {config.root_dir}templates
content_dir = {config.template_dir}/content
config_dir = {config.template_dir}/configs
lang_dir = {config.template_dir}/lang/{LANG_CODE}
cache_dir = {config.root_dir}smarty.{global.domain}/cache
compile_dir = {config.root_dir}smarty.{global.domain}/compiled
clear_all_cache = 0
caching = 1
force_compile = 0
debugging = 0
; --------------------------------------------------
; settings for local.example.com
[config_local]
hostname = local
clear_all_cache = 1
caching = 0
force_compile = 1
debugging = 1
; cache files go to TEMP dir on local PC
cache_dir = {TMP}/smarty/{global.domain}/cache
compile_dir = {TMP}/smarty/{global.domain}/compiled
; --------------------------------------------------
[config_foobar]
; settings for hekate.example.com
hostname = hekate
clear_all_cache = 1
caching = 0 ; 0=kein, 1=cache_lifetime
force_compile = 1
debugging = 1 ; debugging ist aktiv sobald hier irgendein Wert steht
| As a side note: I'm going to switch to XML files for the next version of "Class Page".
I mainly use the "hostname" as a switch for the various settings, since most of my apps run in different subdomains anyway, you may use the "application" name as this switch.
[global]
sets the website domain and "known" hostnames (or apps) that need special Smarty settings
[config]
is a catch-all setup and contains ALL defaults for ANY host, incl. those not explicitely given in [global] hosts.
Also declares the root_dir folder, that'll be the path to "/webroot" in your case, and contains similar things: /include, /templates etc.
As mohrt proposed, this is NEVER EVER the document_root, but rather one level up (at least)!
It can even be someplace else as long as PHP has access.
The remaining are counterparts of the Smarty properties.
{config.template_dir} is a "variable", it refers to the value of
[config]
template_dir = /webroot/templates
Any section can be referenced this way as well as any global PHP variable or (reliable) values from the "Superglobals" (eg. $_SERVER) Code: | cache_dir = {config.root_dir}smarty.{global.domain}/cache | would result in Code: | cache_dir = /webroot/smarty.example.com/cache |
Since I can "feed" the parser with an explicit hostname you may provide the "appname" to perform such a switch.[php:1:ff13d3b2b0]process_ini($pb_ini, 'foobar');[/php:1:ff13d3b2b0]process_ini() would then return the merged content of [config] and [config_foobar] after doing a bit of criss-cross variable assignment.
If the hostname is ommited it tries to detects the name by itself and checks the "hosts" list (in [global]) if it's "valid". It'll then merge whatever it finds in both, the [config] section and the [config_foobar] section -- that's the "inheritance" part, with the latter overriding the former.
I only have to adjust the different folders for foobar.example.com in [config_foobar] and they'll override whatever's written in the plain [config].
IF I install a new subdomain, lets say "forum", I just add this host to
[global]
hosts = www,local,forum,foobar
and a section with the different settings
[config_forum]
template_dir = {config.root_dir}templates/forum
config_dir = {config.template_dir}/configs/forum
cache_dir = {config.root_dir}smarty.{global.domain}/cache/forum
compile_dir = {config.root_dir}smarty.{global.domain}/compiled/forum
CirTap |
|
Back to top |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Sun Dec 11, 2005 1:18 am Post subject: |
|
|
Another note about "applications": they're basically nothing more than "Skins" or "Themes" (ignoring the "logic" part here), so you may use this structure as well
Code: |
$smarty->template_dir = "/webroot/templates";
/webroot/templates/news
/webroot/templates/forum
/webroot/templates/main
main/index.tpl:
{include file="main/header.tpl"}
{include file="main/content.tpl"}
{include file="forum/simple_login.tpl"}
{if $USER.options.show_news}{include file="news/ticker.tpl"}{/if}
{include file="main/footer.tpl"} |
Since the template name is part of the cached file name, each can have an "index.tpl" without interfereing, and it's easier to include the template from "news" into the "main" part of templates, because both are subfolders of template_dir. Presuming that the main site includes the appropriate code from "news" to feed it with content
CirTap |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|