Skip to content

Instantly share code, notes, and snippets.

@jdsteinbach
Last active August 29, 2015 14:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdsteinbach/c4760741b8c5f0284725 to your computer and use it in GitHub Desktop.
Save jdsteinbach/c4760741b8c5f0284725 to your computer and use it in GitHub Desktop.
WP Template Patterns: `sprint()` & `printf()`

Documentation

If you're not familiar with these PHP functions, read this documentation: printf() / sprintf().

Benefits of s/printf()

Cleaner Validation

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();.

Tighter Code

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.

Examples

Single Value

PHP with s/printf()

<?php
if ( array_key_exists('benefits_title', $page_meta) ) :
  printf('<h3 class="benefits-title">%s</h3>', $page_meta['benefits_title']);
endif;
?>

149 characters

PHP Shorthand Validation with printf()

<?php
$title = array_key_exists('benefits_title', $page_meta) ) ? printf('<h3 class="benefits-title">%s</h3>', $page_meta['benefits_title']) : null;
?>

152 characters

HTML-first + PHP

<?php if ( array_key_exists('benefits_title', $page_meta) ) : ?>
  <h3 class="benefits-title"><?php echo $page_meta['benefits_title']; ?></h3>
<?php endif; ?>

158 characters

Array -> List

PHP with s/printf()

<?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 Shorthand Validation with s/printf()

<?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

HTML-first + PHP

<?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.

Mixing Required & Optional ACF Field Content

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 with s/print()

<?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 Shorthand Validation with s/printf()

<?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

HTML-first + PHP

<?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

@weeirishman
Copy link

<?php if ( array_key_exists('section_title', $page_meta) && array_key_exists('section_text', $page_meta) ) {
  echo '<h2 class="section-title">' . $page_meta['section-title'] . '</h2>';
  if ( $array_key_exists('section_subtitle', $page_meta) ) {
    echo '<h3 class="section">' . $page_meta['section_subtitle'] . '</h3>';
  }
  echo '<h2 class="section-text">' . $page_meta['section_text'] . '</h2>';
} ?>

@weeirishman
Copy link

<?php
  extract( $page_meta );
  if( isset( $section_title, $section_text) ) {
    echo '<h2 class="section-title">' . $section_title . '</h2>';
    if( isset( $section_subtitle ) {
      echo '<h3 class="section">' . $section_subtitle . '</h3>';
      echo '<h2 class="section-text">' . $section_text . '</h2>';
    }
  } ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment