473,413 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Including html files

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
18 2556
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
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
"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

"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
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
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 nørder og andet godtfolk
Jul 17 '05 #7
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
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
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
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
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
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
*** 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
> 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
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: WindAndWaves | last post by:
Hi Gurus I hope I am going to make sense with this question: I have an html page that I have turned into a php page with a bit of php code above the html (connect to database, massage data a...
2
by: Sten Westerback | last post by:
Hi I'm considering using .asp to implement a selective inclusion of .htm files containing tables into a HTML code sent to browser from IIS. My question is if it is possible to make an .asp...
4
by: Ralf Koms | last post by:
Hi, I would like to reference some other HTML files within an "main" HTML file (within the "header"), Something like this: <link rel="part1" href="file1.htm"> <link rel="part2"...
2
by: Craig | last post by:
Is there a way to conditionally include a file in the HTML? <TD> if x= 1 then <!--#INCLUDE FILE="../File1"--> else <!--#INCLUDE FILE="../File2"--> end if
1
by: relisoft | last post by:
SEATTLE, Washington. - July 12, 2006: Reliable Software® announces the upcoming release of Code Co-op® version 5.0. Code Co-op is an affordable peer-to-peer version control system for distributed...
3
by: Mr. Roper | last post by:
I'm pretty weak when it comes to Java script, hopefully someone will take mercy on my sole and explain this to me. How come on the following HTML page, when I have my first script tag commented...
10
by: Ben Sehara | last post by:
Hi, I want to include three php files in index.php file like the code below. But it always shows up only two, any two of the three, any order. <tr> <td><?php include...
10
by: Robert Huff | last post by:
Can someone offer suggestions why, on the same server (Apache 2.2.8), this works <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html lang="en-US"> <head> <link rel=stylesheet...
1
by: vunet | last post by:
What's the possible disadvantage of including JavaScript files within a JavaScript file? Why I needed it? For the sake of easy management, perhaps? Any load speed disadvantage? Example below: ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.