Best way to conditionally show large blocks of HTML

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

    Best way to conditionally show large blocks of HTML

    Hi,

    I have a situation where I have a large block of HTML code that is
    conditionally displayed. In effect I have:

    html page
    some html

    <?php
    if (bShowBlock)
    {
    //LARGE BLOCK OF HTML
    }
    else
    {
    //SOME OTHER LARGE BLOCK OF HTML
    }

    some more html

    What is the quickest and easiest way to do this? Echoing is not an
    option as I would have to escape eveything in the HTML blocks.

    Thanks,
    Lister

  • Kimmo Laine

    #2
    Re: Best way to conditionally show large blocks of HTML

    "lister" <listerofsmeg01 @hotmail.comwro te in message
    news:1169116479 .055045.279380@ v45g2000cwv.goo glegroups.com.. .
    Hi,
    >
    I have a situation where I have a large block of HTML code that is
    conditionally displayed. In effect I have:
    >
    html page
    some html
    >
    I usually do it like so:

    <?php if (bShowBlock){ ?>

    LARGE BLOCK OF HTML

    <?php } else { ?>

    SOME OTHER LARGE BLOCK OF HTML

    <? } ?>

    --
    "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
    http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
    spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


    Comment

    • Erwin Moller

      #3
      Re: Best way to conditionally show large blocks of HTML

      lister wrote:
      Hi,
      >
      I have a situation where I have a large block of HTML code that is
      conditionally displayed. In effect I have:
      >
      html page
      some html
      >
      <?php
      if (bShowBlock)
      {
      //LARGE BLOCK OF HTML
      }
      else
      {
      //SOME OTHER LARGE BLOCK OF HTML
      }
      >
      some more html
      >
      What is the quickest and easiest way to do this? Echoing is not an
      option as I would have to escape eveything in the HTML blocks.
      >
      Thanks,
      Lister
      Hi Lister,

      Just jump out of PHP, like this:

      <?php
      if (bShowBlock) {
      ?>
      Use plain html here
      <?php
      } else {
      ?>
      Use plain html here
      <?php
      }
      ?>

      Alternatively you can include a file that contains the large piece of html,
      in which case you just use something like :
      require 'myLargeHTML1.h tml';


      Regards,
      Erwin Moller

      Comment

      • lister

        #4
        Re: Best way to conditionally show large blocks of HTML

        D'oh!

        Thanks guys.

        For some reason I thought I wouldn't be able to drop out of PHP half
        way through a conditional.
        Many thanks.

        Comment

        • Toby Inkster

          #5
          Re: Best way to conditionally show large blocks of HTML

          Kimmo Laine wrote:
          <?php if (bShowBlock){ ?>
          >
          LARGE BLOCK OF HTML
          >
          <?php } else { ?>
          >
          SOME OTHER LARGE BLOCK OF HTML
          >
          <? } ?>
          Personally, I've never found that very aesthetically pleasing. I prefer
          things between '<?php' and '?>' to seem to make sense on their own (even
          if they do, say, access functions or variables defined elsewhere).

          Slightly nicer looking are:

          <?php
          include (bShowBlock ? 'foo.html' : 'bar.html');
          ?>

          or, if you don't want to have the blocks of HTML stored in external files,
          you could use output buffering, like so:

          <?php
          ob_start();
          ?>
          LARGE BLOCK OF HTML
          <?php
          $foo = ob_get_clean();
          ob_start();
          ?>
          SOME OTHER LARGE BLOCK OF HTML
          <?php
          $bar = ob_get_clean();

          print (bShowBlock ? $foo : $bar);
          ?>

          However, if the "large blocks of HTML" actually contain some PHP code, be
          aware that the latter of these two will execute both sets of code fully.

          --
          Toby A Inkster BSc (Hons) ARCS
          Contact Me ~ http://tobyinkster.co.uk/contact

          Comment

          • mark Bannister

            #6
            Re: Best way to conditionally show large blocks of HTML

            lister wrote:
            Hi,
            >
            I have a situation where I have a large block of HTML code that is
            conditionally displayed. In effect I have:
            >
            html page
            some html
            >
            <?php
            if (bShowBlock)
            {
            //LARGE BLOCK OF HTML
            }
            else
            {
            //SOME OTHER LARGE BLOCK OF HTML
            }
            >
            some more html
            >
            What is the quickest and easiest way to do this? Echoing is not an
            option as I would have to escape eveything in the HTML blocks.
            >
            Thanks,
            Lister
            >

            If you wish to stay in PHP:

            echo <<<endofecho
            //LARGE BLOCK OF HTML
            //does not have to be escaped
            // and php vars work
            endofecho;

            The endofecho must appear on a line by itself without any whitespace and
            is case sensitve. It can be hard to debug an error if you have a typo
            in the end marker.
            Mark B.

            Comment

            • boclair

              #7
              Re: Best way to conditionally show large blocks of HTML

              lister wrote:
              Hi,
              >
              I have a situation where I have a large block of HTML code that is
              conditionally displayed. In effect I have:
              >
              html page
              some html
              >
              <?php
              if (bShowBlock)
              {
              //LARGE BLOCK OF HTML
              }
              else
              {
              //SOME OTHER LARGE BLOCK OF HTML
              }
              >
              some more html
              >
              What is the quickest and easiest way to do this? Echoing is not an
              option as I would have to escape eveything in the HTML blocks.
              >

              A further method that can be useful is to use conditionals on the css
              display property. e.g

              <?php if(conditionA){ $display="block ;"}
              elseif (conditionB) {$display="none ;"}
              ?>
              <div style="<php echo $display;?>">
              BLOCK OF MARKUP
              </div>

              Louise




              Comment

              • Toby Inkster

                #8
                Re: Best way to conditionally show large blocks of HTML

                boclair wrote:
                A further method that can be useful is to use conditionals on the css
                display property. e.g
                >
                <?php if(conditionA){ $display="block ;"}
                elseif (conditionB) {$display="none ;"}
                ?>
                <div style="<php echo $display;?>">
                BLOCK OF MARKUP
                </div>
                Doesn't work in non-CSS aware browsers. e.g. older browsers, text
                browsers, search engine spiders, many mobile phones. And you waste
                bandwidth.

                --
                Toby A Inkster BSc (Hons) ARCS
                Contact Me ~ http://tobyinkster.co.uk/contact

                Comment

                Working...