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