Download the PHP package salesforce/handlebars-php without Composer
On this page you can find all versions of the php package salesforce/handlebars-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download salesforce/handlebars-php
More information about salesforce/handlebars-php
Files in salesforce/handlebars-php
Package handlebars-php
Short Description Handlebars processor for php
License MIT
Homepage http://www.github.com/salesforce/handlebars-php
Informations about the package handlebars-php
handlebars-php
A simple, logic-less, yet powerful templating engine for PHP
Name: handlebars-php
License: MIT
Requirements: PHP >= 5.4
About Handlebars
Handlebars provides the power necessary to let you build semantic templates effectively with no frustration, that keep the view and the code separated like we all know they should be.
Fork of: Handlebars.php by XaminProject
Handlebars, is the PHP port of Handlebars.js
Install Handlebars
You can just download Handlebars.php as is, or with Composer.
To install with composer, add the following in the require key in your composer.json file
"salesforce/handlebars-php": "1.*"
composer.json
Getting Started
At the minimum, we are required to have an array model and a template string. Alternatively we can have a file containing handlebars (or html, text, etc) expression.
Template
Handlebars templates look like regular HTML, with embedded handlebars expressions.
Handlebars HTML-escapes values returned by a {{expression}}.
The string above can be used as is in your PHP file, or be put in a file (ie: /templates/main.tpl), to be called upon rendering.
PHP file
Now the we've created our template file, in a php file (index.php) we'll create the data to passed to the model. The model is a key/value array.
Below we are going to create the Handlebars object, set the partials loader, and put some data in the model.
/index.php
Assign Data
The simplest way to assign data is to create an Array model. The model will contain all the data that will be passed to the template.
Render Template
Use the method Handlebars\Handlebars::render($template, $model)
to render you template once everything is created.
$template : Template can be the name of the file or a string containing the handlebars/html.
$model : Is the array that we will pass into the template
The code below will render the model to the templates/main.tpl template
Alternatively you can use $handlebars itself without invoking the render method
Expressions
Let's use this simple model for the following examples, assuming everything is already set like above.
Let's work with the template.
Handlebars expressions are the basic unit of a Handlebars template. You can use them alone in a {{mustache}}, pass them to a Handlebars helper, or use them as values in hash arguments.
The simplest Handlebars expression is a simple identifier:
Handlebars nested expressions which are dot-separated paths.
Handlebars nested expressions in an array.
Handlebars also allows for name conflict resolution between helpers and data fields via a this reference:
Handlebars expressions with a helper. In this case we're using the upper helper
Nested handlebars paths can also include ../ segments, which evaluate their paths against a parent context.
Handlebars HTML-escapes values returned by a {{expression}}. If you don't want Handlebars to escape a value, use the "triple-stash", {{{ }}}
Control Structures
if/else
and unless
control structures are implemented as regular Handlebars helpers
IF/ELSE
You can use the if helper to conditionally render a block. If its argument returns false, null, "" or [] (a "falsy" value), Handlebars will not render the block.
Example
UNLESS
You can use the unless helper as the inverse of the if helper. Its block will be rendered if the expression returns a falsy value.
Iterators: EACH
You can iterate over a list using the built-in each helper. Inside the block, you can use {{this}} or {{.}} to reference the element being iterated over.
Example
EACH/ELSE
You can optionally provide an {{else}} section which will display only when the list is empty.
Slice EACH Array[start:end]
The #each helper (php only) also has the ability to slice the data
- {{#each Array[start:end]}} = starts at start through end -1
- {{#each Array[start:]}} = Starts at start though the rest of the array
- {{#each Array[:end]}} = Starts at the beginning through end -1
- {{#each Array[:]}} = A copy of the whole array
- {{#each Array[-1]}}
- {{#each Array[-2:]}} = Last two items
- {{#each Array[:-2]}} = Everything except last two items
{{@INDEX}} and {{@KEY}}
When looping through items in each, you can optionally reference the current loop index via {{@index}}
Change Context: WITH
You can shift the context for a section of a template by using the built-in with block helper.
Handlebars Built-in Helpers
If
Unless
Each
With
Other Helpers
For convenience, Voodoo\Handlebars added some extra helpers.
Upper
To format string to uppercase
Lower
To format string to lowercase
Capitalize
To capitalize the first letter
Capitalize_Words
To capitalize each words in a string
Reverse
To reverse the order of string
Format_Date
To format date: {{#format_date date '$format'}}
Inflect
To singularize or plurialize words based on count {{#inflect count $singular $plurial}}
Truncate
To truncate a string: {{#truncate title $length $ellipsis}}
Default
To use a default value if the string is empty: {{#default title $defaultValue}}
Raw
This helper return handlebars expression as is. The expression will not be parsed
Repeat
To truncate a string: {{#repeat $count}}{{/repeat}}
Variable and blocks can still be used
Define/Invoke
Allow to define a block of content and use it later. It helps follow the DRY (Don't repeat yourself) principle.
Define
Invoke
Example:
Template Comments
You can use comments in your handlebars code just as you would in your code. Since there is generally some level of logic, this is a good practice.
Partials
Partials are other templates you can include inside of the main template.
To do so:
which is a file under /templates/my_partial.html
Writing your own helpers
Block helpers make it possible to define custom iterators and other helpers that can invoke the passed block with a new context.
To create your own helper, use the method: Handlebars::addHelper($name, $callback)
The following helper will UPPERCASE a string
And now we can use the helper like this:
Data Variables for #each
In Handlebars JS v1.1, data variables @first
and @last
were added for the #each helper. Due to the these variables
not being backwards compatible, these data variables are disabled by default and must be enabled manually.
To enable the new data variables, set the enableDataVariables
option to true
when instantiating the Handlebars
instance.
Given the following template and data:
The output will be
Given the following template and the data above:
The output will be
Data variables also support relative referencing within multiple #each statements. Given
The output will be
Be aware that when data variables are enabled, variables starting with @
are considered restricted and will override
values specified in the data.
For example, given the following template and the following data, the output will be different depending on if data variables are enabled.
When enableDataVariables
is false
, existing behavior is not changed where some variables will be return.
When enableDataVariables
is true
, the behavior matches HandlebarsJS 1.1 behavior, where all data variables replace
variables defined in the data and any data variable prefixed with @
that is unknown will be blank.
Credits
- Fork of Handlebars.php by XaminProject
- The documentation was edited by Mardix.
Contribution
Contributions are more than welcome!