Embedding php in html code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken Ward

    Embedding php in html code?

    Hi All,

    I'm a new boy to php and working on a web site with long sections of
    code that could be done as includes apart from one or two things
    changing.
    I've tested the following and it works, but what I really want to know
    is am I OK placing php code inside a html line like this, or could there
    be problems?


    <!-- html code for test.php -->
    <!-- lot of code cut -->
    <a href="<?php print $filename; ?>"><?php print $title; ?></a>
    <!-- lot of code cut -->
    <!-- end of test.php -->

    <!-- which is called in the main file like this -->
    <?php
    $filename="inde x.php";
    $title="Index Page";
    include ("test.php") ;
    ?>

    --
    Ken Ward in Norwich ken@oldcity.f2s .com
    For Norwich and East Anglia visit www.oldcity.org.uk
  • .:Ninja

    #2
    Re: Embedding php in html code?

    Ken Ward wrote:
    [color=blue]
    > Hi All,
    >
    > I'm a new boy to php and working on a web site with long sections of
    > code that could be done as includes apart from one or two things
    > changing.
    > I've tested the following and it works, but what I really want to know
    > is am I OK placing php code inside a html line like this, or could there
    > be problems?
    >
    >
    > <!-- html code for test.php -->
    > <!-- lot of code cut -->
    > <a href="<?php print $filename; ?>"><?php print $title; ?></a>
    > <!-- lot of code cut -->
    > <!-- end of test.php -->
    >
    > <!-- which is called in the main file like this -->
    > <?php
    > $filename="inde x.php";
    > $title="Index Page";
    > include ("test.php") ;
    > ?>[/color]

    Yeah I guess you're ok. You can actually use a shorter syntax to just print
    a variable: <?= $varname ?>

    I suspect someone will suggest you use some kind of template system that
    separates your PHP from your HTML. Well, they have a lot of time on their
    hands :)

    ..:A

    --

    Comment

    • Juha Suni

      #3
      Re: Embedding php in html code?

      ..:Ninja wrote:[color=blue]
      > Yeah I guess you're ok. You can actually use a shorter syntax to just
      > print a variable: <?= $varname ?>[/color]

      This, however, will only work if the PHP directive "short_open_tag " is
      se to On, and by default many hosts have it off.

      --
      Suni

      Comment

      • Five Cats

        #4
        Re: Embedding php in html code?

        In message <1084183396.817 537@inet2.up.ac .za>, ".:Ninja"
        <albe@it.up.ac. za> writes[color=blue]
        >Ken Ward wrote:
        >[color=green]
        >> Hi All,
        >>
        >> I'm a new boy to php and working on a web site with long sections of
        >> code that could be done as includes apart from one or two things
        >> changing.
        >> I've tested the following and it works, but what I really want to know
        >> is am I OK placing php code inside a html line like this, or could there
        >> be problems?
        >>
        >>
        >> <!-- html code for test.php -->
        >> <!-- lot of code cut -->
        >> <a href="<?php print $filename; ?>"><?php print $title; ?></a>
        >> <!-- lot of code cut -->
        >> <!-- end of test.php -->
        >>
        >> <!-- which is called in the main file like this -->
        >> <?php
        >> $filename="inde x.php";
        >> $title="Index Page";
        >> include ("test.php") ;
        >> ?>[/color]
        >
        >Yeah I guess you're ok. You can actually use a shorter syntax to just print
        >a variable: <?= $varname ?>
        >
        >I suspect someone will suggest you use some kind of template system that
        >separates your PHP from your HTML. Well, they have a lot of time on their
        >hands :)[/color]

        Takes more time initially but probably saves it in the long term.[color=blue]
        >
        >.:A
        >[/color]

        --
        Five Cats
        Email to: cats_spam at uk2 dot net

        Comment

        • Garp

          #5
          Re: Embedding php in html code?


          ".:Ninja" <albe@it.up.ac. za> wrote in message
          news:1084183396 .817537@inet2.u p.ac.za...[color=blue]
          > I suspect someone will suggest you use some kind of template system that
          > separates your PHP from your HTML. Well, they have a lot of time on their
          > hands :)[/color]

          Oi! ;-)

          Garp


          Comment

          • Chung Leong

            #6
            Re: Embedding php in html code?

            "Five Cats" <cats_spam@[127.0.0.1]> wrote in message
            news:Ev6UBjBN95 nAFwxf@[127.0.0.1]...[color=blue][color=green]
            > >I suspect someone will suggest you use some kind of template system that
            > >separates your PHP from your HTML. Well, they have a lot of time on their
            > >hands :)[/color]
            >
            > Takes more time initially but probably saves it in the long term.[/color]

            I doubt it. Using a template system is a little like going back to using PHP
            1--you get a far less powerful, less flexible system to work with.

            I'm not saying that template systems are bad. People just need to remember
            that their raison d'etre is to limit what can be done within a page. If that
            is your requirment then it makes sense. Otherwise it can hardly be
            advisable. The benefits of "separating presentation from logic" is abstract
            and often times cosmetic, whereas the impact of bumping into a template
            system limitation is very real.


            Comment

            • Five Cats

              #7
              Re: Embedding php in html code?

              In message <qoGdnfJl2eLAgT 3dRVn-hQ@comcast.com> , Chung Leong
              <chernyshevsky@ hotmail.com> writes[color=blue]
              >"Five Cats" <cats_spam@[127.0.0.1]> wrote in message
              >news:Ev6UBjBN9 5nAFwxf@[127.0.0.1]...[color=green][color=darkred]
              >> >I suspect someone will suggest you use some kind of template system that
              >> >separates your PHP from your HTML. Well, they have a lot of time on their
              >> >hands :)[/color]
              >>
              >> Takes more time initially but probably saves it in the long term.[/color]
              >
              >I doubt it. Using a template system is a little like going back to using PHP
              >1--you get a far less powerful, less flexible system to work with.
              >
              >I'm not saying that template systems are bad. People just need to remember
              >that their raison d'etre is to limit what can be done within a page. If that
              >is your requirment then it makes sense. Otherwise it can hardly be
              >advisable. The benefits of "separating presentation from logic" is abstract
              >and often times cosmetic, whereas the impact of bumping into a template
              >system limitation is very real.[/color]

              I would have said the reason for using a template (in the way I mean it)
              is to make it easy to get a uniform page layout across a site.[color=blue]
              >
              >[/color]

              --
              Five Cats
              Email to: cats_spam at uk2 dot net

              Comment

              • Tony Marston

                #8
                Re: Embedding php in html code?


                "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
                news:qoGdnfJl2e LAgT3dRVn-hQ@comcast.com. ..[color=blue]
                > "Five Cats" <cats_spam@[127.0.0.1]> wrote in message
                > news:Ev6UBjBN95 nAFwxf@[127.0.0.1]...[color=green][color=darkred]
                > > >I suspect someone will suggest you use some kind of template system[/color][/color][/color]
                that[color=blue][color=green][color=darkred]
                > > >separates your PHP from your HTML. Well, they have a lot of time on[/color][/color][/color]
                their[color=blue][color=green][color=darkred]
                > > >hands :)[/color]
                > >
                > > Takes more time initially but probably saves it in the long term.[/color]
                >
                > I doubt it. Using a template system is a little like going back to using[/color]
                PHP[color=blue]
                > 1--you get a far less powerful, less flexible system to work with.[/color]

                I totally disagree. My system is based around an amalgamation of the 3 tier
                architecture (refer to
                http://www.tonymarston.co.uk/php-mys...structure.html) and the
                Model-View-Controller design pattern (refer to
                http://www.tonymarston.co.uk/php-mys...ontroller.html) and it
                has proved to be very powerful and very flexible.
                [color=blue]
                > I'm not saying that template systems are bad. People just need to remember
                > that their raison d'etre is to limit what can be done within a page. If[/color]
                that[color=blue]
                > is your requirment then it makes sense. Otherwise it can hardly be
                > advisable. The benefits of "separating presentation from logic" is[/color]
                abstract[color=blue]
                > and often times cosmetic, whereas the impact of bumping into a template
                > system limitation is very real.[/color]

                I use XSL stylesheets to generate all my HTML and I have never found any
                limitations. In fact my level of reusable code has gone up considerably
                since using XSL. Take a look at
                http://www.tonymarston.co.uk/xml-xsl/reusable-xsl.html for details.

                --
                Tony Marston

                This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




                Comment

                • Berislav Lopac

                  #9
                  Re: Embedding php in html code?

                  Chung Leong wrote:[color=blue]
                  > I'm not saying that template systems are bad. People just need to
                  > remember that their raison d'etre is to limit what can be done within
                  > a page. If that is your requirment then it makes sense.[/color]

                  Than it always makes sense. You see, you *must* limit what happens in a
                  page; specifically, the only thing that happens in a page is what is needed
                  to display it.

                  Everything else: database queries, calculations, text translations etc
                  should be done separately. When it comes to HTML, all data should already be
                  prepared for display. This is why templates are a good tool, despite their
                  performance penalty.

                  Berislav

                  --
                  If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
                  Groucho, Chico, and Harpo, then Usenet is Zeppo.


                  Comment

                  • Tony Marston

                    #10
                    Re: Embedding php in html code?


                    "Berislav Lopac" <berislav.lopac @dimedia.hr> wrote in message
                    news:c7q7bo$9pt $1@ls219.htnet. hr...[color=blue]
                    > Chung Leong wrote:[color=green]
                    > > I'm not saying that template systems are bad. People just need to
                    > > remember that their raison d'etre is to limit what can be done within
                    > > a page. If that is your requirment then it makes sense.[/color]
                    >
                    > Than it always makes sense. You see, you *must* limit what happens in a
                    > page; specifically, the only thing that happens in a page is what is[/color]
                    needed[color=blue]
                    > to display it.[/color]

                    I disagree with your emphasis on *must*. It is possible to write a component
                    which contains presentation, business and data access logic, but this does
                    not offer much opportunity for reusability. Personally I use a combination
                    of the 3 tier architecture and the Model-View-Controller design model to
                    produce a great deal of reusable code. However, the design and
                    implementation of such an infrastructure is beyond the capabilities of most
                    developers.
                    [color=blue]
                    > Everything else: database queries, calculations, text translations etc
                    > should be done separately. When it comes to HTML, all data should already[/color]
                    be[color=blue]
                    > prepared for display. This is why templates are a good tool, despite their
                    > performance penalty.[/color]

                    All my HTML is produced from XML data using XSL stylesheets. There may be a
                    performance overhead but the savings are made in short development times and
                    large amounts of reusable code.

                    --
                    Tony Marston

                    This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL



                    [color=blue]
                    > Berislav
                    >
                    > --
                    > If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
                    > Groucho, Chico, and Harpo, then Usenet is Zeppo.
                    >
                    >[/color]


                    Comment

                    • Chung Leong

                      #11
                      Re: Embedding php in html code?

                      "Berislav Lopac" <berislav.lopac @dimedia.hr> wrote in message
                      news:c7q7bo$9pt $1@ls219.htnet. hr...[color=blue]
                      > Than it always makes sense. You see, you *must* limit what happens in a
                      > page; specifically, the only thing that happens in a page is what is[/color]
                      needed[color=blue]
                      > to display it.[/color]

                      Like handcuffs, the purpose of a template system is to limit what others can
                      do (page designers, end users, etc). You will never convince me that it's a
                      good idea to handcuff myself.
                      [color=blue]
                      > Everything else: database queries, calculations, text translations etc
                      > should be done separately. When it comes to HTML, all data should already[/color]
                      be[color=blue]
                      > prepared for display. This is why templates are a good tool, despite their
                      > performance penalty.[/color]

                      But you can easily keep these separate without resorting to a template
                      system--just don't mix them. I mean unless you are so undisciplined that you
                      need something to save yourself from yourself...


                      Comment

                      Working...