### Example of String Parameter Usage
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Illustrates the output when using the 'sayHi' helper with string parameters enabled.
```html
{{sayHi this edgar}}
```
--------------------------------
### Example of String Parameter Result
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Shows the resulting string after the 'sayHi' helper is applied with string parameters.
```text
Hello edgar!
```
--------------------------------
### Handlebars Template Example (home.hbs)
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
A simple Handlebars template demonstrating iteration over a list of items.
```handlebars
{{#items}}
{{name}}
{{/items}}
```
--------------------------------
### Null Cache Implementation Example
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
A basic implementation of a template cache that bypasses caching and directly parses the source.
```java
Template get(TemplateSource source, Parser parser) throws IOException {
return parser.parse(source);
}
```
--------------------------------
### Handlebars Template Syntax Error Example
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Example of a syntax error in a Handlebars template where an expected token was not found.
```handlebars
{{value
```
--------------------------------
### JSON Data for Template
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Example JSON data structure to be merged with the home.hbs template.
```json
{
"items": [
{
"name": "Handlebars.java rocks!"
}
]
}
```
--------------------------------
### Precompile Helper Example
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
The 'precompile' helper generates JavaScript code from a Handlebars template. It requires the template name as a context.
```html
```
--------------------------------
### Register a List Helper using the Helper Interface
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Register a helper for processing lists. This example iterates over a list and applies the block helper to each item.
```java
handlebars.registerHelper("blog-list", new Helper>() {
public CharSequence apply(List list, Options options) {
String ret = "
";
for (Blog blog: list) {
ret += "
" + options.fn(blog) + "
";
}
return new Handlebars.SafeString(ret + "
");
}
});
```
--------------------------------
### Handlebars.js Scope Resolution Example
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Demonstrates the default scope resolution behavior in Handlebars.js, which does not look up the context stack for missing attributes.
```html
Hello {{#child}}{{value}}{{/child}}
```
--------------------------------
### Handlebars.java Scope Resolution Example
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Illustrates how Handlebars.java resolves scope differently from Handlebars.js by looking up missing attributes in the context stack. Use 'this.' to explicitly refer to the current scope.
```json
{
"value": "parent",
"child": {
}
}
```
```html
Hello {{#child}}{{value}}{{/child}}
```
```html
Hello {{#child}}{{this.value}}{{/child}}
```
--------------------------------
### Generated i18nJs JavaScript Code
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
This is an example of the JavaScript code generated by the 'i18nJs' helper, setting up translations for the I18n.js library.
```javascript
```
--------------------------------
### Use Jackson 1.x JSON Helper in Template
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Example of using the registered Jackson 1.x JSON helper within a Handlebars template to render a context object as JSON.
```handlebars
{{json context [view="foo.MyFullyQualifiedClassName"] [escapeHTML=false] [pretty=false]}}
```
--------------------------------
### Use Jackson 1.x JSON Helper with View Alias in Template
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Example of using the Jackson 1.x JSON helper with a defined view alias in a Handlebars template.
```handlebars
{{json context [view="myView"] [escapeHTML=false] [pretty=false]}}
```
--------------------------------
### Embedded Helper Example
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
The 'embedded' helper allows embedding a Handlebars template directly within an HTML script tag.
```html
...
{{embedded "user"}}
...
```
--------------------------------
### Handlebars Partial Not Found Error
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Example of an error message when a Handlebars partial is not found, including the call stack.
```plaintext
/deep1.hbs:1:5: The partial '/deep2.hbs' could not be found
{{> deep2
^
at /deep1.hbs:1:10
at /deep.hbs:1:10
```
--------------------------------
### Initialize Handlebars with a Custom TemplateLoader
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Demonstrates how to create a Handlebars instance with a custom TemplateLoader implementation.
```java
TemplateLoader loader = ...;
Handlebars handlebars = new Handlebars(loader);
```
--------------------------------
### Running the Handlebars.java Server
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Execute the server JAR from the command line, specifying the directory containing your templates.
```bash
java -jar handlebars-proto-${current-version}.jar -dir myTemplates
```
--------------------------------
### Precompile Helper Usage with Wrapper Option
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Illustrates the usage of the 'precompile' helper with an optional 'wrapper' argument to control the output format.
```handlebars
{{precompile "template" [wrapper="anonymous, amd or none"]}}
```
--------------------------------
### Accessing Template with Multiple Data Sources
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Use the 'data' parameter in the URI to test multiple datasets with a single template. The file extension is not required.
```bash
http://localhost:6780/home.hbs?data=mytestdata
```
--------------------------------
### Run Precompile Goal via Maven Command
Source: https://github.com/jknack/handlebars.java/blob/master/handlebars-maven-plugin/README.md
Execute the precompile goal directly using the Maven command line.
```bash
mvn handlebars:precompile
```
--------------------------------
### Accessing the Rendered Template
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Open a web browser and navigate to this URL to view the rendered template.
```bash
http://localhost:6780/home.hbs
```
--------------------------------
### Register JavaScript Helpers from a File
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Load and register JavaScript helpers defined in a specified file. Ensure the file path is correct.
```java
handlebars.registerHelpers(new File("helpers.js"));
```
--------------------------------
### Run i18njs Goal via Maven Command
Source: https://github.com/jknack/handlebars.java/blob/master/handlebars-maven-plugin/README.md
Execute the i18njs goal directly using the Maven command line.
```bash
mvn handlebars:i18njs
```
--------------------------------
### Configuring Handlebars.js Version for Precompilation
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Shows how to specify a different version of handlebars.js for the precompilation process in Handlebars.java.
```java
Handlebars handlebars = new Handlebars();
handlebars.handlebarsJsFile("/handlebars-v2.0.0.js");
```
--------------------------------
### Initialize Jasmine Environment
Source: https://github.com/jknack/handlebars.java/blob/master/handlebars-maven-plugin/src/test/resources/jasmine/SpecRunner.html
Sets up the Jasmine testing environment, configures the HTML reporter, and defines a spec filter. This code should be included in your SpecRunner.html file to initialize Jasmine.
```javascript
(function() { var jasmineEnv = jasmine.getEnv(); jasmineEnv.updateInterval = 1000; var htmlReporter = new jasmine.HtmlReporter(); jasmineEnv.addReporter(htmlReporter); jasmineEnv.specFilter = function(spec) { return htmlReporter.specFilter(spec); }; var currentWindowOnload = window.onload; window.onload = function() { if (currentWindowOnload) { currentWindowOnload(); } execJasmine(); }; function execJasmine() { jasmineEnv.execute(); } })();
```
--------------------------------
### Compile and Apply Template from Classpath
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
This Java snippet shows how to compile a template named 'mytemplate' located in the classpath and apply it with a given string.
```java
var handlebars = new Handlebars();
var template = handlebars.compile("mytemplate");
System.out.println(template.apply("Handlebars.java"));
```
--------------------------------
### Access Helper Hash Parameters
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Access hash parameters (key-value pairs) passed to a helper using the options.hash() method. This is useful for passing configuration options.
```java
handlebars.registerHelper("blog-list", new Helper() {
public CharSequence apply(List list, Options options) {
String classParam = options.hash("class");
assertEquals("blog-css", classParam);
...
}
});
handlebars.compileInline("{{#blog-list blogs class=\"blog-css\"}}{{/blog-list}}");
```
--------------------------------
### Using Multiple Value Resolvers
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Java code demonstrating how to configure Handlebars.java to use multiple value resolvers in a specific order.
```java
Context context = Context
.newBuilder(model)
.resolver(
MapValueResolver.INSTANCE,
JavaBeanValueResolver.INSTANCE,
FieldValueResolver.INSTANCE
).build();
```
--------------------------------
### Create and Apply a TypeSafeTemplate
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Compile a template inline and cast it to the defined TypeSafeTemplate interface. Set values using setter methods and apply the template.
```java
UserTemplate userTmpl = handlebars.compileInline("{{name}} is {{age}} years old!")
.as(UserTemplate.class);
userTmpl.setAge(32);
assertEquals("Edgar is 32 years old!", userTmpl.apply(new User("Edgar")));
```
--------------------------------
### Configuring a Custom Template Cache
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Demonstrates how to configure Handlebars.java to use a custom template cache implementation.
```java
Handlebars hbs = new Handlebars()
.with(new MyCache());
```
--------------------------------
### Accessing Precompiled Template
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Demonstrates how to access a precompiled Handlebars template in JavaScript.
```js
var template = Handlebars.templates['user']
```
--------------------------------
### Configure TemplateLoader with Prefix and Suffix
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
This Java snippet illustrates configuring a ClassPathTemplateLoader with a custom prefix and suffix to resolve template paths.
```java
TemplateLoader loader = new ClassPathTemplateLoader();
loader.setPrefix("/templates");
loader.setSuffix(".html");
Handlebars handlebars = new Handlebars(loader);
Template template = handlebars.compile("mytemplate");
System.out.println(template.apply("Handlebars.java"));
```
--------------------------------
### i18n Helper with Different Bundle
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Shows how to use the 'i18n' helper with a custom ResourceBundle name, requiring a properties file with that name.
```html
{{i18n "hello" bundle="myMessages"}}
```
--------------------------------
### Access Helper Default Hash Parameters
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Access hash parameters, providing default values if the hash key is not supplied. This simplifies template usage by providing sensible defaults.
```java
handlebars.registerHelper("blog-list", new Helper() {
public CharSequence apply(List list, Options options) {
String class = options.hash("class", "blog-css");
assertEquals("blog-css", class);
...
}
});
handlebars.compileInline("{{#blog-list blogs}}{{/blog-list}}");
```
--------------------------------
### Access Helper Parameters
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Access parameters passed to a helper using the options.param() method. Parameters are zero-indexed.
```java
handlebars.registerHelper("blog-list", new Helper() {
public CharSequence apply(List list, Options options) {
String p0 = options.param(0);
assertEquals("param0", p0);
Integer p1 = options.param(1);
assertEquals(123, p1);
...
}
});
Bean bean = new Bean();
bean.setParam1(123);
Template template = handlebars.compileInline("{{#blog-list blogs \"param0\" param1}}{{/blog-list}}");
template.apply(bean);
```
--------------------------------
### Register a Helper using the Helper Interface
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Register a custom helper by implementing the Helper interface. The apply method defines the helper's logic.
```java
handlebars.registerHelper("blog", new Helper() {
public CharSequence apply(Blog blog, Options options) {
return options.fn(blog);
}
});
```
--------------------------------
### YAML Data for Template
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Alternative YAML data structure for the home.hbs template.
```yaml
items:
- name: Handlebars.java rocks!
```
--------------------------------
### Extending Context Stack with Custom Data
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Java code demonstrating how to extend the Handlebars context stack with custom data like user information and other maps.
```java
hookContextStack(Object model, Template template) {
User user = ....;// Get the logged-in user from somewhere
Map moreData = ...;
Context context = Context
.newBuilder(model)
.combine("user", user)
.combine(moreData)
.build();
template.apply(context);
context.destroy();
}
```
--------------------------------
### Convert Resource Bundles to JavaScript with Maven
Source: https://github.com/jknack/handlebars.java/blob/master/handlebars-maven-plugin/README.md
Configure the plugin to convert Java Resource Bundles to JavaScript files for i18n.js. Specify the output directory, bundle name, and options for merging and AMD module generation.
```xml
com.github.jknackhandlebars-maven-plugin${handlebars-version}i18njsprepare-packagei18njsmessagesfalsefalseUTF-8
```
--------------------------------
### i18n Helper with Message Formatting
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Illustrates using the 'i18n' helper with arguments to interpolate values into message patterns defined in the ResourceBundle.
```html
{{i18n "hello" "Handlebars.java"}}
```
--------------------------------
### Iterate and Display Stock Data with Handlebars
Source: https://github.com/jknack/handlebars.java/blob/master/handlebars/src/test/resources/com/github/jknack/handlebars/bench/stocks.hbs.html
Use the #each helper to loop through stock items and display symbol, name, price, change, and ratio. Access item properties directly or using the @index.
```handlebars
{{#each items base=1}} {{/each}}
#
symbol
name
price
change
ratio
{{&@index}}
[{{&symbol}}](/stocks/{{&symbol}})
[{{&name}}]({{&url}})
**{{&price}}**
{{&change}}
{{&ratio}}
```
--------------------------------
### i18n Helper with Specific Locale
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Demonstrates using the 'i18n' helper to fetch translations for a specific locale, requiring a locale-specific properties file.
```html
{{i18n "hello" locale="es_AR"}}
```
--------------------------------
### Precompile Handlebars Templates with Maven
Source: https://github.com/jknack/handlebars.java/blob/master/handlebars-maven-plugin/README.md
Configure the plugin in your pom.xml to precompile Handlebars/Mustache templates to JavaScript. Specify output file, template prefix, suffix, and handlebars.js file location. Optional settings include minimization, runtime, AMD module generation, encoding, and specific templates to process.
```xml
com.github.jknackhandlebars-maven-plugin${handlebars-version}precompileprepare-packageprecompile${basedir}/src/main/webapp.hbs/handlebars-v1.3.0.jsfalsefalseUTF-8
mytemplateA
mytemplateB
```
--------------------------------
### Compile and Apply Inline Template
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Demonstrates how to compile an inline Handlebars template and apply it with a given string value. This is useful for simple, dynamic string generation.
```java
Handlebars handlebars = new Handlebars();
Template template = handlebars.compileInline("Hello {{this}}!");
System.out.println(template.apply("Handlebars.java"));
```
--------------------------------
### Define Helper Methods in a HelperSource Class
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Define public methods in a class to be used as helpers. Method names become helper names. Context, parameters, and options are optional arguments.
```java
public class HelperSource {
public String blog(Blog blog, Options options) {
return options.fn(blog);
}
public static String now() {
return new Date().toString();
}
public String render(Blog context, String param0, int param1, boolean param2, Options options) {
return ...
}
}
```
--------------------------------
### Precompiled Template Output
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
This is the JavaScript output generated by the 'precompile' helper for the 'user' template.
```html
```
--------------------------------
### Register Helpers from a HelperSource Class
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Register all public static methods from a HelperSource class as helpers. This is useful for utility-like helpers.
```java
handlebars.registerHelpers(HelperSource.class);
```
--------------------------------
### Registering String Helpers
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Note that string helpers are not registered by default and must be explicitly added to the Handlebars instance.
```markdown
> NOTE: You need to register string helpers (they are not added by default)
```
--------------------------------
### Access Helper Default Parameters
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Access parameters passed to a helper, providing default values if the parameter is not supplied. The default value is returned if the parameter index is out of bounds.
```java
handlebars.registerHelper("blog-list", new Helper() {
public CharSequence apply(List list, Options options) {
String p0 = options.param(0, "param0");
assertEquals("param0", p0);
Integer p1 = options.param(1, 123);
assertEquals(123, p1);
...
}
});
Template template = handlebars.compileInline("{{#blog-list blogs}}{{/blog-list}}");
```
--------------------------------
### Register Helpers from a HelperSource Instance
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Register all public methods from a HelperSource instance as helpers. This allows for a more object-oriented approach to helper definition.
```java
handlebars.registerHelpers(new HelperSource());
```
--------------------------------
### Include Helper for Partials
Source: https://github.com/jknack/handlebars.java/blob/master/handlebars-helpers/README.md
The include helper is a port from handlebars.js and supports plain partials, aligning with the Mustache Specification. It is necessary if you intend to use handlebars.js features that are not natively supported by Handlebars.java's partials.
--------------------------------
### Handlebars Template with Block Helper
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
A simple Handlebars template using a block helper.
```handlebars
{{#block}} {{/block}}
```
--------------------------------
### Register a JavaScript Helper
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Register helpers written in JavaScript. This requires a JavaScript file containing Handlebars helper definitions.
```javascript
Handlebars.registerHelper('hello', function (context) {
return 'Hello ' + context;
})
```
--------------------------------
### Enabling String Parameter Access in Helpers
Source: https://github.com/jknack/handlebars.java/blob/master/README.md
Configures Handlebars.java to resolve parameters by name when their values are not present in the context stack, allowing access via options.param(index).
```java
Handlebars handlebars = new Handlebars()
.stringParams(true);
handlebars.registerHelper("sayHi", new Helper