在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:jknack/handlebars.java开源软件地址:https://github.com/jknack/handlebars.java开源编程语言:Java 74.8%开源软件介绍:Handlebars.javaLogic-less and semantic Mustache templates with JavaHandlebars handlebars = new Handlebars();
Template template = handlebars.compileInline("Hello {{this}}!");
System.out.println(template.apply("Handlebars.java")); Output:
Handlebars.java is a Java port of handlebars. Handlebars provides the power necessary to let you build semantic templates effectively with no frustration. Mustache templates are compatible with Handlebars, so you can take a Mustache template, import it into Handlebars, and start taking advantage of the extra Handlebars features. Requirements
Getting StartedIn general, the syntax of Handlebars templates is a superset of Mustache templates. For basic syntax, check out the Mustache manpage. The Handlebars.java blog is a good place for getting started too. Javadoc is available at javadoc.io. MavenStable version: <dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
<version>${handlebars-version}</version>
</dependency> Loading templatesTemplates are loaded using the
This example loads mytemplate.hbs:
Handlebars handlebars = new Handlebars();
Template template = handlebars.compile("mytemplate");
System.out.println(template.apply("Handlebars.java")); Output:
You can specify a different TemplateLoader loader = ...;
Handlebars handlebars = new Handlebars(loader); Templates prefix and suffixA
Example: 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")); Handlebars.java will resolve The Handlebars.java ServerThe handlebars.java server is small application where you can write Mustache/Handlebars template and merge them with data. It is a useful tool for Web Designers. Download from Maven Central:
Maven: <dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars-proto</artifactId>
<version>${current-version}</version>
</dependency> Usage:
Example: myTemplates/home.hbs
myTemplates/home.json {
"items": [
{
"name": "Handlebars.java rocks!"
}
]
} or if you prefer YAML myTemplates/home.yml: items:
- name: Handlebars.java rocks! Open a browser a type:
enjoy it! Additional options:
Multiple data sources per templateSometimes you need or want to test multiple datasets over a single template, you can do that by setting a Example:
Please note you don't have to specify the extension file. HelpersBuilt-in helpers:
with, each, if, unless:See the built-in helper documentation. block and partialBlock and partial helpers work together to provide you Template Inheritance. Usage:
context: A string literal which defines the region's name. Usage:
context: A string literal which defines the region's name. precompilePrecompile a Handlebars.java template to JavaScript using handlebars.js user.hbs Hello {{this}}! home.hbs <script type="text/javascript">
{{precompile "user"}}
</script> Output: <script type="text/javascript">
(function() {
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['user'] = template(function (Handlebars,depth0,helpers,partials,data) {
helpers = helpers || Handlebars.helpers;
var buffer = "", functionType="function", escapeExpression=this.escapeExpression;
buffer += "Hi ";
depth0 = typeof depth0 === functionType ? depth0() : depth0;
buffer += escapeExpression(depth0) + "!";
return buffer;});
})();
</script> You can access the precompiled template with: var template = Handlebars.templates['user'] By default it uses: Handlebars handlebars = new Handlebars();
handlebars.handlebarsJsFile("/handlebars-v2.0.0.js"); For more information have a look at the Precompiling Templates documentation. Usage:
context: A template name. Required. wrapper: One of "anonymous", "amd" or "none". Default is: "anonymous" There is a maven plugin available too. embeddedThe embedded helper allow you to "embedded" a handlebars template inside a user.hbs <tr>
<td>{{firstName}}</td>
<td>{{lastName}}</td>
</tr> home.hbs <html>
...
{{embedded "user"}}
...
</html> Output: <html>
...
<script id="user-hbs" type="text/x-handlebars">
<tr>
<td>{{firstName}}</td>
<td>{{lastName}}</td>
</tr>
</script>
...
</html> Usage:
context: A template name. Required. i18nA helper built on top of a {@link ResourceBundle}. A {@link ResourceBundle} is the most well known mechanism for internationalization (i18n) in Java. Usage: {{i18n "hello"}} This require a Using a locale: {{i18n "hello" locale="es_AR"}} This requires a Using a different bundle: {{i18n "hello" bundle="myMessages"}} This requires a Using a message format: {{i18n "hello" "Handlebars.java"}} Where i18nJsTranslate a Usage:
If the locale argument is present it will translate that locale to JavaScript. Otherwise, it will use the default locale. The generated code looks like this: <script type="text/javascript">
I18n.defaultLocale = 'es_AR';
I18n.locale = 'es_AR';
I18n.translations = I18n.translations || {};
// Spanish (Argentina)
I18n.translations['es_AR'] = {
"hello": "Hi {{arg0}}!"
}
</script> Finally, it converts message patterns like: string helpersFunctions like abbreviate, capitalize, join, dateFormat, yesno, etc., are available from StringHelpers.
conditional helpersFunctions like eq, neq, lt, gt, and, or, not, etc., are available from ConditionalHelpers.
TypeSafe TemplatesTypeSafe templates are created by extending the // 1
public static interface UserTemplate extends TypeSafeTemplate<User> {
// 2
public UserTemplate setAge(int age);
public UserTemplate setRole(String role);
}
// 3
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")));
Registering HelpersThere are two ways of registering helpers.
Using the |
请发表评论