I'm working on a template system and I'm running into problems using it.

<?php
require 'tpleng.php'; //require the template class

$tpl = new Template();
$tpl->set('title', 'someTitle');

echo $tpl->fetch('templates/index.tpl');
?>

This works fine so long as tpleng.php is located in the same directory as the script calling it. However, if I change the path to tpleng.php to includes/tpleng.php, the template file is not parsed. PHP successfully includes the file, but returns blank page. I have no idea why this is. See the good demo and the bad demo.

The code for the template engine is rather simple (40 lines of code or so). Scroll down a bit. I've stripped out the caching functionality as I won't be using it. The rest remains unchanged.

If someone could take a peek and offer some advice, I would be most appreciative.

    Here's why:
    The template path is dependant upon the path of the tpleng.php file.

    So if you have public_html/templates/template.tpl as your template file, and your tplengine.php file is where your index.php is, then using the path templates/template.tpl will work. But if you move tplengine.php to another directory, like public_html/scripts/tpleng.php, you have to update the template path in your index.php page to be ../templates/template.tpl otherwise, there is nothing to parse.

    I ran into a similar problem, but figured it out, that's why I know.

    So if you want a more fluid example:

    + public_html
    |
    +-+ templates
    |..|
    |..|-- template.tpl
    |
    +-+ scripts
    |..|
    |..|-- tpleng.php
    |
    |-- index.php

    Using that setup, your code wont' work. Your index.php muse use the path as relative from tpleng.php to include the template which is: ../templates/template.php.

    + public_html
    |
    +-+ templates
    |..|
    |..|-- template.tpl
    |
    |-- tpleng.php
    |-- index.php

    Using this setup, your code is fine because the path from index.php is identical to what tpleng.php is using.

    Hope I cleared it up for you.

    ~Brett

      Write a Reply...