473,372 Members | 1,076 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,372 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 13010
"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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.