views:

70

answers:

3

Trying to come up with a basic way to display code in a way that would be nicely maintainable, I thought of doing something like this:

echo htmlfunction('a',array('href'=>'http://google.com'),'google');

to generate:

<a href="http://google.com"&gt;google&lt;/a&gt;

this would use as a global scale function to output all html tags. that way, if I ever wanted to change the way my html is displayed, I could easily do it with one tweak.

does using this kind of outputting mean a grave loss of agility in performance?

thanks.

+2  A: 

No, but you would be reinventing the wheel and in a wrong way.

Instead of writing echo title("bar"); to echo <title>bar</title>, use a templating engine to output your dynamic data inside a static HTML template, that looks like <title>$title</title> where your code has $template->assign('title','bar');.

That way you will be keeping markup as markup and code as code and in separate files, which is a worthy goal in at least the maintainability and flexibility departments.

Look, for example, at Smarty

Vinko Vrsalovic
Agreed. It's extremely hard to come up with something that's "nicely maintainable" while keeping the markup in the code. Some degree of separation is generally the key.
Brian McKenna
I see your point, thanks, I'll look into smarty then.
sombe
A: 

No, it would not cause a grave loss of agility -- it would likely be hardly noticeable. Many PHP content management systems (such as Drupal and Joomla) do the same sort of thing for all their links.

Kaleb Brasee
+2  A: 

I would avoid writing functions like that in most cases. Notice how it takes more code to write than you actually have output. That's also additional processing the server is going to need to do each and every time. It probably won't be a huge performance problem at first, but if used excessively, it could be bad.

If you want to abstract presentation from logic, might I recommend a template engine like Smarty

Dominic Barnes