Welcome to WebmasterWorld Guest from 54.166.152.121

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Template Parser Issue

     
1:29 pm on Jan 22, 2011 (gmt 0)

5+ Year Member



I'm currently having some issues with a PHP template parser class I've been working on. To begin with, here is the code:

<?php

// template.php

class template
{
public $output;

// Function to set the file names

function set_filenames($filenames = array())
{
foreach ($filenames as $files)
{
$this->output = file_get_contents($files);

return $this->output;
}
}

// Function to define the language file

function define_language($language = array())
{
foreach ($language as $placeholder => $conversion)
{
$this->output = str_replace('{' . $placeholder . '}',$conversion,$this->output);
}

echo $this->output;
}
}

?>


<?php

// viewcontent.php

// Include the file

require('template.php');

// Instantiate the class

$template = new template();

$lang = array(
'TITLE'=>'Template Parser',
'CONTENT'=>'<p>This is an example of the template parser.</p>',
'MORE_CONTENT'=>'<p>This is some more content.</p>',
'EXAMPLE_LINK'=>'<p><a href="link.php">Example Link</a></p>',
'CONTENT_AGAIN'=>'<p>Footer content.</p>'
);

// Display the page

$template->set_filenames(array(
'overall_content.html',
'overall_footer.html'));

$template->define_language($lang);

?>


Basically, only the first item in the array under the set_filenames() function is being output on the page; meaning 'overall_footer.html' is not visible. I've tried re-ordering some code and even called the function twice (rather than using an array), but still no luck. I'd rather use an array to set the file names as it keeps everything tidy and in one place.

Any ideas on how to solve this will be greatly appreciated. Thanks in advance. :)
1:19 pm on Jan 26, 2011 (gmt 0)

5+ Year Member



Only had a quick look at this, but from what I can see all your uses of $this->output in your functions are straight assignment, you don't seem to be doing any concatenation, so surely you are just overwriting what's already there? Based on that though I'd expect footer to be all that's returned. But then again you are returning this->output after the first loop through.

Have you tried


function set_filenames($filenames = array())
{
$this->output="";
foreach ($filenames as $files)
{
$this->output .= file_get_contents($files);
}
return $this->output;
}
 

Featured Threads

Hot Threads This Week

Hot Threads This Month