4.12. Handlebars Helpers

Cradle uses Handlebars for the templating engine. We chose Handlebars for it’s simplicity and it’s availability in other programming languages. As well as the original Handlebars helpers. Cradle adds over 30 more reusable helpers to use within your templates.

4.B.1. {{capital}}

Capitalizes the given value.

Parameters
  • string The string to parse
Usage
<!-- Foo Bar -->
{{capital 'foo bar'}}

<!-- Bar -->
{{capital foo}}

4.B.2. {{upper}}

Uppercases the given value.

Parameters
  • string The string to parse
Usage
<!-- FOO BAR -->
{{upper 'foo bar'}}

<!-- BAR -->
{{upper foo}}

4.B.3. {{lower}}

Lowercases the given value.

Parameters
  • string The string to parse
Usage
<!-- foo bar -->
{{lower 'Foo Bar'}}

<!-- bar -->
{{lower foo}}

4.B.4. {{chars}}

Limits the output by the given amount of characters.

Parameters
  • string The string to parse
  • int The amount of characters to show
Usage
<!-- foo -->
{{chars 'foo bar' 3}}

<!-- b -->
{{chars foo 1}}

4.B.5. {{words}}

Limits the output by the given amount of words.

Parameters
  • string The string to parse
  • int The amount of words to show
Usage
<!-- foo -->
{{words 'foo bar' 1}}

<!-- Went to -->
{{words zoo 2}}

4.B.6. {{strip}}

Strip HTML tags

Parameters
  • string The string to parse
Usage
<!-- foo bar -->
{{strip '<b>foo</b> <em>bar</em>'}}

<!-- foo <em>bar</em> -->
{{strip '<b>foo</b> <em>bar</em>' '<em>'}}

4.B.7. {{markdown}}

Converts markdown to HMTL

Parameters
  • string The string to parse
Usage
<!-- foo <em>bar</em> -->
{{markdown 'foo *bar*'}}

4.B.8. {{number}}

Formats a number using commas and decimals

Parameters
  • int The number to parse
  • int The number of decimal places
Usage
<!-- 1,000 -->
{{number 1000}}

<!-- 1,000.00 -->
{{number 1000 2}}

4.B.9. {{price}}

Formats a number to a price format

Parameters
  • int The number to parse
Usage
<!-- 1,000.00 -->
{{price 1000}}

4.B.10. {{formula}}

Computes the given formula

Parameters
  • string The template string to parse
Usage
{{formula '1000 + {{amount}}'}}

4.B.11. {{number_format_short}}

Returns a short version of the number

Parameters
  • int The number to parse
Usage
<!-- 100 -->
{{number_format_short 100}}

<!-- 1K -->
{{number_format_short 1000}}

<!-- 1M -->
{{number_format_short 1000000}}

4.B.12. {{date}}

Returns a date time format

Parameters
  • string The raw date
  • string The date format
Usage
<!-- January 01, 2018 -->
{{date '2018-01-01' 'F d, Y'}}

4.B.13. {{relative}}

Returns a date time format relative to now

Parameters
  • string The raw date
  • string The date format
Usage
<!-- 3 months ago -->
{{relative '2018-01-01' 'F d, Y'}}

4.B.14. {{join}}

Transforms an array to string

Parameters
  • array The array to join
  • string The separator
Usage
<!-- 1, 2 -->
{{join '1 2' ', '}}

4.B.15. {{split}}

Splits a string into an array

Parameters
  • string The string to split
  • string The separator
Usage
<!-- foo,bar,zoo, -->

