By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

Including html files

P: n/a
I am looking for a way to combine the functionality of "include" and
"here documents" so that an included html file containing $variables
is expanded and handled (normally printed to stdout).

It would be something like:

print <<<HERE
include file.html;
HERE;

except that, of course, this won't work. The text "include
file.html;" will be printed and no file will be included. So it needs
to be some other syntax. Does php support this?
Jul 17 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
David Johnson wrote:
I am looking for a way to combine the functionality of "include" and
"here documents" so that an included html file containing $variables
is expanded and handled (normally printed to stdout).

It would be something like:

print <<<HERE
include file.html;
HERE;

except that, of course, this won't work. The text "include
file.html;" will be printed and no file will be included. So it needs
to be some other syntax. Does php support this?


Simply use include....

....
....
include('file.html');
....
....

The text of the file will be inserted at that position and parsed as
normal html/php.
Jul 17 '05 #2

P: n/a
I don't think so. Or, at least, it doesn't work for me. I have two files:

hello.php:
<?php
$foo = "Hello, there";
include('header.html');
include('hello.html');
include('footer.html');
?>
hello.html:
<h1>$foo</h1>

I want the value of $foo to be printed when hello.html is included.
Instead, only the variable "$foo" is printed.
Jul 17 '05 #3

P: n/a
"David Johnson" <da******@yahoo.com> wrote in message
news:b1**************************@posting.google.c om...
I don't think so. Or, at least, it doesn't work for me. I have two files:
hello.php:
<?php
$foo = "Hello, there";
include('header.html');
include('hello.html');
include('footer.html');
?>
hello.html:
<h1>$foo</h1>

I want the value of $foo to be printed when hello.html is included.
Instead, only the variable "$foo" is printed.


You have to change hello.html to either this:

<?php
print <<<BLOCK
<h1>$foo</h1>
BLOCK;
?>

or this:

<h1><?php print $foo; ?></h1>

Unless $foo is between <?php and ?> tags, it will be printed as normal HTML.

- JP
Jul 17 '05 #4

P: n/a

"David Johnson" <da******@yahoo.com> wrote in message
news:b1**************************@posting.google.c om...
I don't think so. Or, at least, it doesn't work for me. I have two files:
hello.php:
<?php
$foo = "Hello, there";
include('header.html');
include('hello.html');
include('footer.html');
?>
hello.html:
<h1>$foo</h1>

I want the value of $foo to be printed when hello.html is included.
Instead, only the variable "$foo" is printed.


That's because you don't have the power of Bobo the Clown. Observe Bob in
action:

<?

// PHP 4.3.0+ required

define('ECHO_HEREDOC_START', "<?\necho <<<__BOBO_THE_CLOWN__\n");
define('ECHO_HEREDOC_END', "\n__BOBO_THE_CLOWN__;\n?>");

class PrintDocHereStream {
var $position;
var $data;

function stream_open($path, $mode, $options, &$opened_path)
{
// 'heredoc://<path to file>'
$filepath = substr($path, 10);
$this->data = ECHO_HEREDOC_START
. file_get_contents($filepath)
. ECHO_HEREDOC_END;
$this->position = 0;
return true;
}

function stream_read($count)
{
$ret = substr($this->data, $this->position, $count);
$this->position += strlen($ret);
return $ret;
}

function stream_write($data)
{
$left = substr($this->data, 0, $this->position);
$right = substr($this->data, $this->position + strlen($data));
$this->data = $left . $data . $right;
$this->position += strlen($data);
return strlen($data);
}

function stream_tell()
{
return $this->position;
}

function stream_eof()
{
return $this->position >= strlen($this->data);
}

function stream_seek($offset, $whence)
{
switch($whence) {
case SEEK_SET:
if ($offset < strlen($this->data) && $offset >= 0) {
$this->position = $offset;
return true;
} else {
return false;
}
break;

case SEEK_CUR:
if ($offset >= 0) {
$this->position += $offset;
return true;
} else {
return false;
}
break;

case SEEK_END:
if (strlen($this->data) + $offset >= 0) {
$this->position = strlen($this->data) + $offset;
return true;
} else {
return false;
}
break;

default:
return false;
}
}

function stream_stat()
{
return array( 'size' => strlen($this->data) );
}
}

stream_register_wrapper("heredoc", "PrintDocHereStream")
or die("Failed to register PrintDocHereStream");

$foo = "Hello, there";
include('header.html');
include('heredoc://hello.html');
include('footer.html');

?>
Jul 17 '05 #5

