Page 1 of 1
simple template help
Posted: Mon Oct 20, 2008 12:39 am
by kzinner
Hi,
I am trying to figure out how to do some simple templating with my site. Essentially all I want to do is be able to pull values from a file and input them where I want in my html.
I would just like 1 file to have something like
Link1=
http://example.com
Link1=
http://example2.com
Contactemail=
email@email.com
Then be able to pull each where I want. So far all I have been able to figure out is how to include an entire file using include. I'm not sure what to search for or look into to just pull the values I want. Any help would be greatly appreciated.
Thank you
Re: simple template help
Posted: Mon Oct 20, 2008 1:53 am
by omniuni
OK, I'm going to take an object oriented approach here....
We will create a file "templatefunctions.php" and apply it to "index.php".
templatefunctions.php
Code: Select all
<?php
function insertvalue($descriptor){
switch($descriptor){
case 'name':echo 'OmniUni'; break;
case 'forum':echo 'DevNetwork';break;
default: echo '[Not Set]';
}
}
?>
Now, to apply this in an index.php to print the text "OmniUni is a member of DevNetwork, and lives in NC." we would do as follows. Note, however, that I have not in the insertvalue() function created a result for the state I live in.
index.php
Code: Select all
<?php insertvalue('name');?> is a member of <?php insertvalue('forum');?>, and lives in <?php insertvalue('state');?>.
The output would be:
OmniUni is a member of DevNetwork and lives in [Not Set].
Again, remember I did not set a value for the state I live in, so therefore the switch uses the default case.
Hope that helps,
OmniUni
Re: simple template help
Posted: Mon Oct 20, 2008 11:28 am
by inet411
Ok, well then I'm going to take a procedural approach here....
file: templateVars.php
Code: Select all
<?PHP
$title = "Site Title";
$site_name = "Site Name";
$stuff = "Stuff";
?>
file: index.php
Code: Select all
<?PHP
include('templateVars.php');
?>
<html>
<head>
<title><?=$title?></title>
</head>
<body>
Welcome to <?=$site_name?>.<br />
We have lots of stuff on this site like <?=$stuff?>.
</body>
</html>
Output:
Welcome to Site Name.
We have lots of stuff on this site like Stuff.
Re: simple template help
Posted: Mon Oct 20, 2008 2:19 pm
by pickle
Both approaches are quite simple & make sense for this application. However, in my mind, the OO approach provides for much more expansion & cleanliness in the future. For example, an object would help you namespace the variables. Using procedural like you wrote, you couldn't use the variable $title for anything else because it would get displayed in the template. There's also no way to tell if a variable is for the template or not, or if a variable you're setting in run-time is overwriting a pre-set template variable.
I'd use the magic __get(), and __set() functions rather than a new function, so you could do something like this:
Code: Select all
<?PHP
class TemplateVars
{
private $vars;
function __set($name,$value)
{
$this->vars[$name] = $value;
}
function __get($name)
{
return $this->vars[$name];
}
}
?>
Use it like so:
Code: Select all
<?PHP
$tpl = new TemplateVars();
$tpl->title = 'My page';
$tpl->email = 'contact@email.com';
?>
<html>
<head>
<title>
<?= $tpl->title ?>
</title>
</head>
<body>
...
</body>
</html>
This approach is nothing new - some template engines work just like this. The OO approach would also allow you to modify __set() and __get() (and perhaps use a different approach) to have variable escaping, or other functionality.
Re: simple template help
Posted: Mon Oct 20, 2008 3:37 pm
by omniuni
oooooo pretty code pickle! (haha)
I'm still new to OO, I may look to this example later to get past basic functions....

Re: simple template help
Posted: Wed Oct 22, 2008 2:40 pm
by inet411
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
pickle wrote:Both approaches are quite simple & make sense for this application. However, in my mind, the OO approach provides for much more expansion & cleanliness in the future. For example, an object would help you namespace the variables. Using procedural like you wrote, you couldn't use the variable $title for anything else because it would get displayed in the template. There's also no way to tell if a variable is for the template or not, or if a variable you're setting in run-time is overwriting a pre-set template variable.
I'd use the magic __get(), and __set() functions rather than a new function, so you could do something like this:
Code: Select all
<?PHP
class TemplateVars
{
private $vars;
function __set($name,$value)
{
$this->vars[$name] = $value;
}
function __get($name)
{
return $this->vars[$name];
}
}
?>
Use it like so:
Code: Select all
<?PHP
$tpl = new TemplateVars();
$tpl->title = 'My page';
$tpl->email = 'contact@email.com';
?>
<html>
<head>
<title>
<?= $tpl->title ?>
</title>
</head>
<body>
...
</body>
</html>
This approach is nothing new - some template engines work just like this. The OO approach would also allow you to modify __set() and __get() (and perhaps use a different approach) to have variable escaping, or other functionality.
That's cool.
I'm not an oop'er so take it easy on me, but I found your code cool. What about something like this? Am I doing this right and what are the pros/cons of doing it this way? (I've tried to merge your code and mine)
template.class.php
Code: Select all
<?PHP
Class TemplateVars {
private $vars = array();
function set($varname, $value) {
$this->vars[$varname] = $value;
return true;
}
function show($template_name) {
global $site_url; // for the url path so you only have to define the page. Not sure what to do here??
$path = $site_url . $template_name;
// Load vars here (using variable variables so they are available to the file we are about to include)
foreach ($this->vars as $key => $value) {
$$key = $value;
}
include($path);
}
}
?>
index.php
Code: Select all
<?PHP
include('template.class.php');
//This file is for php code and processing etc.
//No html at all here
$title = 'Hello World!';
$templateObj = new TemplateVars;
$templateObj->set('title',$title);
$templateObj->show('template.php');
?>
template.php
Code: Select all
<html>
<head>
<title>
<?=$title?>
</title>
</head>
<body>
<!--This file is for html only (well...and the occasional <?=$var?>
) -->
</body>
</html>
That way you have everything separate and now you can have a bunch of template files (index_template.php, about_template.php) and a bunch of processing/controlling files (index.php, about.php) and each could call on the template it wants to use.
You could even use one template file with multiple controlling files. Or multiple template files with one controlling file, or multiple/multiple etc...
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Re: simple template help
Posted: Wed Oct 22, 2008 2:53 pm
by pickle
What you've built is pretty much exactly like how TemplateLite and Smarty (two template engines) are used.
Some thoughts:
- If you don't know OO too well, you probably won't notice the difference between my __set() function and your set() function. But look closely at how variables are set in my code as opposed to yours. __set() is a magic function you can write that automatically gets called when you try to set a class's private variable. So, while you have to use $tpl->set('title','myTitle') for your code, you can simply use $tpl->title = 'myTitle' for my code. This is assuming you're running PHP5. If you've got PHP4, __set() doesn't exist & your method is fine.
- A danger with your show() function is: what happens if someone has set a "path" variable? It would then get overridden. What if somehow my $path gets set to "/var/www/html/super.secure.database.credentials.inc"? That file would then be displayed. I think in that situation, it's best to make $path a class variable, and refer to it as $this->path, rather than just $path. That way, any variables that are set & meant for the template, don't override any class variables.
- A similar danger is if you set a $site_url variable for your template. Since you're referencing it in a global context, if it gets overwritten in your foreach() loop, it would get overwritten for the whole site. I'd recommend making it a constant with define().
Re: simple template help
Posted: Fri Oct 24, 2008 1:50 am
by omniuni
I would mention two things:
1. Keep it simple. Always a good practice. Use PHP's functions when available.
2. Watch out for calling anything global. It's generally not a great practice.