homepage Welcome to WebmasterWorld Guest from 54.163.91.250
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Template Parser Issue
Sp4rkyM4rk




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

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. :)

 

Tommybs




msg:4258314
 1:19 pm on Jan 26, 2011 (gmt 0)

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;
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved