Forum Moderators: coopster

Message Too Old, No Replies

echo-ing a template

question about a previous post

         

irmdogg

8:45 pm on Sep 28, 2003 (gmt 0)

10+ Year Member



Original post here: [webmasterworld.com...]

New to php, I found that jatar_k's post was very clever about echo-ing the template:

by the same token you can also put the header and footer code into a single page called template.php with
echo $content;

in the middle. Then you just do this on every page.

$content = "pathtocontentpage.html";
include "template.php";

and voila, the template echo the page set to content.

However, upon attempting this, I find that all I can do is echo the literal string that I put into $content. For instance, if I said

$content = "pathtocontent.html"

what would appear in my pages is

pathtocontent.html

Any help on this? Apologies for possibly posting in the wrong place. Futher apologies for being such a newbie.

daisho

12:56 am on Sep 29, 2003 (gmt 0)

10+ Year Member



If the echo example you have to set content to the literal content. ie:

$content='This is the content of the page';

To get what was descripted simply change:

echo $content;

to

@include $content;

ps. the "@" simply surpresses errors if $content file is not found. I'd suggest something a little smarter like:

if(!@include($content) ) {
echo "I'm sorry this was not found";
}

or something like that.

daisho.

jatar_k

1:36 am on Sep 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld irmdogg,

a very good catch, I edited my previous post accordingly. ;)

though it is good to mention that there would be 2 methodologies here

either build your content and do the echo $content or pass a page name and do include $content.

daisho

1:47 am on Sep 29, 2003 (gmt 0)

10+ Year Member



Yup. Depending on the situation I've used both.

irmdogg

3:31 am on Sep 30, 2003 (gmt 0)

10+ Year Member



Thanks for your help, but I'm unclear about the implementation of the include method that was described.

In the so called template(.php), if I were to say

@include $content;

and then in the page I wish to place content I said

$content="thispage.php";
include "template.php";

Would this not lead to an endless cycle? As in, you go to thispage.php, which includes template.php, but template.php again includes thispage.php which includes.... do you see my point? Could you possibly elaborate?

I am amazed at your fast responses. Thanks for explaining!

daisho

4:24 am on Sep 30, 2003 (gmt 0)

10+ Year Member



Your right that would be a recursive loop.

You have template.php that includes $content

You then have $default.php that does:

$content='thefile.php';
include $template.php;

If this is the way you are going to do things then I'd recommend splitting template into a header.php and footer.php (everything before and then everything after the content.)

Then in default.php do:

<?include 'header.php';?>

This is my content...

<?include 'footer.php';?>

The real advantage to the first template design described is if you pass the content file as a parameter ie:

[example.com...]

Then template.php will include the file pointed to by the content variable at the appropriate time.

Sky is the limit. All depends on what you like best for what you are doing.

Daisho.

irmdogg

3:14 am on Oct 1, 2003 (gmt 0)

10+ Year Member



Thank you for your response. Everything is now crystal clear.