Using strtr content ends up in improper place

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
coffejor
Forum Newbie
Posts: 2
Joined: Mon Sep 08, 2008 6:00 pm

Using strtr content ends up in improper place

Post by coffejor »

Hello all,

I'm creating a template engine to replace placeholder strings in my html template with dynamic content served by a function. To do this I have written the following template processor:

Code: Select all

function load_template()
{
    $template_file = "template.html";
    $template = fread(fopen($template_file, 'r'), filesize($template_file));
    $template_vars = array('{MAIN}' => bodyContent(), '{NAV}' => navContent(), '{TITLE}' => $title);
 
    function template_eval (&$template, &$template_vars)
    {
        return strtr($template, $template_vars);
    }
 
    echo template_eval($template, $template_vars);
}
 
load_template();
which is called by a page like so:

Code: Select all

<?php
$title = "Example Title";
 
function navContent()
{
?>
                    <h1>Page Links:</h1>
                    <ul>
                        <li><a href="">Link 1</a></li>
                        <li><a href="">Link 2</a></li>
                        <li><a href="">Link 3</a></li>
                    </ul>
<?php
}
 
function bodyContent()
{
    ?>
 
                    <h1>Example Title 1</h1>
                    <p>Example Content 1</p>
                    <h1>Example Title 2</h1>
                    <p>Example Content 2</p>
                    <h1>Example Title 3</h1>
                    <p>Example Content 3</p>
<?php
}
 
    require_once (URL."/test_templateProcessor.php");
?>
Unfortunately, the content fails to be placed into the appropriate place in the template but, rather, ends on top of the template as can be seen here: http://www.jordancoffey.com/test_index.php.

I'm not quite sure why it does this, so any insight would be greatly appreciated. Thanks in advance for any and all help!

Jordan
coffejor
Forum Newbie
Posts: 2
Joined: Mon Sep 08, 2008 6:00 pm

Re: Using strtr content ends up in improper place

Post by coffejor »

I've updated my templateProcessor script to the following:

Code: Select all

<?php
function load_template()
{
    $template_file = "template.html";
    $template = file_get_contents($template_file);
    $template_vars = array('{MAIN}', '{NAV}', '{TITLE}');
    $template_vals = array(bodyContent(), navContent(), $title);
 
    function template_eval (&$template, &$template_vars, &$template_vals)
    {
        return str_replace($template_vars, $template_vals, $template);
    }
 
    echo template_eval($template, $template_vars, $template_vals);
}
 
load_template();
?>
in an attempt to address the problem, but with no result. Any suggestions would be greatly appreciated.

~Jordan
Post Reply