Cleanest way to seperate HTML code in PHP

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…

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

although sometimes i use echo depends on context

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.

The cleanest and most efficient way of storing chunks of HTML for output is to save the “chunks of HTML” and use:

<?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.

Zend says that the following is fastest:


<?php
function doOutput() {
?>
HTML goes here...
<?php
}
?>

Rather than…


<?php
echo <<<EOD
HTML goes here
EOD;
?>

or…


include("htmlTpl.tpl");

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.

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

The original post reads, in part:

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:

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.

That was my first though.

very well i will paraphrase using the initial example

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


this to me could be improved like this

function table(){
?>
<table>
<tr>
<td>Stuff here</td>
</tr>
</table>
<?
}

the echo function is not quick so why use it in this example?

You, still, seem to be missing the point; let me try an example.

  1. you have 100 pages
  2. you want “hello world” and “goodbye world”, which we’ll call the “html”, on each page

Regardless of how you paraphrase it, both your suggestion and the “echo” method require the “hello world” and “goodbye world” to be entered on each page. Neither is a “clean” nor “efficient” method of either “storing” the “chunks” of “html” nor of outputting them.

…the echo function is not quick so why use it in this example?
I didn’t use it nor was it used in an example. This was how the original poster displayed “html”. Your suggestion is quicker but it’s, also, irrelevant as it doesn’t the answer the question that was asked. You aren’t “storing” the “chunks”; you’re entering them as required and you’re not “outputting” the html; you’re including it. Again, neither is an answer to the question and neither is “clean” or “efficient”.

I’m sorry if you still don’t understand but I don’t know how to make it any clearer.

The best solution to many problems is not always the one which looks best. in terms of efficiency however if you are using an include then you are calling on additional modules which is not efficient, it may look neater than an example which has all the code in the same module but it is not more efficient. If i had to output 100 pages each containing hello world, goodbye world and some other text which was not the same on each page then i would consider using mysql for it and put the differing html in a database and call it as needed using say a pageID to reference the text needed, in that case indeed you would need to use an echo call as it’s easier (although probably possible without using it).

The original example however is not as complicated and seems to be a function which displays a table every time a function is called. The question on efficiency and cleanliness of code like many programming problems should be solved on an individual basis, there is no efficient solution for every problem.

Most of your post is just a rehash of stuff you’ve, already, said, still doesn’t address the issues of “storing the chunks” or “outputting” and you still seem to be confusing “outputting” with “displaying” as well as ignoring “storing”.

I’m curious about this bit, though:

i would consider using mysql for it and put the differing html in a database and call it as needed using say a pageID to reference the text needed, in that case indeed you would need to use an echo call as it’s easier (although probably possible without using it).
Using a MySQL db occurred to me but I wouldn’t unless there were a significant number of “chunks” and, given the problems people seem to be having with their MySQL servers, not on 000webhost, in any case. If I were to do something along this line, on 000webhost, I’d use a flat file.

What I’m interested in is: “call it as needed using say a pageID to reference the text needed, in that case indeed you would need to use an echo call as it’s easier”. By “pageID”, I’m assuming you mean a unique identifier, assigned to each “chunk”, in the database. Is that correct?

I believe I know what you mean by “use an echo call” but would appreciate an example to confirm my understanding’s correct

“Easier” than what?

yes indeed i mean a unique identifier, a simple paired list with the html stored as a chunk in the database, i did an adventure game ages ago using this method with each unique ID being used as a page number, the routine would have some options (what do you wanna do?) clicking the link would bring up the relevant page and display it, worked well for that application.

The problem, as i am lead to believe, with the echo function is it is not an efficient piece of code (due to the nature of what it is doing i.e. it parses the code and outputs it as html, in many cases there is no need to parse the code (like in the initial example given) and it can be output as html directly). If writing a clumsy looking script without the echo function being used wherever possible means the code runs faster then in my book that makes it an efficient solution (in terms of performance at least).

