This is a utility class which I’ve used in a basic form for a while now to quickly generate HTML programmatically in PHP.
The idea behind it is that I wanted a fairly simple object that would represent an HTML tag element. Once created, the content and attributes can be changed dynamically and when ready, a
render() method can be called to output the HTML.
I know there are loads of templating classes out there and you could use the built in PHP XML functions to do this, but they are overkill for what I want. This is particularly useful for rapid prototyping and testing. You can quickly create HTML output without having to actually write any HTML. Plus this is an interesting programming exercise.
So far, it’s pretty simple:
$tag = new Tag( 'div', ['class' => 'my_class'], 'my content' ); $tag->render(); // outputs <div class="my_class">my content</> $tag->setAttribute('class' => 'new_class'); $tag->render(); // outputs <div class="new_class">my content</>
The next step is to make it recursive and then add some kind of find/ search method to target embedded Tags.
The project is here: https://bitbucket.org/dgifford/tag