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

Best way to conditionally show large blocks of HTML

P: n/a
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

Jan 18 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"lister" <li************@hotmail.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.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
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Jan 18 '07 #2

P: n/a
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.html';
Regards,
Erwin Moller
Jan 18 '07 #3

P: n/a
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.

Jan 18 '07 #4

P: n/a
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

Jan 18 '07 #5

P: n/a
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.
Jan 18 '07 #6

P: n/a
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


Jan 18 '07 #7

P: n/a
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

Jan 19 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.