Skip to content
Blocks Code

Handlebars

Handlebars output method lets you write HTML templates with simple logic using the Handlebars syntax. It's perfect for static content and basic conditional rendering.

Handlebars code editor in Lazy Blocks

Basic Usage

Handlebars Example
{{#if control_name}}
  <div class="message">
    {{control_name}}
  </div>
{{/if}}

Available Helpers

In addition to default Handlebars helpers, Lazy Blocks provides custom helpers for WordPress-specific functionality.

truncate

Truncates text to a specified length.

Truncate Example
{{truncate control_name 100 "true"}}
ParameterDescription
StringText to truncate
LimitCharacter limit
DotsShow ellipsis (true/false)

compare

Compares values for conditional rendering.

Compare Example
{{#compare price ">" 100}}
  <span class="premium">Premium Item</span>
{{/compare}}
ParameterDescription
First ValueLeft side value
Operator==, ===, !=, !==, <, >, <=, >=, &&, ||
Second ValueRight side value

math

Performs mathematical operations.

Math Example
<div class="price">
  ${{math price "*" quantity}}
</div>
ParameterDescription
First ValueLeft side value
Operator+, -, *, /, %
Second ValueRight side value

date_i18n

Formats dates using WordPress localization.

Date Example
<time datetime="{{control_date}}">
  {{date_i18n "F j, Y" control_date}}
</time>
ParameterDescription
FormatDate format (e.g., F j, Y H:i)
DateDate string

do_shortcode

Processes WordPress shortcodes.

Shortcode Example
{{{do_shortcode "my_shortcode" this}}}
ParameterDescription
NameShortcode name
AttributesUse this for all block attributes

wp_get_attachment_image

Outputs WordPress image with proper srcset.

Image Example
{{{wp_get_attachment_image image_control "large"}}}
ParameterDescription
ImageImage control value
SizeImage size name (default: thumbnail)

Adding Custom Helpers

Create your own Handlebars helpers:

PHP
function my_custom_handlebars_helper($handlebars) {
    $handlebars->registerHelper('format_price', function($price) {
        return '$' . number_format($price, 2);
    });
}
add_action('lzb/handlebars/object', 'my_custom_handlebars_helper');

Usage:

<div class="price">
  {{format_price product_price}}
</div>

Use Handlebars when you need simple templates with basic logic. For complex dynamic content, consider using PHP output instead.

Was this article helpful?