TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-24-2007, 06:23 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default [Request tutorial] Template class

Hey! I'm very interested in learning, but none of the tutorials I've found so far have covered the matter in a more deeper way, they've only scratched the surface. I wanna know more about how it works, and how to script it.

So I'm requesting some of you PHP pro's here on talkphp.net to make an article/tutorial on how to script an elite/1337/pro template class!
Tanax is offline  
Reply With Quote
Old 09-24-2007, 07:39 PM   #2 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Elite, 1337, Pro template class available at http://www.phpsavant.com. Trust me, it's a reliable class.
Haris is offline  
Reply With Quote
Old 09-24-2007, 08:14 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Yea, but I want my own, because it's easier to understand... I don't have a clue how to use savant.. :S
Tanax is offline  
Reply With Quote
Old 09-24-2007, 08:51 PM   #4 (permalink)
The Contributor
 
Izym's Avatar
 
Join Date: Sep 2007
Posts: 32
Thanks: 0
Izym is on a distinguished road
Default

Well, depends on how complex you want it to be. Im planning on writing one (customized for my CMF) soon, so i might write a tutorial about it soon. ;) However, i won't make it as advanced as my class is gonna be, also since its gonna be commercial (Not alone, but with my CMF).
Izym is offline  
Reply With Quote
Old 09-24-2007, 08:54 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Well, really I'm happy with whatever info I can get on the subject Izym.

I'd love to read your tutorial when it's done, perhaps you can show a preview of your page later :)
Tanax is offline  
Reply With Quote
Old 09-24-2007, 10:56 PM   #6 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Yea, but I want my own, because it's easier to understand... I don't have a clue how to use savant.. :S
Documentations are available for a purpose, duh.
Haris is offline  
Reply With Quote
Old 09-25-2007, 07:50 AM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

The documentations on their websites sucks :S And also, the layout of the website is horrible, no offense, but I can't find almost anything on that page..
Tanax is offline  
Reply With Quote
Old 09-25-2007, 09:51 AM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

The problem is that a "template class" is many things for many people. The requirements for a template class can vary from project to project. Because of that, it would be nice to know what you consider should be required features (and optional/nice to have features) in your "elite/1337/pro template class".

It could end up being something as simple as the following class, or 1,000s of lines of code.
PHP Code:
<?php

class Template
{
    public function 
display($szView)
    {
        if (
file_exists('views/' $szView '.php'))
        {
            include 
'views/' $szView '.php';
        }
    }
}

$tpl = new Template;
$tpl->display('example'); // views/example.php
Salathe is offline  
Reply With Quote
Old 09-25-2007, 10:34 AM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Salathe is spot on. Template files can be really extensive. However, to extend onto our simple template class, I would add the following code to store page states as well. That way you can have like so:
  • profile.default.php: Page to allow members to edit their profile
  • profile.complete.php: Page to inform users that profile update is complete
  • profile.error.php: Page to pull users back when they've left a required field empty

We create our views folder and then inside there we have our individual page folders, and then in our individual page folders we have the page states as listed above.

PHP Code:
<?php

define
('VIEW_DEFAULT''default');
define('VIEW_COMPLETE''complete');
define('VIEW_ERROR''error');

class 
Template
{
    public function 
display($szView$szState VIEW_DEFAULT)
    {
        
$szPage 'views/' $szView '/' $szView '.' $szState '.php';
        
        if (
file_exists($szPage))
        {
            include 
$szPage;
        }
    }
}

$tpl = new Template;
$tpl->display('example'VIEW_COMPLETE); // views/example/example.complete.php

?>
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-25-2007, 02:28 PM   #10 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Well, I mean a class that allows me to change the template of the website with a click from a menu, like on this forum.. you can quite easially change the theme from this theme to another(ofcourse you have to import it etc etc.. but you get the point).

And make a management system, like in vB edit template feature in the admin cp, where you can change the template code quite easially.

And they're using both php and html in the same document without any tags(don't quite understand how..), they write stuff like:
if($condition['show'] == 'member')
<a href="something.php">Forum index</a>


And that's how I would want it! To easially be able to CHANGE the templates imported, via an admin cp.

So, that's my pro/elite/1337 template class idea! :)
(hard - yes, impossible - noo :D)
Tanax is offline  
Reply With Quote
Old 09-25-2007, 02:51 PM   #11 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

I actually wrote a class to do this exact same thing yesteday. Essentially though, it's simply a template within a template. The outter template provides the layout, such as:

