Including html files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Johnson

    #1

    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?
  • Kevin Thorpe

    #2
    Re: Including html files

    David Johnson wrote:
    [color=blue]
    > 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?[/color]

    Simply use include....

    ....
    ....
    include('file.h tml');
    ....
    ....

    The text of the file will be inserted at that position and parsed as
    normal html/php.

    Comment

    • David Johnson

      #3
      Re: Including html files

      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.

      Comment

      • kingofkolt

        #4
        Re: Including html files

        "David Johnson" <davidaak@yahoo .com> wrote in message
        news:b1ee3f2a.0 406211019.6ef44 696@posting.goo gle.com...[color=blue]
        > I don't think so. Or, at least, it doesn't work for me. I have two[/color]
        files:[color=blue]
        >
        > 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.[/color]

        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


        Comment

        • Chung Leong

          #5
          Re: Including html files


          "David Johnson" <davidaak@yahoo .com> wrote in message
          news:b1ee3f2a.0 406211019.6ef44 696@posting.goo gle.com...[color=blue]
          > I don't think so. Or, at least, it doesn't work for me. I have two[/color]
          files:[color=blue]
          >
          > 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.[/color]

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

          <?

          // PHP 4.3.0+ required

          define('ECHO_HE REDOC_START', "<?\necho <<<__BOBO_THE_C LOWN__\n");
          define('ECHO_HE REDOC_END', "\n__BOBO_THE_C LOWN__;\n?>");

          class PrintDocHereStr eam {
          var $position;
          var $data;

          function stream_open($pa th, $mode, $options, &$opened_pat h)
          {
          // 'heredoc://<path to file>'
          $filepath = substr($path, 10);
          $this->data = ECHO_HEREDOC_ST ART
          . file_get_conten ts($filepath)
          . ECHO_HEREDOC_EN D;
          $this->position = 0;
          return true;
          }

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

          function stream_write($d ata)
          {
          $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($of fset, $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("hered oc", "PrintDocHereSt ream")
          or die("Failed to register PrintDocHereStr eam");

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

          ?>


          Comment

          • David Johnson

            #6
            Re: Including html files

            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.

            Comment

            • Jeppe Uhd

              #7
              Re: Including html files

              David Johnson wrote:[color=blue]
              > 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?[/color]

              How about this one?



              --
              MVH Jeppe Uhd - NX http://nx.dk
              Webhosting for nørder og andet godtfolk


              Comment

              • David Johnson

                #8
                Re: Including html files

                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; ?>">
                ....

                Comment

                • David Johnson

                  #9
                  Re: Including html files

                  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.

                  Comment

                  • Jeffrey Silverman

                    #10
                    Re: Including html files

                    On Tue, 22 Jun 2004 11:54:27 -0700, David Johnson wrote:
                    [color=blue]
                    > 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.[/color]

                    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.thewebmaste rs.net/php/FastTemplate.ph tml>

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

                    bTemplate - <http://www.massassi.co m/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/

                    Comment

                    • Brad Kent

                      #11
                      Re: Including html files

                      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.

                      davidaak@yahoo. com (David Johnson) wrote in message news:<b1ee3f2a. 0406220430.7f93 a43a@posting.go ogle.com>...[color=blue]
                      > 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.[/color]

                      Comment

                      • Brad Kent

                        #12
                        Re: Including html files

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

                        "Jeppe Uhd" <lnewsnospam@nx .dk> wrote in message news:<ppnlq1-dai.ln1@crm.nwg .dk>...[color=blue]
                        > David Johnson wrote:[color=green]
                        > > 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?[/color]
                        >
                        > How about this one?
                        >
                        > http://dk2.php.net/manual/en/function.readfile.php[/color]

                        Comment

                        • Nikolai Chuvakhin

                          #13
                          Re: Including html files

                          davidaak@yahoo. com (David Johnson) wrote in message
                          news:<b1ee3f2a. 0406210626.12d1 4dd3@posting.go ogle.com>...[color=blue]
                          >
                          > 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;[/color]

                          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

                          Comment

                          • Alvaro G Vicario

                            #14
                            Re: Including html files

                            *** David Johnson wrote/escribió (21 Jun 2004 11:19:11 -0700):[color=blue]
                            > I don't think so. Or, at least, it doesn't work for me. I have two files:[/color]

                            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?>
                            [color=blue]
                            > <h1>$foo</h1>[/color]

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

                            --
                            --
                            -- Álvaro G. Vicario - Burgos, Spain
                            --

                            Comment

                            • David Johnson

                              #15
                              Re: Including html files

                              > 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.
                              [color=blue]
                              > readfile() is faster...[/color]
                              That's fine but it doesn't support $variables.
                              [color=blue]
                              >
                              > Second, HTML files are not supposed to be "containing $variables";
                              > only PHP files are.[/color]

                              Not supposed to? There's nothing in the HTML spec that prohibits
                              text from containing dollar signs.
                              [color=blue]
                              >
                              > 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...[/color]

                              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.
                              [color=blue]
                              >
                              > Cheers,
                              > NC[/color]

                              And to you. And thanks to everyone who offered suggestions.

                              Comment

                              Working...