By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,770 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,770 IT Pros & Developers. It's quick & easy.

Project Design

P: n/a

I am fairly new to PHP and have written an application for managing spam in
a quarantine like environment.

Now the problem has arisen that Branding will eventually come into the
picture as well as localisation.

I prefer to embed php code into the HTML segments of the page, the dynamic
PHP components having been populated by middleware classes that can take
care of all the business logic.

But now I am finding that it might be better to write classes that generate
blocks of HTML instead of having dynamic elements of PHP inside HTML.

What have other users experiences been like? Do you prefer to write
functions that generate HTML or do you prefer to have HTML blocks with PHP
in them?

Looking forward to reply's!
Rich
--
beep beep
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Richard Stride wrote:
I am fairly new to PHP and have written an application for managing spam in
a quarantine like environment.

Now the problem has arisen that Branding will eventually come into the
picture as well as localisation.

I prefer to embed php code into the HTML segments of the page, the dynamic
PHP components having been populated by middleware classes that can take
care of all the business logic.

But now I am finding that it might be better to write classes that generate
blocks of HTML instead of having dynamic elements of PHP inside HTML.

What have other users experiences been like? Do you prefer to write
functions that generate HTML or do you prefer to have HTML blocks with PHP
in them?


Neither!

I use a custom HTML template system, which allows me to write HTML
files (one for each page, or sub-section e.g. a news article template)
completely independently of the PHP code. The HTML contains
placeholders, e.g. {USER_NAME} or {HEADLINE}. I also have a switching
system, so that HTML sections can be blanked out conditionally. Might
look something like:

<DIV>
<H1>{HEADLINE}</H1>
<P>{BODY}</P>
{IS_UGLY?}<P>One paragraph.</P>{/IS_UGLY?}
{~IS_UGLY?}<P>An alternative paragraph.</P>{/~IS_UGLY?}
</DIV>

I then have a function which parses the template file, and replaces the
placeholders with strings. So I can just call something like:

parseTemplate("home.html", array("USER_NAME" => $userName,
"EMAIL_ADDRESS" => $email,
"HEADLINE" => $headline,
"BODY" => $newsBody,
"IS_UGLY?" => true));

This may seem like a lot of work to set up, and overkill, and some
might say that it's inefficient at runtime. But from a maintainability
and clarity point of view, it's perfect. There's a sharp interface
between my PHP scripts and HTML design, it's immediately obvious what
variables are going where, and I can design the HTML and PHP completely
separately. And there's no messy nesting of HTML in PHP (so no escape
characters that reduce clarity) or vice versa.

--
Oli

Jul 17 '05 #2

P: n/a
Richard Stride wrote:

I am fairly new to PHP and have written an application for managing spam
in a quarantine like environment.

Now the problem has arisen that Branding will eventually come into the
picture as well as localisation.

I prefer to embed php code into the HTML segments of the page, the dynamic
PHP components having been populated by middleware classes that can take
care of all the business logic.

But now I am finding that it might be better to write classes that
generate blocks of HTML instead of having dynamic elements of PHP inside
HTML.

What have other users experiences been like? Do you prefer to write
functions that generate HTML or do you prefer to have HTML blocks with PHP
in them?

Looking forward to reply's!


This has been well covered recently and there is a FAQ entry, but here is my
$.02.

The FAQ entry says it best, PHP is itself a templating system, separation of
logic and layout are accomplished by habit and practice, not by technology.

So if you look at templating systems, you will see stuff like:

<h1>{Title}</h1>

Whereas if you just assign pieces of HTML into a lot of variables, you have
this:

<h1><?=$Title?></h1>

The only difference is that in the second case you don't have to learn a new
language or parse the file.

You can also go the other way in building strings, such as this:

$HTML_BigOutput .= "<h1>".$Title."</h1>"

but I personally have found that cumbersome and problematic.

With all of that said, when we make a page "brandable" we identify the
smallest fragments of HTML that cannot be divided, assign them to
variables, and then include an HTML file, like so:

$Title = "Page Title";
$text = "This is some simple text";
include("HTML_Output.php");

....and the file HTML_Output.php looks like:

<html>
<head>
<title><?=$Title?></title>
</head>
<body>
<h1><?=$Title?></h1>
<?=$text=?>
</body>
</html>

You can then replace the HTML_Output.php file with something that has
different graphics, placement of objects and so forth.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.