PHP Code:
<html>
<
head>
<
title>$pageTitle</title>
</
head>
<
body>
$pageContent
</body>
</
html
You then render your inner template (I call them Views - from an MVC pattern), store it in a string and push it to the layout as $pageContent. I will wrire up a tutorial for a simplified version of this soon, however it wont be for a few days as I am currently busy with other things :(
Karl is offline  
Reply With Quote
Old 09-25-2007, 03:02 PM   #12 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

That would be awesome! :D

One thing though, how do they do this:

<span id="something">Hello and welcome {username}</span>

Which would outprint the username of the current logged in user..

I'm just curious(as always :D), how they can do with curly brackets instead of $username
Tanax is offline  
Reply With Quote
Old 09-25-2007, 03:19 PM   #13 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Well it's just a matter of preference really. In the above example, the template would be parsed and {username} would be replaced during this stage. Parsing like that can slow down templates though.
Karl is offline  
Reply With Quote
Old 09-25-2007, 03:21 PM   #14 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

It's just a matter of going through the template text with str_replace, preg_replace or similar. Again, I'm sure someone has the time to write up a tutorial or quick example but I don't today.
Salathe is offline  
Reply With Quote
Old 09-25-2007, 03:45 PM   #15 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Ah, okey, I get the idea.. :)

But I'll look forward to both karl's and Izym's tutorials :D
Tanax is offline  
Reply With Quote
Old 10-09-2007, 06:22 PM   #16 (permalink)
The Visitor
 
Join Date: Oct 2007
Posts: 1
Thanks: 0
fierceangel is on a distinguished road
Default

I recently developed a small script that I am using for a small business website. I'll show the code first then describe what the function does.

template.html

Code:
<html>
<head>
<title>My Template</title>
</head>
<body>
<h2>{PAGE_TITLE}</h2>
<p>{WELCOME_TEXT}</p>
</body>
</html>
functions.php

PHP Code:
function parseTemplate($content$templatefile) {

     
$html implode(""file($templatefile));

          foreach(
$content as $key => $value) {
               
$html str_replace('{' $key '}'$value$html);
          }

     return 
$html;


index.php

PHP Code:
include('functions.php');

$content['PAGE_TITLE']     = "My Website";    
$content['WELCOME_TEXT'] = "Welcome to my website!";

echo 
parseTemplate($content'template.html'); 
My script will read the file (in this example, template.html) into a string, then do a simple foreach loop on an array that has this structure - "CONTENT_PLACEHOLDER" => "CONTENT".

Each time the loop goes around it will replace the 'placeholder' with the content. Once the loop is finished the content is displayed on the page.

The script isn't perfect, but I'm new(ish) to this too ;)
fierceangel is offline  
Reply With Quote
Old 10-09-2007, 07:26 PM   #17 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

That looks nice :)

And it's not far from perfect ;) It's just that this is only got to do with the TEXT on the webpage. I want to be able to change the actual style of the page :)
Tanax is offline  
Reply With Quote
Old 10-10-2007, 04:46 PM   #18 (permalink)
The Wanderer
 
cherries's Avatar
 
Join Date: Oct 2007
Posts: 20
Thanks: 0
cherries is an unknown quantity at this point
Default

Also using Smarty would be a good choice
cherries is offline  
Reply With Quote
Old 10-11-2007, 04:52 PM   #19 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Urg, as I said earlier, I wanna use my own template class, and not some already made-ready to use- template class.

So, smarty and savant goes down ..
Tanax is offline  
Reply With Quote
Old 10-18-2007, 04:35 PM   #20 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Smarty seems quite easy to use tbh, but if u really wanna do your own im sure that you could build on some of the examples above, just tinker with them, its the best way to teach yourself PHP or indeed any langauge (i mean learn by example, i dont mean copy code and just use it, that would get u nowhere).

a simple smart template could be constructed like this:
PHP Code:
<?php
require 'Smarty.class.php';
$smarty = new Smarty;//create new smarty object

$smarty->assign('Username''Sam');
//assign smarty variable Username with the value 'Sam' in the template file

$smarty->display('index.tpl');
//tell smarty which template file you want to use
?>
now we need to create the "index.tpl" template page that we told smarty to use

HTML Code:
<html>
<body>
        Welcome {$Username} <!--Smarty variable-->
</body>
</html>
Will output: "Welcome Sam" when run

simple really and it provides the much coverted seperation between logic and presentation, which makes scripts alot easier to maintain and extend.
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:41 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design