P: n/a
Thank you for your solutions. I guess the answer is that there is
nothing native in php that supports such a "here-include." Pity,
because I think it would be quite useful.

Modifying the "hello.html" file to make it no longer straight html is
not an option since I want to be able to browse and edit it like an
html file.
Adding the snippet of php is the closest to what I had hoped for. It
would look a little strange in the html browser but not much.

The other solution, to me, is nothing short of amazing. If php can't
do it, then write your own. I question the interpretation time and
whether my ISP has the
latest version of php, but that may just be the way to go. Thanks.

Does anyone know if what I called "here-include" is in the php plan or
wish list?
And what it's really called?

Thanks.
Jul 17 '05 #6

P: n/a
David Johnson wrote:
Thank you for your solutions. I guess the answer is that there is
nothing native in php that supports such a "here-include." Pity,
because I think it would be quite useful.

Modifying the "hello.html" file to make it no longer straight html is
not an option since I want to be able to browse and edit it like an
html file.
Adding the snippet of php is the closest to what I had hoped for. It
would look a little strange in the html browser but not much.

The other solution, to me, is nothing short of amazing. If php can't
do it, then write your own. I question the interpretation time and
whether my ISP has the
latest version of php, but that may just be the way to go. Thanks.

Does anyone know if what I called "here-include" is in the php plan or
wish list?


How about this one?

http://dk2.php.net/manual/en/function.readfile.php

--
MVH Jeppe Uhd - NX http://nx.dk
Webhosting for nrder og andet godtfolk
Jul 17 '05 #7

P: n/a
Can I also execute php from within the quoted part of a form line?

hello.html:
....
<input type=text name=foo size=60 value="<?php print $foo; ?>">
....
Jul 17 '05 #8

P: n/a
Except it doesn't expand any variables in the file.
I want the file treated as if it were imbedded into
a here-doc so that all of the $foo variables in it are
replaced with their values.
Jul 17 '05 #9

P: n/a
On Tue, 22 Jun 2004 11:54:27 -0700, David Johnson wrote:
Except it doesn't expand any variables in the file. I want the file
treated as if it were imbedded into a here-doc so that all of the $foo
variables in it are replaced with their values.


Why do you want to use this approach? The "Here doc" approach. There are
already several available approaches to do what you probably want to do.
You seem to want to have your cake and eat it to. By this I mean in your
earlier post you said

"Modifying the "hello.html" file to make it no longer straight html is not
an option since I want to be able to browse and edit it like an html file.
Adding the snippet of php is the closest to what I had hoped for. It
would look a little strange in the html browser but not much."

But including PHP variables in the HTML will inherently make the page not
pure HTML!

So my suggestion is, USE TEMPLATES!

Try one of the following templating systems:

PHPFastTemplate - <http://www.thewebmasters.net/php/FastTemplate.phtml>

Smarty - <http://smarty.php.net/>

bTemplate - <http://www.massassi.com/bTemplate/>

(I personally prefer bTemplate -- easiest to set up, implement, and use.)

All three do basically the same thing: allow you to use pure HTML
templates that you can edit in, say, Dreamweaver or other WYSIWYG HTML
editor. And from my experience with bTemplate, I think it does something
very similar to what you are looking for as judging by your original post.

later...

--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Website | http://www.wse.jhu.edu/newtnotes/

Jul 17 '05 #10

P: n/a
what type of editor are you using where you can't
put <?=$stringvar?> in it
any <xxxx> is a tag and therefore "straight html"
putting "$vars" in your document isn't "straight html." It's html
with "$vars" in it. What if you have javascript in your html file?
or $5.25 How's it supposed to know what $xxxx to ignore?

What do you mean the other solution is amazing "if php can't do it,
then write your own." What you mean if php doesn't have a predefined
function then write your own IN PHP. PHP gives you the tools, not
the finished house.

da******@yahoo.com (David Johnson) wrote in message news:<b1**************************@posting.google. com>...
Thank you for your solutions. I guess the answer is that there is
nothing native in php that supports such a "here-include." Pity,
because I think it would be quite useful.

Modifying the "hello.html" file to make it no longer straight html is
not an option since I want to be able to browse and edit it like an
html file.
Adding the snippet of php is the closest to what I had hoped for. It
would look a little strange in the html browser but not much.

The other solution, to me, is nothing short of amazing. If php can't
do it, then write your own. I question the interpretation time and
whether my ISP has the
latest version of php, but that may just be the way to go. Thanks.

Does anyone know if what I called "here-include" is in the php plan or
wish list?
And what it's really called?

Thanks.

Jul 17 '05 #11

P: n/a
Uh.. No.
readfile doesn't parse the output at all..
it's a straight passthru to the output buffer.

"Jeppe Uhd" <ln*********@nx.dk> wrote in message news:<pp************@crm.nwg.dk>...
David Johnson wrote:
Thank you for your solutions. I guess the answer is that there is
nothing native in php that supports such a "here-include." Pity,
because I think it would be quite useful.

Modifying the "hello.html" file to make it no longer straight html is
not an option since I want to be able to browse and edit it like an
html file.
Adding the snippet of php is the closest to what I had hoped for. It
would look a little strange in the html browser but not much.

The other solution, to me, is nothing short of amazing. If php can't
do it, then write your own. I question the interpretation time and
whether my ISP has the
latest version of php, but that may just be the way to go. Thanks.

Does anyone know if what I called "here-include" is in the php plan or
wish list?


How about this one?

http://dk2.php.net/manual/en/function.readfile.php

Jul 17 '05 #12

P: n/a
da******@yahoo.com (David Johnson) wrote in message
news:<b1**************************@posting.google. com>...

I am looking for a way to combine the functionality of "include" and
"here documents" so that an included html file containing $variables
is expanded and handled (normally printed to stdout).

It would be something like:

print <<<HERE
include file.html;
HERE;


First of all, why would you ever want to include() an HTML file?
readfile() is faster...

Second, HTML files are not supposed to be "containing $variables";
only PHP files are.

Third, what you seem to want is probably achievable with eval(),
although I am still at a loss what it is you are trying to accomplish
in such a convoluted way...

Cheers,
NC
Jul 17 '05 #13

P: n/a
*** David Johnson wrote/escribi (21 Jun 2004 11:19:11 -0700):
I don't think so. Or, at least, it doesn't work for me. I have two files:
The 'include' approach should work fine. I've tested!
*** foo.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<?
$name='John Smith';
include 'foo.txt';
?>

</body>
</html>

*** foo.txt
My name is <?=$name?>
<h1>$foo</h1>


That's why <? ?> tags exist. Otherwise, how could you tell what's PHP code
and what isn't? ;-)

--
--
-- lvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #14

P: n/a
> First of all, why would you ever want to include() an HTML file?

Sounds like I wouldn't since it doesn't support what I want to do.
readfile() is faster... That's fine but it doesn't support $variables.

Second, HTML files are not supposed to be "containing $variables";
only PHP files are.
Not supposed to? There's nothing in the HTML spec that prohibits
text from containing dollar signs.

Third, what you seem to want is probably achievable with eval(),
although I am still at a loss what it is you are trying to accomplish
in such a convoluted way...
Only convoluted if php doesn't support it in a simple function call.
Which appears to be the case. Funny how it takes longer for people to say
"I don't know" than almost anything else.

For your information, I am trying to do bare-bones templates. I know
there are several fine template packages, but I don't want all the
function or overhead they provide. I want to put all of my html in
one file with variable names instead of text. The html would never be
displayed as such so no one but me would see the variable names.
But I want to use an html editor to edit these files and be able to
display them in a web browser. My php program would then "include" the
html file, substituting text for the variables.

php comes very close to supporting this. An included file can contain
php and a here-doc but that spoils it for display by a web browser.
If you could imbed an external text file while you're in a here-doc,
you could get what I'm looking for. But, of course, everything in a
here-doc is interpreted as straight text, not commands.

Maybe there is another way to do it, such as with an eval. But, as
you say, it tends to get convoluted.

Cheers,
NC


And to you. And thanks to everyone who offered suggestions.
Jul 17 '05 #15

P: n/a
On 23 Jun 2004 04:58:07 -0700, da******@yahoo.com (David Johnson)
wrote:
First of all, why would you ever want to include() an HTML file?
Sounds like I wouldn't since it doesn't support what I want to do.
readfile() is faster...

That's fine but it doesn't support $variables.


Yeah, but you can str_replace to your hearts content.


Second, HTML files are not supposed to be "containing $variables";
only PHP files are.
Not supposed to? There's nothing in the HTML spec that prohibits
text from containing dollar signs.


Maybe he should have said "Really shouldn't" ?


Third, what you seem to want is probably achievable with eval(),
although I am still at a loss what it is you are trying to accomplish
in such a convoluted way...
Only convoluted if php doesn't support it in a simple function call.
Which appears to be the case. Funny how it takes longer for people to say
"I don't know" than almost anything else.

For your information, I am trying to do bare-bones templates.


For his information? He was trying to be nice in suggesting you post
what you're trying to DO so people could help you.

