Skip to content

Instantly share code, notes, and snippets.

@rmorse
Last active May 3, 2022 09:18
Show Gist options
  • Save rmorse/3653f811407ef3a3ec649c8de315085f to your computer and use it in GitHub Desktop.
Save rmorse/3653f811407ef3a3ec649c8de315085f to your computer and use it in GitHub Desktop.
Adds support for `if truthy`, `if falsy`, `if equals` and `if not equals` to Handlebars PHP - https://github.com/salesforce/handlebars-php
<?php
/**
* Adds helpers to Handlebars to support the following expressions:
*
* If truthy - `{{#if_truthy varName}}...{{/if_truthy}}`
* If falsy - `{{#if_falsy varName}}...{{/if_falsy}}`
* If equal - `{{#if_equal varName anotherVar}}...{{/if_equal}}`
* If not equal - `{{#if_not_equal varName anotherVar}}...{{/if_not_equal}}`
*
* Supports the PHP implementation of Handlebars found here: https://github.com/salesforce/handlebars-php
*/
Class Handlebars_Helpers {
private $helpers = array(
'if_equal',
'if_not_equal',
'if_truthy',
'if_falsy',
);
public function register( $handlebars ) {
foreach( $this->helpers as $helper ) {
$handlebars->addHelper( $helper, array( $this, 'helper_' . $helper ) );
}
}
private function render_if( $template, $context ) {
$template->setStopToken( 'else' );
$buffer = $template->render( $context );
$template->setStopToken( false );
$template->discard();
return $buffer;
}
private function render_else( $template, $context ) {
$template->setStopToken( 'else' );
$template->discard();
$template->setStopToken( false );
return $template->render( $context );
}
private function get_args_values( $args, $context ) {
$args_arr = explode( ' ', $args );
$args_arr = array_map( 'trim', $args_arr );
$args_values = array();
foreach ( $args_arr as $arg ) {
$new_arg = '';
if ( $this->is_args_string( $arg ) ) {
$new_arg = trim( $arg, '"' );
} else {
$new_arg = $context->get( $arg );
}
$args_values[] = $new_arg;
}
return $args_values;
}
private function is_args_string( $arg ) {
// Args in quotes are strings.
// TODO - might need to support single quotes?
if ( substr( $arg, 0, 1 ) === '"' && substr( $arg, -1 ) === '"' ) {
return true;
}
return false;
}
public function helper_if_equal( $template, $context, $args, $source ) {
$args_arr = $this->get_args_values( $args, $context );
if ( count( $args_arr ) === 2 && $args_arr[0] === $args_arr[1] ) {
return $this->render_if( $template, $context );
} else {
return $this->render_else( $template, $context );
}
}
public function helper_if_not_equal( $template, $context, $args, $source ) {
$args_arr = $this->get_args_values( $args, $context );
if ( count( $args_arr ) === 2 && $args_arr[0] !== $args_arr[1] ) {
return $this->render_if( $template, $context );
} else {
return $this->render_else( $template, $context );
}
}
public function helper_if_truthy( $template, $context, $args, $source ) {
$args_arr = $this->get_args_values( $args, $context );
if ( count( $args_arr ) === 1 && $args_arr[0] ) {
return $this->render_if( $template, $context );
} else {
return $this->render_else( $template, $context );
}
}
public function helper_if_falsy( $template, $context, $args, $source ) {
$args_arr = $this->get_args_values( $args, $context );
if ( count( $args_arr ) === 1 && ! $args_arr[0] ) {
return $this->render_if( $template, $context );
} else {
return $this->render_else( $template, $context );
}
}
}
<?php
/**
* How to init and add the helpers to handlebars.
*/
require_once 'class-handlebars-helpers.php'
// Start with the default implementation of the Handlbars class as found in the repo:
$partials_dir = '/path/to/partials';
$partials_loader = new FilesystemLoader( $partials_dir, array( 'extension' => 'html' ) );
// Init handlebars.
$handlebars = new Handlebars(
array(
'loader' => $partials_loader,
'partials_loader' => $partials_loader,
)
);
// Now register the helpers
$helpers = new Handlebars_Helpers();
$helpers->register( $handlebars );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment