Jump to content

[SOLVED] Help with template class - include statements


john010117

Recommended Posts

I have managed to build a small template class that can so far parse template variables. Now, to move one step further, I have begun to put in a template include part (includes separate templates within a template file). Here's what I have so far (only the include parts are shown):

 

<?php
class template
{
var $template_root = './template';
var $output;

function _include_template($filename)
{
	ob_start();
	include($this->template_root . '/' . $filename);
	$content = ob_get_contents();
	ob_end_clean();
	return $content;
}

function compile($filename)
{
	$this->output = @file_get_contents($this->template_root . '/' . $filename);

	// Locates all the include statements (looks like: <!-- INCLUDE some_file.html -->)
	preg_match_all('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\./]+) -->#', $this->output, $include_matches);

	if(!empty($include_matches[1]))
	{
		for($i = 0, $size = sizeof($include_matches[1]); $i < $size; $i++)
		{
			$this->output = preg_replace('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\./]+) -->#', $this->_include_template($include_matches[1][0]), $this->output);
			array_shift($include_matches[1]);
		}
	}
	echo $this->display();
}

function display()
{
	return $this->output;
}
}
?>

 

If I include one file in a template file, the file shows up fine. However, if I include two or more files, the first file that was included in the template keeps on getting included over and over again. Does anyone see a problem with this code? Help will be much appreciated.

Link to comment
Share on other sites

explain !

However, if I include two or more files, the first file that was included in the template keeps on getting included over and over again.

 

What I mean is this:

 

Let's say I had the template file "index_body.html" (called from index.php). Then, in that template file, I include "index_2.html". So far, so good. But, if I include "index_3.html" or any other file below the first include statement, only "index_2.html" shows up within "index_body.html". That happens  the number of include statements (stated in index_body.html) times.

Link to comment
Share on other sites

maybe

instead of

		for($i = 0, $size = sizeof($include_matches[1]); $i < $size; $i++)
		{
			$this->output = preg_replace('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\./]+) -->#', $this->_include_template($include_matches[1][0]), $this->output);
			array_shift($include_matches[1]);
		}

 

try

		foreach($include_matches[1] as $IM)
		{
			$this->output = preg_replace('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\./]+) -->#', $this->_include_template($IM), $this->output);
		}

Link to comment
Share on other sites

Ok, I tried that method, but that outputted no difference. To make sure, I've echo'ed the value of $IM within the loop, and it shows what it's supposed to show. Here's the updated code:

 

<?php
class template
{
var $template_root = './template';
var $output;

function _include_template($filename)
{
	ob_start();
	include($this->template_root . '/' . $filename);
	$content = ob_get_contents();
	ob_end_clean();
	return $content;
}

function compile($filename)
{
	$this->output = @file_get_contents($this->template_root . '/' . $filename);

	preg_match_all('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\./]+) -->#', $this->output, $include_matches);

	if(!empty($include_matches[1]))
	{
		foreach($include_matches[1] as $IM)
		{
			$this->output = preg_replace('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\./]+) -->#', $this->_include_template($IM), $this->output);
		}
	}
	echo $this->display();
}

function display()
{
	return $this->output;
}
}
?>

 

I'm just guessing, but could the problem lie in the _include_template() function with the output buffering?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.