473,385 Members | 1,356 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,385 software developers and data experts.

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

Jan 18 '07 #1
7 13036
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: James | last post by:
What is the best method for creating a Web Page that uses both PHP and HTML ? <HTML> BLA BLA BLA BLA BLA
10
by: Andrew | last post by:
I am making a web site and I am using ASP because of its templating capabilities. My footer is an include file, for obvious reasons. I can't use an include file for the header, because, while the...
2
by: steven | last post by:
Hi, sorry for the crosspost, but I'm not sure if my problem is with the HTML or the CSS :-( URL: http://www.nenya.be/temp/list.htm I have a list with in each list item a text + link which are...
3
by: Bob Maggio | last post by:
I have created a function that returns a string containing raw HTML to be used on a web form (see below): private string GetReportHTML() { string strHTML = ""; // I have some code here that...
14
by: J.S. | last post by:
In a Windows Form application, which is the better method to concatenate large blocks of code? 1. Reading the text from text files. 2. Adding the text to the VB file itself? Thanks! J.S. ...
8
by: Francine.Neary | last post by:
I'm experimenting with a program that manipulates a large matrix, i.e. a 2-deep array. I started by just allocating this in one go, but later it occurred to me that, as there are still some very...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.