Passing HTML into Template Page

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
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Passing HTML into Template Page

Post by millsy007 »

I have set up two pages, content.php and template.php, I was the content.php file to contain a variable that will contain all the html for my content that will then be displayed within my template.php file, mostly this works fine. However the problem I have is that I want additional html in my content.php file so that when other people are editing it looks more like the finished file. My problem is that when I add this additional html it is being passed into the template.php file even though it is not within the content variable. Hopefully this code snippet will explain it:

template.php

Code: Select all

<html><head>head><body
<table width="100%" cellpadding="10" cellspacing="0"><tr><td width="30%" valign="top" bgcolor="#EEEEEE">
Beach Hostel is located right next to one of Holland’s beautiful beaches.   There’s nothing that can beat the sound of waves and the tranquility of the   beach-life, especially after you’ve been surrounded by the madness of the city   (of Amsterdam) for a while. This Flying Pig Hostel is known for offering you the   best chill out spot you can imagine; in this backpacker hostel you can relax,   smoke, hang out with friends, party or challenge your limits and abilities in   various beach sports!
</td>
<td width="70%" valign="top">
<?php echo $content; ?>
</td></tr></table>
</body></html>
content.php

Code: Select all

<p>TEST CONTENT - this is displayed even though it is outside the php tags?</p>
 
<?php ob_start(); ?>
    
<p>The Beach Hostel is located right next to one of Holland’s beautiful beaches. There’s nothing that can beat the sound of waves and the tranquility of the beach-life, especially after you’ve been surrounded by the madness of the city (of Amsterdam) for a while. This Flying Pig Hostel is known for offering you the best chill out spot you can imagine; in this backpacker hostel you can relax, smoke, hang out with friends, party or challenge your limits and abilities in various beach sports!</p>
<?php 
//Assign all Page Specific variables  
$content = ob_get_contents();  
ob_end_clean();   
//Apply the template  
include("template.php");
?>
Why is the <p>Test..... part being passed accross and displayed in the template file?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Passing HTML into Template Page

Post by aceconcepts »

Because you've written it before you have included template.php.

You also need to review you head and body tags.
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Passing HTML into Template Page

Post by millsy007 »

I am a little confused, where should I put the include("template.php"); line?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Passing HTML into Template Page

Post by aceconcepts »

It looks like you call content.php first and then display template.php as an include right?

So, as soon as you call content.php, the script is processed in a linear fashion - so, the first line:

Code: Select all

<p>TEST CONTENT - this is displayed even though it is outside the php tags?</p>
Will be displayed regardless.
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Passing HTML into Template Page

Post by millsy007 »

Okay I see what you mean now, is there anyway around this, at the moment it means that when the content page is edited the layout bears no resemblance to how it looks when contained within the final template page.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Passing HTML into Template Page

Post by aceconcepts »

You could use a url variable in order to display the test content.

Say for example the url you use to visit your content is http://www.site.com/content.php, you could append this url with a variable like this: http://www.site.com/content.php?test=1

From your content page you could base the test text on the url variable:

Code: Select all

 
<?
if(isset($_REQUEST['test']) && $_REQUEST['test']==1)
{
echo'<p>TEST CONTENT - this is displayed even though it is outside the php tags?</p>';
 
echo'<p>The Beach Hostel is located right next to one of Holland’s beautiful beaches. There’s nothing that can beat the sound of waves and the tranquility of the beach-life, especially after you’ve been surrounded by the madness of the city (of Amsterdam) for a while. This Flying Pig Hostel is known for offering you the best chill out spot you can imagine; in this backpacker hostel you can relax, smoke, hang out with friends, party or challenge your limits and abilities in various beach sports!</p>';
}
else
{
ob_start();
 
//Assign all Page Specific variables  
$content = ob_get_contents();  
ob_end_clean();  
//Apply the template  
include("template.php");
}
?>
 
Is this what you're after?
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Passing HTML into Template Page

Post by millsy007 »

Its is clever how that works, but what I need is a way that the formatting can be shown in dreamweaver without any code running (for editing the content.php page), but then when the content.php is run and is within the template.php it not displaying the extra formatting code/text.

Not sure if this is possible..?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Passing HTML into Template Page

Post by aceconcepts »

It sounds like you may need a development site/area. Its not a very good idea to run testing stuff with live data - for obvious reasons.
Post Reply