I know
there are several fine template packages, but I don't want all the
function or overhead they provide. I want to put all of my html in
one file with variable names instead of text. The html would never be
displayed as such so no one but me would see the variable names.
But I want to use an html editor to edit these files and be able to
display them in a web browser. My php program would then "include" the
html file, substituting text for the variables.
use readinfile to store your file in $htmldoc;

$htmldoc = str_replace(Variable1, $value1, $htmldoc)
$htmldoc = str_replace(Variable2, $value2, $htmldoc)
$htmldoc = str_replace(Variable3, $value3, $htmldoc)
$htmldoc = str_replace(Variable4, $value4, $htmldoc)
echo $htmldoc;

How hard is that?


php comes very close to supporting this. An included file can contain
php and a here-doc but that spoils it for display by a web browser.
If you could imbed an external text file while you're in a here-doc,
you could get what I'm looking for. But, of course, everything in a
here-doc is interpreted as straight text, not commands.


Then maybe you should join the PHP team and WRITE it?
--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
DataBasix | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ۳
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://www.databasix.com
================================================== =========================
Jul 17 '05 #16

P: n/a
> First of all, why would you ever want to include() an HTML file?

Sounds like I wouldn't since it doesn't support what I want to do.
readfile() is faster... That's fine but it doesn't support $variables.

Second, HTML files are not supposed to be "containing $variables";
only PHP files are.
Not supposed to? There's nothing in the HTML spec that prohibits
text from containing dollar signs.

Third, what you seem to want is probably achievable with eval(),
although I am still at a loss what it is you are trying to accomplish
in such a convoluted way...
Only convoluted if php doesn't support it in a simple function call.
Which appears to be the case. Funny how it takes longer for people to say
"I don't know" than almost anything else.

For your information, I am trying to do bare-bones templates. I know
there are several fine template packages, but I don't want all the
function or overhead they provide. I want to put all of my html in
one file with variable names instead of text. The html would never be
displayed as such so no one but me would see the variable names.
But I want to use an html editor to edit these files and be able to
display them in a web browser. My php program would then "include" the
html file, substituting text for the variables.

php comes very close to supporting this. An included file can contain
php and a here-doc but that spoils it for display by a web browser.
If you could imbed an external text file while you're in a here-doc,
you could get what I'm looking for. But, of course, everything in a
here-doc is interpreted as straight text, not commands.

Maybe there is another way to do it, such as with an eval. But, as
you say, it tends to get convoluted.

Cheers,
NC


And to you. And thanks to everyone who offered suggestions.
Jul 17 '05 #17

P: n/a
da******@yahoo.com (David Johnson) wrote in message
news:<b1**************************@posting.google. com>...
Second, HTML files are not supposed to be "containing $variables";
only PHP files are.
Not supposed to? There's nothing in the HTML spec that prohibits
text from containing dollar signs.


That's right, but in PHP's default setup *.htm[l] files are not
parsed by the PHP interpreter.
For your information, I am trying to do bare-bones templates. I know
there are several fine template packages, but I don't want all the
function or overhead they provide. I want to put all of my html in
one file with variable names instead of text. The html would never be
displayed as such so no one but me would see the variable names.
But I want to use an html editor to edit these files and be able to
display them in a web browser. My php program would then "include" the
html file, substituting text for the variables.


OK, now we're talking. Here's how you can do this.

Let's say you have a file 'intro.htm':

<html>
<h1>Hello, my name is $name</h1>
</html>

Then, you have this script:

$name = 'David Johnson';
$content = '$newcontent = "' .
str_replace('"', '&quot;', file_get_contents('intro.htm')) .
'";';
eval($content);
echo $newcontent;

Voila, your HTML output reads:

<html>
<h1>Hello, my name is David Johnson</h1>
</html>

Cheers,
NC
Jul 17 '05 #18

P: n/a
nc@iname.com (Nikolai Chuvakhin) wrote in message
news:<32**************************@posting.google. com>...

Let's say you have a file 'intro.htm':

<html>
<h1>Hello, my name is $name</h1>
</html>

Then, you have this script:

$name = 'David Johnson';
$content = '$newcontent = "' .
str_replace('"', '&quot;', file_get_contents('intro.htm')) .
'";';
eval($content);
echo $newcontent;

Voila, your HTML output reads:

<html>
<h1>Hello, my name is David Johnson</h1>
</html>


On a second thought, there's a more elegant (yet still conceptually
convoluted) solution:

$name = 'David Johnson';
eval('echo "' .
str_replace('"', '&quot;', file_get_contents('intro.htm')) .
'";');

Cheers,
NC
Jul 17 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.