|
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 |
harris Smarty Rookie
Joined: 15 Oct 2003 Posts: 5
|
Posted: Wed Oct 15, 2003 10:53 am Post subject: Smarty implementation |
|
|
I`m working on a site that has different categories, example:
- view owner
- edit owner
- delete owner
- insert owner
I tried working with 1 main template file, and from that file I include other files i.e: header, footer, etc.
Example:
====================================
view.tpl:
{include file="header.tpl"}
{include file="menu.tpl"}
{if $owner.id neq null}
INSERT HTML CODE TO VIEW OWNER HERE
{/if}
{include file="footer.tpl"}
====================================
My question is: is this a good implementation of smarty, because I studied some different methods.
I also tried making more than 1 instance of smarty, i.e:
=======================================
$header = new Smarty;
$header->assign("header_data", "blablablabla");
$header->display("header.tpl");
$menu = new Smarty;
$menu->assign("menu_data", "blablabla");
$menu->display("menu.tpl");
$content = new Smarty;
............
etc, etc.
=======================================
I also looked at some "bigger" php/mysql/smarty projects, for example PHPBB, Xoops, and I noticed that some projects use only a few or only 1 tpl file, and i.e. PHPBB uses a lot of tpl files.
Is there any noticable speed difference between this 2 different methods, and what method is "the more official way". And of course.. if anyone has a better way... I`m interested!
Regards,
Harris |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Oct 15, 2003 2:02 pm Post subject: |
|
|
In my experience, calling $smarty->display() once to render the entire page works well. The templates can sort out what other templates need to be included to make up the page. You can assign data to aid in how they go together too. This keeps your application logic clean and puts the responsibility of template heirarchy management in the templates. If at all possible, don't make the application logic rely on how the template files are organized. ie. if you wanted to completely change the interface one day and reorganize the templates, the application logic shouldn't need adjusting.
One method I use is an initialization template:
$smarty->display('init.tpl');
The init.tpl is the only template the application ever calls directly. This init.tpl does global initialization stuff (like loading configs from an init.conf) then uses assigned data to figure out where to go from there: typically choosing between something like layout.tpl or printable.tpl. |
|
Back to top |
|
harris Smarty Rookie
Joined: 15 Oct 2003 Posts: 5
|
Posted: Wed Oct 15, 2003 2:26 pm Post subject: |
|
|
Okay, so in you're opinion the best way is something like this
site.tpl
====================================
Code: |
{include file="header.tpl"}
{include file="menu.tpl"}
{if $my.cat eq 1}
{include file="categories/1.tpl"}
{else if $my.cat eq 2}
{include file="categories/2.tpl"}
{/if}
{include file="footer.tpl"}
|
====================================
An other advantage of this way is that you don't have to assign global data more than once |
|
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
|