TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   [Request tutorial] Template class (http://www.talkphp.com/advanced-php-programming/1214-request-tutorial-template-class.html)

Tanax 09-24-2007 06:23 PM

[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!

Haris 09-24-2007 07:39 PM

Elite, 1337, Pro template class available at http://www.phpsavant.com. Trust me, it's a reliable class.

Tanax 09-24-2007 08:14 PM

Yea, but I want my own, because it's easier to understand... I don't have a clue how to use savant.. :S

Izym 09-24-2007 08:51 PM

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).

Tanax 09-24-2007 08:54 PM

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 :)

Haris 09-24-2007 10:56 PM

Quote:

Originally Posted by Tanax (Post 2550)
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.

Tanax 09-25-2007 07:50 AM

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..

Salathe 09-25-2007 09:51 AM

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


Wildhoney 09-25-2007 10:34 AM

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

?>


Tanax 09-25-2007 02:28 PM

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)

Karl 09-25-2007 02:51 PM

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 :(

Tanax 09-25-2007 03:02 PM

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

Karl 09-25-2007 03:19 PM

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.

Salathe 09-25-2007 03:21 PM

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.

Tanax 09-25-2007 03:45 PM

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

But I'll look forward to both karl's and Izym's tutorials :D

fierceangel 10-09-2007 06:22 PM

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 ;)

Tanax 10-09-2007 07:26 PM

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 :)

cherries 10-10-2007 04:46 PM

Also using Smarty would be a good choice

Tanax 10-11-2007 04:52 PM

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 ..

sketchMedia 10-18-2007 04:35 PM

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.


All times are GMT. The time now is 12:25 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0