|
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 |
Do you like the method? |
Yes |
|
33% |
[ 1 ] |
No |
|
66% |
[ 2 ] |
It is flawed in some ways, and could use some improvements, but I like it |
|
0% |
[ 0 ] |
|
Total Votes : 3 |
|
Author |
Message |
JasonDS Smarty Rookie
Joined: 25 Jan 2007 Posts: 25
|
Posted: Thu Jan 25, 2007 6:40 am Post subject: My Framework (Rate It) |
|
|
I developed a basic website framework about 3 monthes ago, searching for a template engine, I found Smarty. So this project basically turned out to be a little "framework", I guess. I will demonstrate how it loads, and detects to see if a page is there.
Data Structure
[1] ROOT
[1.2] INDEX.php
[1.3] PORTAL.php
[1.4] CLASSES/
[1.4.1] HTTP.php
[1.5] MODULES/
[1.5.1] HOME/
[1.5.1.1] MAIN.php
[1.5.1.2] EXAMPLE.php
[1.6] THEMES/
[1.6.1] FOO/
[1.6.1.1] HOME.MAIN.tpl
[1.6.1.2] HOME.EXAMPLE.tpl
File: HTTP.php
About: Handles the loading of TPL and Module files, also detects their existance.
Content:
Code: |
class HTTP
{
public $_dir = './templates/';
public function _getTPL( $module, $command )
{
if( !$module || strlen( $module ) < 1 )
{
$module = 'home';
}
if( !$command || strlen( $command ) < 1 )
{
$command = 'main';
}
if( !is_file( $this->_dir . strtolower( $module ) . '.' . strtolower( $command ) . '.tpl' ) )
{
if( !is_file( $this->_dir . strtolower( $module ) . '.main.tpl' ) )
{
return 'home.main.tpl';
}
else
{
return strtolower( $module ) . '.main.tpl';
}
}
else
{
return strtolower( $module ) . '.' . strtolower( $command ) . '.tpl';
}
}
public function _getModule( $module, $command )
{
include( 'portal.php' );
if( !$module || strlen( $module ) < 1 )
{
$module = 'home';
}
if( !$command || strlen( $command ) < 1 )
{
$command = 'main';
}
if( !is_dir( './Modules/' . strtolower($module) . '/' ) )
{
include( './Modules/home/main.php' );
}
elseif( !is_file( './Modules/' . strtolower( $module ) . '/' . strtolower( $command ) . '.php' ) )
{
include( './Modules/' . strtolower( $module ) . '/main.php' );
}
else
{
include( './Modules/' . strtolower( $module ) . '/' . strtolower( $command ) . '.php' );
}
}
}
|
File: PORTAL.php
About: Constructs classes, sets global variables.
Content:
Code: |
<?php
/*
* Settings we would normally have in CONFIG.php
*/
$settings['engine']['template']['theme_dir'] = './Themes/'; // Template directory
$settings['engine']['template']['theme_dir'] = 'FOO'; // Template name
if( !session_id() )
{
session_start();
}
require_once('./Addons/Smarty/libs/Smarty.class.php');
$smarty = New Smarty;
/* Enable compile_check */
$smarty->compile_check = true;
/* Disable Smarty debugging */
$smarty->debugging = false;
/* Set the template directory */
$smarty->template_dir = $settings['engine']['template']['theme_dir'] . $settings['engine']['template']['active']. '/';
/* Set the compile directory */
$smarty->compile_dir = $smarty->template_dir . 'cache';
/* Set the config directory */
$smarty->config_dir = $smarty->template_dir . 'config/';
/* We want to use sub directories */
$smarty->use_sub_dirs = TRUE;
/* Handle the requested Module/Command */
if( !isset( $_REQUEST['com'] ) || strlen( $_REQUEST['com'] ) < 1 )
$command = 'main';
else
$command = $_REQUEST['com'];
if( !isset( $_REQUEST['mod'] ) || strlen( $_REQUEST['mod'] ) < 1 )
$module = 'home';
else
$module = $_REQUEST['mod'];
/**********************/
$smarty->assign("DEBUG_N_Module", $module);
$smarty->assign("DEBUG_N_Command", $command);
require_once('./Classes/HTTP.php');
$http = New HTTP;
/* Set the base directory */
$http->_dir = $smarty->template_dir;
DEFINE( '__TPL__', $http->_getTPL( $module, $command ) );
?>
|
File: INDEX.php
About: Includes the necessary PORTAL.php file, and locates the correct module.
Content:
Code: |
<?php
require_once( 'portal.php' );
$http->_getModule( $module, $command );
?>
|
How the System Works
Stage 1 - Getting the module and its command
-
As you see in PORTAL.php, we request two variables: 'com', 'mod'. These are short for, Command and Module. This system relies on these two variables alone to find the correct template and module file.
Stage 2 - Detecting files
-
The functions in HTTP.php detect if the files requested are available or not. By default if no module is supplied, home is used, if no command is supplied, main is used.
Stage 3 - Placing modules
-
The modules are saved in a module/command format for the best filesystem performance. They are listed like this, ROOT/module/command.php. There should always be ATLEAST one command, that being the main.php command.
Stage 3 - Locating the correct template file with module file
-
The template files are found in a named format. The format is, module.command.tpl. For each module, there should be ATLEAST one template file, that being the module.main.tpl file.
Stage 4 - Displaying the template file
-
All the files are correctly linked together, you see. In PORTAL.php you will see at the bottom that __TPL__ is defined, that tells us our template file. So at the end of every Module file, there should be *if there is a display* $smarty->display( __TPL__ );
Conclusion
I hope you like the way I structured it, I am sorry if the documentation I wrote is confusing, or long. If you see that I have made myself vulnerable in anyway possible, or made a critical mistake, please alert me or post a modification. I would post a working sample, but it would probably not be used, and all you need is right here anyways.
If you want a working demo of it, please request so, and I will leave it here to download.
Regards |
|
Back to top |
|
beginnerbest Smarty n00b
Joined: 04 Feb 2007 Posts: 1
|
Posted: Sun Feb 04, 2007 6:00 pm Post subject: |
|
|
i would love to have the working sample on the code.. tq |
|
Back to top |
|
JasonDS Smarty Rookie
Joined: 25 Jan 2007 Posts: 25
|
Posted: Sun Feb 11, 2007 5:06 am Post subject: |
|
|
Ok, it's uploaded to my server at:
http://www.tibialog.net/files/Framework.zip
Have fun, I hope you find it useful
- You do not need to edit anything to test it, just upload it to your webservers root directory (or other directory), and browse to it. |
|
Back to top |
|
vedau Smarty n00b
Joined: 30 Mar 2007 Posts: 1
|
Posted: Tue Apr 03, 2007 2:00 am Post subject: |
|
|
downloaded and try to using your framework.
hey man i can't unzip it
thank you for your sharing. i will rate it soon. |
|
Back to top |
|
cedar Smarty n00b
Joined: 11 Jul 2007 Posts: 3
|
Posted: Wed Jul 11, 2007 5:00 pm Post subject: More Using Docs |
|
|
Any docs on using it? |
|
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
|
|