Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP template class with variables?

I want to make developing on my new projects easier, and I wanted a bare bones very simple template engine solution. I looked around on the net and everything is either too bloated, or makes me cringe.

My HTML files will be like so:

<html>
    <head>
        <title>{PAGE_TITLE}</title>
    </head>
    <body>
        <h1>{PAGE_HEADER}</h1>
        <p>Some random content that is likely not to be parsed with PHP.</p>
    </body>
</html>

Obviously, I want to replace {PAGE_TITLE} and {PAGE_HEADER} with something I set with PHP. Like this:

<?php

    $pageElements = array(
                            '{PAGE_TITLE}' => 'Some random title.',
                            '{PAGE_HEADER}' => 'A page header!'
                         );

?>

And I'd use something like str_replace and load the replaced HTML into a string, then print it to the page? This is what I'm on the path towards doing at the moment... does anyone have any advice or a way I can do this better?

Thanks.

like image 811
Joe Avatar asked Apr 01 '10 23:04

Joe


3 Answers

Your current strategy will work, and is pretty straightforward. str_replace() is efficient and clean, and you can simply loop it to replace exact tag matches with your variable content. However, the drawback is that you have to load all your templates into strings first, and that can be pretty inefficient.

An alternate method that's very similar, is you can simply use extract(). Extract will take a set of key/value pairs and create variables out of them in the local scope. If you include() a template in the same scope, your variables will be ready to go.

Something like this:

function loadTemplate($template,$vars)
{
    extract($vars);
    include($template);
}

Your template could just be regular PHP.

<html>
    <head>
        <title><?php echo $PAGE_TITLE ?></title>
    </head>
    <body>
        <h1><?php echo $PAGE_HEADER ?></h1>
        <p>Some random content that is likely not to be parsed with PHP.</p>
    </body>
</html>

(Obviously you could use short tags for less template verbosity, although I prefer not to for compatibility reasons.)

Then all you have to do is:

$pageElements = array(
                        'PAGE_TITLE' => 'Some random title.',
                        'PAGE_HEADER' => 'A page header!'
                     );
loadTemplate('file.phtml',$pageElements);
like image 111
zombat Avatar answered Nov 09 '22 09:11

zombat


You might be interested in mustache.php. It's a really lightweight PHP class implementation of Mustache

Quick example taken from the README:

<?php
    include('Mustache.php');
    $m = new Mustache;
    echo $m->render('Hello {{planet}}', array('planet' => 'World!'));
    // "Hello World!"
?>

And a more in-depth example--this is the canonical Mustache template:

Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{taxed_value}}, after taxes.
{{/in_ca}}
like image 5
whalesalad Avatar answered Nov 09 '22 07:11

whalesalad


PHP itself is a template's engine if you want to be really simple. just use include()

file.phtml:

<html>
    <head>
        <title><?=$tpl['PAGE_TITLE']?></title>
    </head>
    <body>
        <h1><?=$tpl['PAGE_HEADER']?></h1>
        <p>Some random content that is likely not to be parsed with PHP.</p>
    </body>
</html>

code.php:

tpl = Array 
(
'PAGE_HEADER' => "This is the lazy way to do it",
'PAGE_TITLE' => "I don't care because i am doing it this way anyways"
)
include("file.phtml")
like image 2
Byron Whitlock Avatar answered Nov 09 '22 07:11

Byron Whitlock