Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

My smarty driven site engine

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
jtm
Smarty Rookie


Joined: 13 Nov 2003
Posts: 9

PostPosted: Wed Nov 26, 2003 10:38 am    Post subject: My smarty driven site engine Reply with quote

This is a short description about how I'm using smarty on my site. It's a bit odd when comparing how smarty is generally used but you might be interested =)

The basic idea is to use template files to store the content of a page. "What" you say? I know that templates should not be used to store the content but keep reading =)

I created my own extension and handler to apache:
Code:

        AddType application/x-httpd-page .page
        Action application/x-httpd-page /smarty/wrapper.php


Now, when you reguest a page (for example) http://www.foobar.com/index.page Apache actually executes the wrapper.php and passes some data into it inside $_SERVER array.

The wrapper.php can be as small as this:
Code:



// get the filename with path which is requested (ie. index.page)
$filename = $_SERVER['PATH_TRANSLATED'];

// Render the index.page. Get the contents (ie. everything except smarty tags) to $content_data.
// This also makes smarty parse the smarty variables (like assign) in the .page file so they can be read with $smarty->get_template_vars after this.
$content_data = $smarty->fetch($filename);

// Assign the main content to 'content_data'. This is used in the layout file
$smarty->assign('content_data', $content_data);

// We defined what layout we wan't to use in the index.page
$layout = $smarty->get_template_vars('layout');

$layout_file = "/path/where/layouts/are/" . $layout . ".tpl";

// Assign the layout filename to 'root_layout' template variable. root.tpl uses this to finally render the whole page.
// The results are also cached on the root.tpl
$smarty->assign('root_layout', $layout_file);

$page_cache_id = md5($filename . serialize($_GET));

// Get the cache
$smarty->display("root.tpl", $page_cache_id);



the index.page:
Code:

{assign name="layout" value="mylayout"}

<h1>Hello, World!</h1>


Here we assign value "mylayout" to template variable "layout". The wrapper used this when it chooses what layout we use to render the page. The contents (ie. the hello world in the .page file) is stored into another tempate variable 'content_data'.



The mylayout.tpl in /path/where/layouts/are
Code:

<html><head>...</head>
<body>
{* this is replaced with the contents of index.page -template file *}
{$content_data}
</body>
</html>



root.tpl is used to render the full page onto it. after that the root.tpl is cached using smarty's own cache functions.
the root.tpl:
Code:

{include file=$root_layout}



So, what we got here?
We can create page as simply as creating a new .page file in the www root directory structure. We can assign variables which affects how the page in finally rendered into the .page file using smarty's {assign} function. Because the .page file is parsed first, the smarty tags are excecuted so we can use those template variables in the layout files when we render the page.

For example, the layout is choosed setting the layout -variable in the .page file. Offcouse, we can make default values for those and so on. I have also developped a system where I can set default values in special config files per directory basics. Those settings also affects the subdirectories. This way I can, for example, set default layout for whole subdirectory hierarchy from just one config file.

The layouts (yes, they are templates) are stored in different directory than other (small templates) outside wwwroot.
In other words:
I have /somedirectory/layouts where the layouts are.
then I have /somedirectory/templates where other templates (like small template which renders a menu or something in the page. these templates can be used in layouts or in .page files just as normally, using {include} tag.
Then I have the .page files which also are templates and which resists in the wwwroot hierarchy.

Offcourse I can use every smarty tag in the .page, layout and template files as normally.

So, when a graphican creates new html page, he only needs to know how to code HTML and a bit smarty. He writes the content of the page into a .page file and sets what layout he want's to use with the assign layout -smarty tag.

If we wan't some dynamic contents into the page, for example, data from database, we create a normal smarty function which gets the data and retuns it into a template variable. We can call this function in the .page document and use normal smarty tags to iterate over the results. Simply as that =)

Tell me what do you think and feel free to ask more =)

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


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

PostPosted: Wed Nov 26, 2003 5:37 pm    Post subject: Reply with quote

Hi jtm,

I like this Smile got a sample/demo? Does this mean that I can do sth like include 'http://example.com/example.page' from PHP and get a fully parsed/processed smarty file back? I was considering using the Streams api to do similar and was wondering if you had any thoughts on that.

Cheers!
Back to top
View user's profile Send private message
jtm
Smarty Rookie


Joined: 13 Nov 2003
Posts: 9

PostPosted: Thu Nov 27, 2003 10:37 am    Post subject: Reply with quote

boots wrote:
Hi jtm,

I like this Smile got a sample/demo? Does this mean that I can do sth like include 'http://example.com/example.page' from PHP and get a fully parsed/processed smarty file back? I was considering using the Streams api to do similar and was wondering if you had any thoughts on that.

