If you're not familiar with these PHP functions, read this documentation: printf()
/ sprintf()
.
All template code requires validation: if a variable (or array key/value) doesn't exist or doesn't contain data, we don't want to print empty HTML. Writing "pure" PHP with s/printf()
for markup makes validation logic easier to see and shorter to write.
All samples below assume $page_meta = get_fields();
.
Rendering an ACF repeater field is an excellent example of the cleaner code s/printf()
allows. In these code blocks, $page['benefits_list']
is an array containing the keys title
, subtitle
, and url
. The array/list examples below show how many additional open/close PHP tags are necessary for complex pieces of markup & content.
<?php
if ( array_key_exists('benefits_title', $page_meta) ) :
printf('<h3 class="benefits-title">%s</h3>', $page_meta['benefits_title']);
endif;
?>
149 characters
<?php
$title = array_key_exists('benefits_title', $page_meta) ) ? printf('<h3 class="benefits-title">%s</h3>', $page_meta['benefits_title']) : null;
?>
152 characters
<?php if ( array_key_exists('benefits_title', $page_meta) ) : ?>
<h3 class="benefits-title"><?php echo $page_meta['benefits_title']; ?></h3>
<?php endif; ?>
158 characters
<?php
if ( array_key_exists('benefits_list', $page_meta) ) :
$list = null;
foreach ( $page_meta['benefits_list'] as $bl ) :
if ( array_key_exists('title', $bl) && array_key_exists('title', $bl) && array_key_exists('title', $bl) ) :
$list .= sprintf('<li class="benefit-item"><a href="%s"><h3>%s</h3><p>%s</p></a></li>',
$bl['url'],
$bl['title'],
$bl['subtitle']);
endif;
endforeach;
printf('<ul class="benefits-list">%s</ul>', $list);
endif;
?>
491 characters
<?php
if ( array_key_exists('benefits_list', $page_meta) ) :
$list = null;
foreach ( $page_meta['benefits_list'] as $bl ) :
$title = array_key_exists('title', $bl) ? sprintf('<h3>%s</h3>', $bl['title']) : null;
$subtitle = array_key_exists('subtitle', $bl) ? sprintf('<p>%s</p>', $bl['subtitle']) : null;
$url = array_key_exists('url', $bl) ? $bl['url'] : null;
$list .= sprintf('<li class="benefit-item"><a href="%s">%s%s</a></li>',
$url,
$title,
$subtitle);
endforeach;
printf('<ul class="benefits-list">%s</ul>', $list);
endif;
?>
582 characters
<?php if ( array_key_exists('benefits_list', $page_meta) ) : ?>
<ul class="benefits-list">
<?php foreach ( $page_meta['benefits_list'] as $bl ) : ?>
<?php if ( array_key_exists('title', $bl) && array_key_exists('title', $bl) && array_key_exists('title', $bl) ) : ?>
<li class="benefit-item">
<a href="<?php echo $bl['url']; ?>">
<h3><?php echo $bl['title']; ?></h3>
<p><?php echo $bl['title']; ?></p>
</a>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
530 characters
Using s/printf()
makes it easier to see and track open/close conditional tags (which would be even harder in the HTML-first block if a dev used if {}
instead of if :
/endif;
). It also lets PHP syntax alone control all the indentation instead of duplicating indentation with both PHP & HTML indentations.
In this scenario, we'll assume a block of content that has a required title, an optional subtitle, and a required block of WYSIWYG content.
<?php
if ( array_key_exists('section_title', $page_meta) && array_key_exists('section_text', $page_meta) ) :
printf('<h2 class="section-title">%s</h2>', $page_meta['section-title']);
if ( array_key_exists('section_subtitle', $page_meta) ) :
printf('<h3 class="section-subtitle">%s</h3>', $page_meta['section_subtitle']);
endif;
printf('<div class="section-text">%s</div>', $page_meta['section_text']);
endif;
?>
424 characters
<?php
if ( array_key_exists('section_title', $page_meta) && array_key_exists('section_text', $page_meta) ) :
$title = sprintf('<h2 class="section-title">%s</h2>', $page_meta['section-title'];
$subtitle = $array_key_exists('section_subtitle', $page_meta) : sprintf('<h3 class="section">%s</h3>', $page_meta['section_subtitle']);
$text = sprintf('<div class="section-text">%s</div>', $page_meta['section-text'];
echo $title, $subtitle, $text;
endif;
?>
459 characters
<?php if ( array_key_exists('section_title', $page_meta) && array_key_exists('section_text', $page_meta) ) : ?>
<h2 class="section-title"><?php echo $page_meta['section-title']; ></h2>
<?php if ( $array_key_exists('section_subtitle', $page_meta) ) : ?>
<h3 class="section"><?php echo $page_meta['section_subtitle']; ?></h3>'
<h2 class="section-text"><?php echo $page_meta['section_text']; ></h2>
<?php endif; ?>
421 characters