Connecting Tech Pros Worldwide Forums | Help | Site Map

Project Design

Richard Stride
Guest
 
Posts: n/a
#1: Jul 17 '05

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

Oli Filth
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Project Design


Richard Stride wrote:[color=blue]
> 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?[/color]

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

Kenneth Downs
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Project Design


Richard Stride wrote:
[color=blue]
>
> 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!
>[/color]

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)
Closed Thread