Cheers!


Hmm. I'm not sure what you mean by that.

The idea of this is that you don't need to code any php. Normally with smarty,
you do the following:

Code:


a php file which you call from the browser

// Do database queries here etc
// and assign them to template variables ($smarty->assign)

$smarty->display($layout);



But here you do the database queries in smarty functions which are coded using php. You then call those smarty functions in the .page file.
They return the results into template variables which you then use to display the results in the .page file using normal smarty features (like foreach etc)

The core wrapper is a "Controller" (MVC, member? Smile
The .page view is the "Model" and the layouts are the "View".

So in normal situation you don't need to do any php to create a page. You just create a .page file, write the contents of the page there and assign some template variables in the .page file which the wrapper then use to select the correct layout etc.

You can offcourse link normally using <a href="someother.page"> in the .page file.

I'll try to grab an example into a tarball and post it here =)

- jtm
Back to top
View user's profile Send private message
monotreme
Smarty Regular


Joined: 22 Feb 2004
Posts: 97
Location: USA

PostPosted: Sun Feb 22, 2004 5:47 pm    Post subject: trying to get my head around this Reply with quote

Where is all your smarty setup in this scheme? Obviously in order to use smarty somewhere
you have to include the smarty class etc.
_________________
Your online 24/7 box office
http://www.tixrus.us
Back to top
View user's profile Send private message Visit poster's website
sprinklersystem
Smarty Rookie


Joined: 23 Apr 2006
Posts: 8

PostPosted: Mon Sep 04, 2006 9:00 am    Post subject: Reply with quote

this looks interesting but no good for me since I won't have apache access for
my current project.

But I was interested in how you did this, since I am currently looking to create a site structure that is scalable.
Back to top
View user's profile Send private message
mobbkopf
Smarty n00b


Joined: 28 Mar 2006
Posts: 4

PostPosted: Fri Feb 09, 2007 7:32 pm    Post subject: Reply with quote

Hi there,

@monotreme: all this is done in the wrapper.php file. The advantage is that you have to write only this one php file and that you'll never ever have to change anything in the PHP code directly.

This is more or less the same I did some time ago. Since my webhoster had no Apache webserver but one which didn't support the creation of forwarding rules, I've written one PHP file for the initialization, and copied and renamed it for every page I've created.

I also support this way of development since I think the processing layer is handled better in a Smarty template than in PHP directly because otherwise you'd have to fight with e. g. your GET/POST variable names in both PHP and Smarty. When you go this way, you can use one main template per page as "processing layer", keeping track of all variable names, link URLs and so on, and communicate with the "presentation layer" by simply including the "real" templates which hold the "real" HTML.

A great addition is, in my opinion, the {use object} command described here: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=7549 I've written this plugin some time ago to be able to execute database queries from out of my template. Here's an example:

Code:

/* object.user.php - All user management relevant functions and variables*/

class user
{
  function login($username, $password)
  {
    ...  // Calling this function creates a new session and returns true, if username and password are valid, otherwise it returns false.
  }
  function logout()
  {
    ... // returns true, if the logout attempt was successful, otherwise false.
  }
  function isLoggedIn()
  {
    ... // returns true, if I'm logged in, otherwise false.
  }
  ...
}


So now I'm able to write a main template like this one:

Code:

{* index.php - the site's main page. *}

{use object="user"}

{include file="header.tpl"}

{if $user->isLoggedIn()} {* user is already logged in *}
  {include file="menu/index.tpl"}
  {include file="main/index.tpl"}
{else}
  {if $smarty.post.login ne "" and $smarty.post.username ne "" and $smarty.post.password ne ""} {* user tries to log in *}
    {if $user->login($smarty.post.username, $smarty.post.password)} {* login successful *}
      {include file="message.tpl" message="Login correct."}
      {include file="menu/index.tpl"}
      {include file="main/index.tpl"}
    {else} {* login failed *}
      {include file="message.tpl" message="Login failed. Check your username and password and try it again."}
      {include file="accessdenied.tpl" username=$smarty.post.username password=$smarty.post.password}
    {/if}
  {else} {* user doesn't try to log in -> show the page for non-users *}
    {include file="accessdenied.tpl" username="" password=""}
  {/if}
{/if}

{include file="footer.tpl"}


As you can see... The whole user log-in logic reduced to approximately 20 lines of code, easy to understand and maintain (at least easier than any other solution I've seen) - and a good way to keep your page consistent, since the main templates are the only places where I deal with the GETT/POST variables and link URLs I'm using.


Sincerely,

Patrick
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks 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