Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Best Pratices for modules templates

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
canoramix
Smarty Rookie


Joined: 12 Oct 2003
Posts: 9

PostPosted: Tue Oct 21, 2003 10:53 pm    Post subject: Best Pratices for modules templates Reply with quote

Hi,

I'm creating my first Smarty site and I would to ask you what's the Best Pratices to use "modules" or "blocks" within my site.

Tipically, the layout of my site has a top table with the logo and some quick links. Below it has a left column with a fixed width for the menu, and the rest of the right space to content. This skeleton will be used all over the site. So I thought I could create a separate top.tpl and top.php file to the top part of my site and the same for the left part: menu.tpl and menu.php.

The site will have three diferente pages: index.php, news.php, archive.php, and three template files: index.html, news.html and archive.html.

Simplifying the files, I will have something like:

Code:
index.html
==========
{include_php file="top.php"}
<table>
 <tr>
  <td>{include_php file="menu.php"}</td>
  <td>{include_php file="content.php"}</td>
 </tr>
</table

index.php
=========
$smarty = new Smarty;
$smarty->display('index.html');


menu.html
=========
{$menu}

menu.php
======
$something = create the menu from database
$this->assign("menu",$something);

$this->display('menu.html');

content.html
============
{$content}

content.php
===========
$something = create the content from database
$this->assign("content",$something);
$this->display('content.html');



news.html
=========
{include_php file="top.php"}
<table>
 <tr>
  <td>{include_php file="menu.php"}</td>
  <td>{include_php file="content-news.php"}</td>
 </tr>
</table

