Free Web Hosting Forum
Go Back   Free Web Hosting Forum > Website Building > Web Programming
Reload this Page cleanest way to seperate HTML code in PHP
Reply
 
Thread Tools Display Modes
(#1)
Old
tester0027's Avatar
tester0027 tester0027 is offline
Junior Member
tester0027 is on a distinguished road
 
Posts: 12
Join Date: Jan 2009
Post cleanest way to seperate HTML code in PHP - 02-09-2009, 06:58 AM

I am just wondering what is peoples opinions regarding the cleanest and most efficient way of storing chunks of HTML for output

like for example i presently do it like this

function table(){
echo <<< HTML
<table>
<tr>
<td>Stuff here</td>
</tr>
</table>
HTML;
}


I am just interested to hear how everyone stores their HTML for output in their scripts. I want to maintain the HTML's tabbing but that looks like an impossibility, with each function each table tabbing code out further...
Reply With Quote
(#2)
Old
Megalith Megalith is offline
Junior Member
Megalith is on a distinguished road
 
Posts: 14
Join Date: Feb 2009
Default 02-15-2009, 10:48 PM

i use
?>
then the html followed by a
<?

although sometimes i use echo depends on context
Reply With Quote
(#3)
Old
Wiseguy84 Wiseguy84 is offline
Junior Member
Wiseguy84 is on a distinguished road
 
Posts: 1
Join Date: Feb 2009
Default 02-27-2009, 10:58 PM

You're on the right track with the heredoc format as it does make it somewhat easier to read, however if you have a large amount of html I would suggest that you look at a template engine.

Template engines separate the code from the html very nicely with many advantages (like the ability to use WYSIWYG editors). I myself use smarty ( www.smarty.net ) but there are many others out there.
Reply With Quote
(#4)
Old
weishun weishun is offline
Senior Member
weishun is on a distinguished road
 
Posts: 223
Join Date: Feb 2009
Default 03-03-2009, 06:35 PM

The cleanest and most efficient way of storing chunks of HTML for output is to save the "chunks of HTML" and use:
Code:
<?php
include();
?>
I'm not sure what you mean by "HTML's tabbing". I was gonna give you the URL of a site I'm working on, that I thought might be what you're looking for, but the server's down. Can you post the URL of a site that's an example of what you're talking about? I like doing things that look like an impossibility.
Reply With Quote
(#5)
Old
aPhpHelper's Avatar
aPhpHelper aPhpHelper is offline
Senior Member
aPhpHelper is on a distinguished road
 
Posts: 206
Join Date: Nov 2008
Location: United Kingdom
Send a message via ICQ to aPhpHelper Send a message via AIM to aPhpHelper Send a message via MSN to aPhpHelper Send a message via Skype™ to aPhpHelper
Default 03-03-2009, 06:39 PM

Zend says that the following is fastest:
PHP Code:
<?php
function doOutput() {
?>
HTML goes here...
<?php
}
?>
Rather than...

PHP Code:
<?php
echo <<<EOD
HTML goes here
EOD;
?>
or...

PHP Code:
include("htmlTpl.tpl"); 
Reply With Quote
(#6)
Old
weishun weishun is offline
Senior Member
weishun is on a distinguished road
 
Posts: 223
Join Date: Feb 2009
Default 03-03-2009, 07:55 PM

I don't know if it's a language or comprehension problem but the objective here is: "the cleanest and most efficient way of storing chunks of HTML for output". Entering the HTML, manually, is neither.

Code:
include("htmlTpl.tpl");
is, pretty much, what I suggested. I wasn't aware you could store "chunks", plural, of HTML in a single 'tpl' file. How does the 'tpl' file know which chunk to output, for a particular page?
Reply With Quote
(#7)
Old
Megalith Megalith is offline
Junior Member
Megalith is on a distinguished road
 
Posts: 14
Join Date: Feb 2009
Default 03-05-2009, 12:41 PM

Quote:
Originally Posted by weishun View Post
I don't know if it's a language or comprehension problem but the objective here is: "the cleanest and most efficient way of storing chunks of HTML for output". Entering the HTML, manually, is neither.

Code:
include("htmlTpl.tpl");
is, pretty much, what I suggested. I wasn't aware you could store "chunks", plural, of HTML in a single 'tpl' file. How does the 'tpl' file know which chunk to output, for a particular page?
as aPhpHelper says the use of includes is not the most efficient way of handling simple html code. the fastest way is to not use the php interpreter at all unless you need it, hence my suggestion above which exits the php and sending the html direct to the browser rather than parsing it through a template or having function calls. in that sense it is the most efficient way of outputting html
Reply With Quote
(#8)
Old
weishun weishun is offline
Senior Member
weishun is on a distinguished road
 
Posts: 223
Join Date: Feb 2009
Default 03-05-2009, 03:02 PM

Quote:
Originally Posted by Megalith View Post
as aPhpHelper says the use of includes is not the most efficient way of handling simple html code. the fastest way is to not use the php interpreter at all unless you need it, hence my suggestion above which exits the php and sending the html direct to the browser rather than parsing it through a template or having function calls. in that sense it is the most efficient way of outputting html
The original post reads, in part:
Quote:
the cleanest and most efficient way of storing chunks of HTML for output
So, we're storing chunks of HTML and they're for output. That being the case, it's likely the "chunks" will be "output" more than once. If so, entering the HTML manually is neither "clean" nor "efficient".

The post title:
Quote:
cleanest way to seperate HTML code in PHP
makes it clear, I think, that the "output" is to "PHP".

Depending on the number of "chunks", there may be "cleaner" more "efficient" methods of "storing" them but, AFAIK, there isn't for "outputting" them, in "PHP". If there is, I'd like to see it as it'd help me out a lot.
Reply With Quote
(#9)
Old
malik_sabri malik_sabri is offline
Senior Member
malik_sabri is on a distinguished road
 
Posts: 146
Join Date: Mar 2009
Default 03-06-2009, 11:28 AM

Quote:
Originally Posted by weishun View Post
The cleanest and most efficient way of storing chunks of HTML for output is to save the "chunks of HTML" and use:
Code:
<?php
include();
?>
That was my first though.
Reply With Quote
(#10)
Old
Megalith Megalith is offline
Junior Member
Megalith is on a distinguished road
 
Posts: 14
Join Date: Feb 2009
Default 03-07-2009, 12:20 PM

Quote:
Originally Posted by weishun View Post
The original post reads, in part:So, we're storing chunks of HTML and they're for output. That being the case, it's likely the "chunks" will be "output" more than once. If so, entering the HTML manually is neither "clean" nor "efficient".

The post title:makes it clear, I think, that the "output" is to "PHP".

Depending on the number of "chunks", there may be "cleaner" more "efficient" methods of "storing" them but, AFAIK, there isn't for "outputting" them, in "PHP". If there is, I'd like to see it as it'd help me out a lot.
very well i will paraphrase using the initial example

PHP Code:
function table(){
echo <<< HTML
<table>
<tr>
<td>Stuff here</td>
</tr>
</table>
HTML;

this to me could be improved like this
PHP Code:
function table(){
?>
<table>
<tr>
<td>Stuff here</td>
</tr>
</table>
<?
}
the echo function is not quick so why use it in this example?
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com