rchipka / tagbuilder
This package is not auto-updated.
Last update: 2025-06-08 08:53:04 UTC
README
A lightweight HTML tag building API for PHP
Benefits:
-
No more HTML mangling and switching between <?php mode
-
No more inline attribute logic/toggling
-
Less painful echoing, concatenating
-
Easily store HTML tags in a variable
-
Impossible to write malformed HTML (PHP brackets require balance)
-
Auto-escaped attribute values
-
Able hook/filter all elements/content/attributes
-
Integration with VBTK Context for granular hooking/filtering
-
Won't show tags with empty content (no more if !empty() logic)
Usage:
_tagname() <- echo html __tagname() <- return html
Args:
_tagname($attributes, $content)
$attributes - optional - key/value array of attributes
-
values can be strings, numbers, or arrays
-
given a plain sequential array (numeric keys), values will default to the
class
attr -
if an attribute is set to a key/value array, then the value will determine whether to include the corresponding key (useful for class toggling)
$content - optional - value or array of values
- values can be strings, functions or an array of strings and functions
Examples
<?php _ul( [ 'class' => [ 'no-bullet', 'sidebar-links' ] ], function () { while ( have_rows( 'links' ) ) : the_row(); _li( function () { _h5( get_sub_field('title') ); _ul( [ 'class' => [ 'no-bullet', 'sub-links' ] ], function () { foreach (get_sub_field('links') as $link) : _li( __a([ 'href' => $link['url'] ], $link['title'] ) ); endforeach; }); }); endwhile; });
_div([ 'class' => 'event-meta' ], [ __p([ 'class' => 'event-date' ], function ($expect) { date( 'm.j', $expect( get_field( 'start_date' ) ) ); }), __p([ 'class' => 'event-time' ], function ($expect) { echo implode(' - ', array_unique([ strtoupper( date( 'g:ia', $expect( get_field( 'start_time' ) ) ) ), strtoupper( date( 'g:ia', $expect( get_field( 'end_time' ) ) ) ), ])); }) ]);