news.php
========
$smarty = new Smarty;
$smarty->display(news.html');

content-news.html
=================
{$content}

content-news.php
================
$something = create the content from database
$this->assign("content",$something);
$this->display('content-news.html');


My question is:
1. Is this the best way to do this things?
2. Is it better to have all the files needed to create a page within the same template file and PHP file?

Any suggestions...

Thanks in advance,
Canoramix
Back to top
View user's profile Send private message
canoramix
Smarty Rookie


Joined: 12 Oct 2003
Posts: 9

PostPosted: Wed Oct 22, 2003 10:18 pm    Post subject: Reply with quote

Hi,

I understand that my last message was a little bit long Wink but what I would like to know from the people that uses Smarty is if you put on the same .tpl/.php files the all page, of if you split the content of one page in several files.

Thanks in advance,
Canoramix
Back to top
View user's profile Send private message
Justin
Smarty Regular


Joined: 07 May 2003
Posts: 38
Location: Vilnius, Lithuania

PostPosted: Thu Oct 23, 2003 3:17 pm    Post subject: Reply with quote

/modules/*mod_name*/ - module dir. php files are here
/tpl/*mod_name*/user/ - dir for user side templates
/tpl/*mod_name*/admin/ - dir for admin side templates

simple structure, but I like it.
_________________
http://www.baubas.net
Back to top
View user's profile Send private message Visit poster's website
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Fri Oct 24, 2003 7:46 am    Post subject: Reply with quote

I have written a custom resource to access my module templates.

That's my hierarchy:
/.../modules/<ModuleName>/func/<ScriptName>.php
/.../modules/<ModuleName>/templates/<TemplateName>.tpl
/.../templates_c
/.../cache

In my script I simply use
Code:
$smarty = new Smarty();
/* ... */
$smarty->display("module:<ModuleName>/templates/<TemplateName>.tpl")


This way I have everything of a module in one place under /.../modules/<ModuleName>. It's easy to remove and install new modules because you don't need to copy files into different directories.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Oct 24, 2003 7:45 pm    Post subject: Reply with quote

andre wrote:
I have written a custom resource to access my module templates.

This way I have everything of a module in one place under /.../modules/<ModuleName>. It's easy to remove and install new modules because you don't need to copy files into different directories.


Nice one!
Back to top
View user's profile Send private message
canoramix
Smarty Rookie


Joined: 12 Oct 2003
Posts: 9

PostPosted: Fri Oct 24, 2003 8:29 pm    Post subject: Reply with quote

Hi Andre, I've a few questions I would like to ask you:

1. The example you posted is from your <ScriptName>.php or from the main page? Do you call the .tpl file from the .php or do you use {include_php}?
2. On your main page do you include the <ScriptName>.php or do you invoke the <TemplateName>.tpl?
3. Does the custom resource you write is open source Smile ? I mean, is it shareble?

Thank you for you tip,
Canoramix
Back to top
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Mon Oct 27, 2003 8:08 am    Post subject: Reply with quote

I use an approach that is similar to PHPNuke / PostNuke. This means I have a index.php file in the root directory "/.../htdocs".
You can call a module using "index.php?module=News" for example. Modules consist of multiple so-called module functions. Each function is represented as a script (.php) file. For example:

/.../htdocs/modules/News/func/showNews.php
/.../htdocs/modules/News/func/editArticle.php
/.../htdocs/modules/News/func/...

These functions can use as many templates as they with. All are located under
/.../htdocs/modules/News/templates/

So the showNews.php module function will look something like this:

[php:1:fab2e12de6]// query database
$articles = $database->getAll("SELECT * FROM `news_articles` ...");
// ...
// now process template(s)
$smarty = new Smarty();
$smarty->assign("articles", $articles);
$smarty->assign( ... );
$smarty->display('module:News/templates/showNews.tpl');[/php:1:fab2e12de6]

Additionally I have added theming support. This means a single template (page.tpl) manages the whole page layout:

Code:
<html>
<head><title>{$page_title}</title></head>
<body>
  ...
</body>
</html>


Now back to the index.php file:

[php:1:fab2e12de6]// ...
// Here I do some checks, load global include files etc.
// ...
include ("includes/Smarty.class.php");

// Now display the output page
$smarty = new Smarty();
$smarty->assign("page_title", $page_title);
// ...
$smarty->display("theme/templates/page.tpl");[/php:1:fab2e12de6]

As meantioned earlier a module call looks like this: index.php?module=News&func=showNews[&foo=bar&x=y&...]

The trick is how to get the module's output (that is generated by showNews.php) into the theme's page layout (page.tpl).

I use an insert here. So somewhere in page.tpl you will find the following line(s):
Code:
 // ... begin of page.tpl
<div id="content">{insert name="module" ... }</div>
// ... rest of page.tpl


The used insert plugin is very simple and handles the module function dispatching:
[php:1:fab2e12de6]function smarty_insert_module($params, &$smarty) {
// ...
$module_name = (isset($_GET["module"]))?$_GET["module"]:$default_module;
$module_func = (isset($_GET["func"]))?$_GET["func"]:$default_func;
// ...
include_once("modules/" . $module_name . "/func/" . $module_func . ".php");
// ...
}[/php:1:fab2e12de6]

This way I don't need to do "VERY BAD THINGS (tm)" like include_php etc. And the theme is completely separated from the modules.

The whole project was developed for my company and is not public (yet?).
Perhaps I can convince them to go open source sometimes.

Feel free to ask if you have more questions Wink
Back to top
View user's profile Send private message
canoramix
Smarty Rookie


Joined: 12 Oct 2003
Posts: 9

PostPosted: Wed Oct 29, 2003 11:07 pm    Post subject: Reply with quote

Thank you Andre,

What I've had in mind is to split the main .tpl file in small blocks but for now I think it would be a good ideia to have all the page in one .tpl file.

I will keep in touch Smile,

Canoramix
Back to top
View user's profile Send private message
mshaffer
Smarty Rookie


Joined: 18 Dec 2003
Posts: 12

PostPosted: Thu Feb 26, 2004 6:24 pm    Post subject: More abstraction for modules Reply with quote

andre wrote:

Additionally I have added theming support. This means a single template (page.tpl) manages the whole page layout:


I really enjoyed your framework potential. However, I have a few questions regarding theming, coloring, multi-lengual support.

It would be nice to come up with a module file directory framework, because plugins could follow certain parameters, and make the framework very nice indeed.

I have been swimming with ideas similar to yours, but I want a few more levels of abstraction. What if the themes (I call them layouts) are multiple within the module (eg., horizontal_toc, vertical_toc)? What if the module has language support (en-us, en-uk, etc.). What about different style sheets (here I use the word theme) for each layout. Finally colors. Colors can be defined globally (page.tpl), or modularly (include with parameters).

I am trying to create a framework that allows for customization with hierarchy from global --> site --> page --> module with the ability for customization to occur via the user if the admin of the module allows it. In that sense, if applicable, the user can overwrite a layout, theme, language, color on the page level and possibly the module level.

Thanks for your efforts, and your response is appreciated. I like your base framework, hence this message.

monte


{x:
Back to top
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Fri Feb 27, 2004 7:36 am    Post subject: Reply with quote

Hi mshaffer!

I've invested quite alot time in my theming engine. Here's how it works basically:

  • All themes extend a basic theme class (ngTheme) which offers functions for displaying the output (ngTheme::displayPage()), assigning very special variables (ngTheme::addStyleSheet(), ngTheme::addScript() - details later) etc.

  • Themes define a basic color set:
    - row_color1, row_color2 (alternating row colors for tables)
    - table_border_color
    - table_head_fore_color
    - table_head_back_color
    - ... some more ...

    Modules can use these pre-defined colors in their templates like this: {$nextgen.theme.row_color1} ("nextgen" is the name of the project for now - to be renamed Wink ) This is not recommended but supported because sometimes it's needed. Better the module should provide it's own StyleSheet and registers it in the theme using ngTheme::addStyleSheet("module:News/css/news.css"). By the way: Additional JavaScript libraries can by loaded by the module using ngTheme::addScript("module:News/javascript/news.js"). Using a second parameter you can even enable additional Smarty parsing for the included file: ngTheme::addScript("module:News/javascript/news.js.tpl", true).

  • If this is not enough for you, you could also create a theme which overwrites module templates. So just place a copy of the module template in the subdirectory /.../themes/<ThemeName>/modules/<ModuleName>/templates/<TemplateName>.tpl
    This is also not recommended because if the module template changes you also have to change the theme template. But because I have alot of small ones this is not a too big problem for me.

  • Last but not least the theme offers some basic styles which can be used by the modules too.


Quote:
What if the themes (I call them layouts) are multiple within the module (eg., horizontal_toc, vertical_toc)?

You see the themes just provide the basic look-and-feel for your application but not neccessarily the layout. If you want horizontal or vertical content lists depends if the module provides the different templates and if there's a possibility in the module for the user or admin to switch between these two layouts.

Quote:
What if the module has language support (en-us, en-uk, etc.).

Here I am using a solution similar to the one I already posted on the Wiki pages: http://smarty.incutio.com/?page=SmartyMultilanguageSupport
This is totally independant from the themes because a theme should not insert any language strings into a module template. For the other strings a theme needs the designer can provide additional language files which will be load automatically.

Quote:
What about different style sheets (here I use the word theme) for each layout.

I hope I have just answered this question above ?! Wink

Quote:
Finally colors. Colors can be defined globally (page.tpl), or modularly (include with parameters).

Nope - I don't like the module to use different colors than provided by the theme. Okay, in some cases I also use style="color:red" for a very very important string within a module template but normally the colors are defined by the included CSS and/or the theme properties {$nextgen.theme.table_border_color}. So I don't see a reason why a module should bring it's own colors. If you meant that a theme should be able to use different colors for different modules that's sure possible. Just let the theme include another stylesheet and define another color set depending on the currently loaded module by overwriting the displayPage() method for example.


Feel free to ask if you have some more questions Very Happy

Andre
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Mar 03, 2004 11:50 pm    Post subject: Reply with quote

Hi andre!

I'm in the process of re-doing my module system and there are some ideas that you use which I have also thought to use. I was wondering if you could share some of your experience. In particular, I'm wondering how to deal with a 'minor' packaging issue.

In my setup I am envisioning something like:

Code:
/modules
  /a_module
    php files for functionality...

    /libs
      required (foreign/custom) php libraries not part of framework/app

    /ui
      default templates for module

      /resources
        css, js, images, etc

      /opt_theme_dir
        optional directories for custom themes (follows ./ui pattern)

My problem is the resources directories. I want to keep these together with the module but I want to keep the whole module out of the webroot. Obviously, the client requires access to the files in resources so I thought to do one of:

1) write a public facing controller that accepts requests for resources and then reads the resource and returns it raw. If this is the answer, I'll abandon the effort as it is seriously inefficient.

2) write a module installer that physically copies resources from the module directories in the appropriate webroot folders.

3) provide plugins to handle resource inclusion which write appropriate tags and copy files as above from the resource to webroot folders but only as required. This is a little cumbersome as I wouldn't want the template writer to have to specify the module parameters as attributes on each resource plugin use.

Have you approached this aspect of the problem and if so, do you have insights? I'd hate to spend a lot of time writing plumbing only to find an issue that makes the idea unworkable.

Thanks in advance!
Back to top
View user's profile Send private message
danny36
Smarty Rookie


Joined: 14 Jul 2004
Posts: 27
Location: Italy - Arezzo (Tuscany)

PostPosted: Tue Aug 31, 2004 10:14 am    Post subject: Reply with quote

andre, is possible now to share your complete code for modules and theme smarty system? Laughing Very Happy
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP