 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
09-24-2007, 06:23 PM
|
#1 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
[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!
|
|
|
|
09-24-2007, 07:39 PM
|
#2 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
Elite, 1337, Pro template class available at http://www.phpsavant.com. Trust me, it's a reliable class.
|
|
|
|
09-24-2007, 08:14 PM
|
#3 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Yea, but I want my own, because it's easier to understand... I don't have a clue how to use savant.. :S
|
|
|
|
09-24-2007, 08:51 PM
|
#4 (permalink)
|
|
The Contributor
Join Date: Sep 2007
Posts: 32
Thanks: 0
|
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).
|
|
|
|
09-24-2007, 08:54 PM
|
#5 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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 :)
|
|
|
|
09-24-2007, 10:56 PM
|
#6 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
Quote:
Originally Posted by Tanax
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.
|
|
|
|
09-25-2007, 07:50 AM
|
#7 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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..
|
|
|
|
09-25-2007, 09:51 AM
|
#8 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
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
|
|
|
|
09-25-2007, 10:34 AM
|
#9 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
09-25-2007, 02:28 PM
|
#10 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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)
|
|
|
|
09-25-2007, 02:51 PM
|
#11 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
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 :(
|
|
|
|
09-25-2007, 03:02 PM
|
#12 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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
|
|
|
|
09-25-2007, 03:19 PM
|
#13 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
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.
|
|
|
|
09-25-2007, 03:21 PM
|
#14 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
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.
|
|
|
|
09-25-2007, 03:45 PM
|
#15 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Ah, okey, I get the idea.. :)
But I'll look forward to both karl's and Izym's tutorials :D
|
|
|
|
10-09-2007, 06:22 PM
|
#16 (permalink)
|
|
The Visitor
Join Date: Oct 2007
Posts: 1
Thanks: 0
|
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 ;)
|
|
|
|
10-09-2007, 07:26 PM
|
#17 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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 :)
|
|
|
|
10-10-2007, 04:46 PM
|
#18 (permalink)
|
|
The Wanderer
Join Date: Oct 2007
Posts: 20
Thanks: 0
|
Also using Smarty would be a good choice
|
|
|
|
10-11-2007, 04:52 PM
|
#19 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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 ..
|
|
|
|
10-18-2007, 04:35 PM
|
#20 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
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.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|