{{#split 'foo,bar,zoo' ','}}{{this}},{{/split}}

4.B.16. {{scope}}

Traverses into the specified array path

Usage

{{#scope list '0'}}
    {{@key}} -> {{this}}
{{/scope}}

4.B.17. {{query}}

Manipulates $_GET and returns the final query

  • if 1 argument, will return the key value in $_GET (should be scalar)
  • if 2 or more arguments, will set the path and return the final query
Usage

{{query 'q'}}

{{query 'q' 'foobar'}}

4.B.18. {{sorturl}}

Manipulates sort order and returns the final query

Parameters
  • string[,string..] url path
Usage

<!-- look for the sorting value at ?order[profile_id] -->
{{sorturl 'order' 'profile_id'}} //--> ASC|DESC|null

4.B.19. {{sortcaret}}

Determines the caret to be used (needs fontawesome 5)

Parameters
  • string[,string..] url path
Usage

<!-- look for the sorting value at ?order[profile_id] -->
{{sortcaret 'order' 'profile_id'}} //--> <i class="fas fa-caret-up"></i>|<i class="fas fa-caret-down"></i>|null

4.B.20. {{redirecturl}}

Returns the current url encoded, used for recdirect flags

Usage
{{redirecturl}}

4.B.21. {{pager}}

Uses a block to generate the pagination

Parameters
  • int total
  • int range
Usage

<ul>
    {{#pager 200 50}}
        <li>{{page}}</li>
    {{/pager}}
</ul>

4.B.22. {{or}}

  • If the 1st argument is populated and evaluates to true, the same argument will be returned
  • Otherwise the 2nd argument will be returned
Parameters
  • scalar The value to be tested
  • scalar The default value otherwise
Usage
{{or 1 0}}

4.B.23. {{when}}

A better if statement for handlebars.

Parameters
  • scalar The comparative value 1
  • string The comparative operator
  • scalar The comparative value 2
Supported Operators
  • ==
  • ===
  • !=
  • !==
  • <
  • <=
  • >
  • >=
  • ||
  • &&
Usage
<!-- No -->

{{#when 1 '===' '1'}}
    Yes
{{else}}
    No
{{/if}}

4.B.24. {{otherwise}}

The opposite of when

Parameters
  • scalar The comparative value 1
  • string The comparative operator
  • scalar The comparative value 2
Supported Operators
  • ==
  • ===
  • !=
  • !==
  • <
  • <=
  • >
  • >=
  • ||
  • &&
Usage
<!-- Yes -->

{{#otherwise 1 '===' '1'}}
    Yes
{{else}}
    No
{{/if}}

4.B.25. {{has}}

Checks to see if a key exists

Parameters
  • array The array
  • string The key
Usage

{{#has post 'title'}}
    Yes
{{else}}
    No
{{/if}}

4.B.26. {{hasnt}}

Checks to see if a key does not exists

Parameters
  • array The array
  • string The key
Usage

{{#hasnt post 'title'}}
    Yes
{{else}}
    No
{{/if}}

4.B.27. {{in}}

Checks to see if the given array has a value

Parameters
  • array The array
  • scalar The value
Usage

{{#in post 'title'}}
    Yes
{{else}}
    No
{{/if}}

4.B.28. {{notin}}

Checks to see if the given array does not have a value

Parameters
  • array The array
  • scalar The value
Usage

{{#notin post 'title'}}
    Yes
{{else}}
    No
{{/if}}

4.B.29. {{compile}}

Calls the compiler again to compile the given string (recursive)

Parameters
  • string The template
  • array The template variables
Usage
{{compile '{{post_title}}' post}}

4.B.30. {{partial}}

Calls the compiler again to compile the given string (recursive)

Parameters
  • string The name of the partial
  • array The template variables
Usage
{{partial 'name' post}}

4.B.31. {{inspect}}

Calls the compiler again to compile the given string (recursive)

Parameters
  • mixed Force outputs any handlebars variables
Usage
{{inspect post}}

4.B.32. {{fileinfo}}

Reads the given filename and determines the file information

Parameters
  • string Path of the file
Usage

{{#fileinfo '/some/path/to/image.jpg'}}
    {{name}} - image.jpg
    {{base}} - image
    {{path}} - /some/path/to
    {{extension}} - jpg
    {{mime}} - image/jpg
{{/fileinfo}}

4.B.33. {{request}}

Gives access to the current request object

Parameters
  • scalar[,scalar..] The request path
Usage
<!-- 127.0.0.1 -->
{{request 'server' 'HTTP_HOST'}}

4.B.34. {{response}}

Gives access to the current response object

Parameters
  • scalar[,scalar..] The response path
Usage
<!-- false -->
{{response 'json' 'error'}}

4.B.35. {{_}}

i18n translation engine. Translate the given string based on the current language.

Parameters
  • string The string to be translated
Usage
<!-- nom -->
{{_ 'Name'}}

4.B.36. {{settings}}

Returns the settings in config/settings.php

Parameters
  • string The key in settings
Usage
<!-- dev -->
{{settings 'environment'}}