Utility functions for writing templates for the ProcessWire CMS
Might even be viewed as a kind of optional "template language" for ProcessWire.
The basic idea of this micro-library is to make writing things like this simpler:
echo "<ul>";
echo "<li>Home</li>";
foreach( $page->children as $child ) {
echo "<li><a href='" . $child->url . "'>" . $child->title . "</a></li>";
}
echo "</ul>";With PW Utils, one would instead write:
echo ul( li( 'Home' ) .
do_for( $page->children, function($p) {
return li( a( $p->title, $p->url ) );
})
);This might not look as very big changes at first, but our experience is that when working practically with the templating, the more "functional" (rather than string-padding:y) approach in PWUtils, makes it much easier to make changes to the code without breaking the number of quotes, and also tends to make the code a slight bit easier to read, in our opinion.
- cd /site/templates
- git clone https://github.com/samuell/pwutils.git
- ln -s pwtutils/pwutils.php .
- Add the following code at the top, inside your template php files:
<?php include("pwutils.php"); ?>Find a few more examples below:
echo '<div class="w-col w-col-8 navigation-column">';
echo "<a href='/' class='nav-link'>Welcome</a>";
foreach( $page->siblings as $sibling ) {
echo "<a href='" . $sibling->url . "' class='nav-link'>" . $sibling->title . "</a>";
}
echo '</div>';$a_attrs = array( 'class' => 'nav-link' );
$div_attrs = array( 'class' => 'w-col w-col-8 navigation-column' );
echo div( a( '/', 'Welcome', $a_attrs ) .
do_for( $page->siblings, function($p) {
return a( $p->url, $p->title, $a_attrs );
} ),
$div_attrs
);foreach( $page->image as $img ) {
echo "<a href='" . $img->url . "' >";
echo "<img src='" . $img->url . "' alt='Some alt-text' style='border: 1px solid black;' />";
echo "</a>";
}$styleattrs = array( 'style' => 'border: 1px solid black;' );
foreach( $page->image as $img ) {
echo a( $img->url, img( $img->url, 'Some alt-text', $styleattrs ));
}