473,769 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13185
"lister" <li************ @hotmail.comwro te in message
news:11******** **************@ 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
sp**@outolempi. net | rot13(xv***@bhg byrzcv.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.h tml';
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
4224
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
1787
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 top of the page will be 90% similar, they will have different titles, page titles, and left nav menus. I could write a function so that these vars can be interpolated, except I don't want 70 lines of Response.Write("foo") (there is a moderate...
2
8749
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 always visible, and a blockquote which can be shown or hidden by clicking the link. Showing/hiding works, but the blockquote's margins behave a bit strange. I'll try to explain, though the best way to understand what happens is to go
3
1748
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 queries a database and // formats the return DataSet as HTML and inserts // this HTML into strHTML for return to the calling
14
2371
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
1932
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 old systems around where such a large allocation might fail, perhaps it would be better to split the allocation into many allocations, one for each row, to increase portability to legacy systems. The advantage I see is that the memory allocator...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.