I'm writing a CMS-style application, and I can't quite figure out templating.

I have a function that returns the file contents of a templaet file within a themes directory. That part works fine. Inside one of those files is a mix between HTML and PHP variables, for example:

<html>
<title>$title</title>
<link href="$theme['css-path']" type="text/css" rel="stylesheet">
...
</html>

<!-- Just an example. ;) -->

And no matter what combination of eval(), echo and variables I use, I simply cannot get the correct page output WITH the variables parsed. I'm really puzzled... and I know it's possible, just look at vBulletin or MyBB. There are variables in those template files and that software works perfectly.

Any tips on templates would be greatly appreciated.

I think I noticed PHPBB using the "execute" command. Have you done anything to trace the loading of those template files? (I've done a bit with PHPBB, but I was installing mods, not figuring out templating.)

execute is a PHPBB-specific function which probably uses eval() anyway (going to php.net/execute doesn't exist, and exec() executes shell commands on Linux). And what do you mean by 'trace' them?

<html>
<title><?php echo $title ?></title>
<link href=" <?php echo $stylepath ?> " type="text/css" rel="stylesheet">
<body>
 
CONTENT HERE
 
</body>
</html>

i think that should do it?

By "tracing" I meant to look at how the templates are loaded and used by one of your cited examples.

PHPBB was using "eval", you're right.

Oh, I get it. It just stores a list of variables and values and replaces accordingly. Not really what I wanted, but I guess it'll have to do. It's good to know something like that exists, thanks.

Oh, I get it. It just stores a list of variables and values and replaces accordingly. Not really what I wanted, but I guess it'll have to do. It's good to know something like that exists, thanks.

PHP was originally was a templating syntax.

Personally I believe you don't need templates since the PHP syntax is so easy to understand.
You just need to define a few variables and make them available to a php page that serves as the template.

Then you have a page such as jbennet pointed out.

<html>
<title><?php echo $title ?></title>
<link href=" <?php echo $stylepath ?> " type="text/css" rel="stylesheet">
<body>
 
CONTENT HERE
 
</body>
</html>

The only drawback with this is that it allows PHP functions to be defined in the template page (which smarty eliminates). However, if you trust the template creator then you don't have to worry.

The templates for the Joomla CMS are done in this way. And I believe its much more flexible and easier to use. With smarty you have to learn the smarty templating syntax. If you create your own templating syntax, thats another learning curve for the template creator.

To create a new "namespace" for your template, just place it inside a function. That way and new PHP variables defined in the template, only exist in the scope of the template.

eg:

// say you want the head section of your page templatable

$head = new Array();
$head['title'] = 'this is an example';
$head['keywords'] = 'example, template';

loadHeadTemplate($head);

function loadHeadTemplate(&$head) {
require('head.php');
}

So the head.php template could have:

<head>
<title><?php echo $head['title']; ?></title>
<meta name="keywords" content="<?php echo $head['keywords']; ?>" />
</head>

Its very easy to implement. And the template syntax is quite straight forward... don't you think?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.