Re: Templates

[email protected] (J-F Mammet)
Newsgroups php.template
Message-ID <[email protected]>
>Sure, go ahead. The more ideas we get, the better.
Ok this will be quite long.

First you have to understand that my system is very database centric, 
because for want it to be fast.

My main goal was to put most of the parsing out of the script, so I have an 
intranet that parses my templates and stores how they link between them in 
the database. That way I only have to do one SQL select and a "simple" loop 
on the result to get all the templates ready to get parsed with the dynamic 
values. My scripts are quite fast now.

Here is my structure. Sorry for the french names but it should be quite 
easy to understand.

CREATE TABLE Contenus (
   Numero_Auto int(11) NOT NULL auto_increment,
   Nom varchar(50) DEFAULT '' NOT NULL,
   Langue varchar(10) DEFAULT '' NOT NULL,
   Contenu text,
   Page varchar(50) DEFAULT '' NOT NULL,
   PRIMARY KEY (Numero_Auto),
   KEY Langue (Langue),
   KEY Nom (Nom),
   KEY NomLangue (Nom,Langue),
   KEY Page (Page),
   KEY NomLanguePage (Nom,Langue,Page),
   KEY NomPage (Nom,Page)
);

#
# Table structure for table 'Langues'
#
CREATE TABLE Langues (
   Numero_Auto int(11) NOT NULL auto_increment,
   Nom varchar(50) DEFAULT '' NOT NULL,
   Symbole varchar(10) DEFAULT '' NOT NULL,
   Priorite_ENG int(11) DEFAULT '0' NOT NULL,
   Priorite_FR int(11) DEFAULT '0' NOT NULL,
   Priorite_DE int(11) DEFAULT '0' NOT NULL,
   PRIMARY KEY (Numero_Auto)
);

#
# Table structure for table 'Liens'
#
CREATE TABLE Liens (
   Numero_Auto int(11) NOT NULL auto_increment,
   Page varchar(50) DEFAULT '' NOT NULL,
   Nom varchar(50) DEFAULT '' NOT NULL,
   Lien varchar(50) DEFAULT '' NOT NULL,
   Langue varchar(10) DEFAULT '' NOT NULL,
   PRIMARY KEY (Numero_Auto),
   KEY Page (Page),
   KEY Nom (Nom),
   KEY NomPage (Nom,Page),
   KEY NumNomPage (Nom,Page,Numero_Auto)
);

#
# Table structure for table 'Templates'
#
CREATE TABLE Templates (
   Numero_Auto int(11) NOT NULL auto_increment,
   Page varchar(50) DEFAULT '' NOT NULL,
   Nom varchar(50) DEFAULT '' NOT NULL,
   Type int(11) DEFAULT '0' NOT NULL,
   PRIMARY KEY (Numero_Auto),
   KEY Page (Page),
   KEY Nom (Nom),
   KEY NomPage (Nom,Page),
   KEY NumNomPage (Nom,Page,Numero_Auto)
);

The main SQL select is :
SELECT 
Templates.Page,Templates.Nom,Contenus.Langue,Contenus.Contenu,Liens.Lien 
FROM Templates LEFT JOIN Contenus USING(Nom,Page) LEFT JOIN Liens ON 
(Contenus.Nom = Liens.Nom AND Contenus.Page = Liens.Page) STRAIGHT_JOIN 
Langues WHERE Langues.Symbole = Contenus.Langue AND Templates.Page = 
'$Page' OR Templates.Page = '__common' ORDER BY 
Templates.Nom,Page,Langues.Priorite_$Langue"

It takes .01 seconds in a complex page.

You can see I order everything by the choosen language at the end. This 
allows me to have content common for all languages, but also have 
differences for specific templates. I have also a "common" template for all 
pages where I can store all the headers/footer and others. This allows me 
to have a common look on all pages, but also to have a specific one for my 
page.

My first loop then stores all the contents and "links" in a multi sized 
array called $Templates.

After this step I parse all my dynamic templates.

If do a simple check like
if ($Templates["In_Memory"]["category"])
{
    loop on all values
       Set values in an array
    Parsearray in template
}

Of course sometimes on very complex pages I have to nest 3 or more of these 
loops. I tried to optimize it a lot, that's why I put everything inside an 
array and call the parsing function only once. This speeds everything a lot.

After all my dynamic templates are parsed, I finally can parse all 
templates into a big string with a lot of str_replace, then echo it (there 
is a bug with PHP4 here, doing a echo on a 63000 bytes string takes 3 
seconds!).

A quite complex page take .2 seconds to parse. This is quite good I think.

I can personnaly give access to my templates intranet so you can see how it 
works, but sorry not for everyone. I would love to gpl this, but I'm pretty 
sure my boss would not like this 8)

Feel free to ask me any question.

J-F Mammet
[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.