When i said easier in relation to the MySQL example, say i have the following snippet (i assume the database is connected and that the query has returned a single line of data into an array called $result, the contents of the array for simplicity would be ‘Content’ and would be accessed using ‘PageID’ i.e the page contents are determined from the unique pageID identifier)


echo $result['Content'];

To do the same in html is not so easy, i’d say impossible although i’m sure someone can provide a method to do this without calling an echo function.

If instead of 100’s of pages there where 20 i would use the html direct within the file and use a session or post variable to determine which snippet to use. So as i said each problem has it’s own efficient solution, the initial example given is an overly simplistic one which would be a case of calling the table function and the fixed html being generated for it. I believe in that example the best solution is to not use the echo function but instead to use the HTML directly outside the php compiler as per my earlier post.

swings and roundabouts…

Another solution that occurs to me given 100’s of unique pages would be to name the files in a manner where they can be easily constructed i.e. page1.htm, page2.htm … page999.htm then you can use something like this (assuming that the relevent page needed is in a variable called $page):-


header("Location: page" . $page . ".htm");

this would only work however if no other content was on the page already. I believe the header function is faster than the include function?

Thanks for the reply which confirms my understanding of what you, initially, posted.

As I said, I don’t think using a db is a good idea, unless there are a huge number of these “chunks”. Also, with the problems users are experiencing with their MySQL servers, I don’t think it’d be a good idea here, in any case.

I’ve kinda gotten into this and have an idea I want to try, as soon as I have an hour or so. If it works, I’ll post the code as I think it’d be the basis for a pretty good, lightweight, basic CMS; especially, for people on 000wh or others who don’t have MySQL.

I don’t see why anyone would want to store anything but content in Mysql. I just don’t see the purpose. Unless you have a seperate template file for every single page/post on your site, this would really be almost useless and defeat the whole purpose of the database.

Now if you wanted something like a rotating ad script, that could be something to store into a database, as it is constantly changing, and each page could access this Mysql data wherever it is called from the page.

Just my thoughts,
Cheers,

I break my code into chunks that make logical sense for my site.

Each code segment is included using a php ‘require’ statement. The reason for doing this is that even if only a single code segment is not loaded the whole page should be considered invalid.

The first segment is for the DOCTYPE, then meta data.

After that I build my site from other code segments that contain the header and footer information, left and right columns and main content. I think you get the idea.

I don’t know if this is technically a template but that is how I think of it. Notice in the following example of my, only slightly edited, home page there is ZERO content. The content is in the code segments.

John


<?php require (“php/docType.php”); ?>

<head>

<?php require (“php/metaData.php”); ?>

<title>Key Largo Software</title>

</head>

<body id=“home”>

<div id=“mainContainer”>
<div id=“header”>
<!-- Start Header Content -->

<?php require (“php/header.php”); ?>

<!-- End Header Content -->
</div> <!-- <div id=“header”> -->

    &lt;div id="content"&gt; 

<!-- Start Main (Middle) Content -->

<?php require (“content.php”); ?>

<!-- End Main (Middle) Content -->

    &lt;/div&gt; &lt;!-- &lt;div id="content"&gt; --&gt;

    &lt;div id="left"&gt;

<!-- Start Left Column (Menu) Content -->

<?php require (“left.php”); ?>

<!-- End Left Column (Menu) Content -->

    &lt;/div&gt; &lt;!-- &lt;div id="left"&gt; --&gt;

  &lt;div id="right"&gt; 

<!-- Start Right Column (Menu) Content -->

<?php require (“right.php”); ?>

<!-- End Right Column (Menu) Content -->
</div> <!-- <div id=“right”> -->

</div><!-- <div id=“mainContainer”> -->

<div id=“footer”>
<!-- Start Footer Content -->

<?php require (“php/footer.php”); ?>

<!-- End Footer Content -->

<!–
<?php require (“php/hitCounterCode.php”); ?>
–>

</div> <!-- <div id=“footer”> -->

</body>

</html>

No this is cool. If you take a CMS like Wordpress it breaks everything down to ‘header’, ‘footer’, ‘content’ etc. This is usually an ideal way to go :wink:

Take care.