jinjava

Jinja template engine for Java

  • 所有者: HubSpot/jinjava
  • 平台:
  • 許可證: Apache License 2.0
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

jinjava

Build Status
Coverage status
Maven Central
Join the chat at https://gitter.im/HubSpot/jinjava

Java-based template engine based on django template syntax, adapted to render jinja templates (at least the subset of jinja in use in HubSpot content). Currently used in production to render thousands of websites with hundreds of millions of page views per month on the HubSpot CMS.

Note: Requires Java >= 8. Originally forked from jangod.

Get it:

  <dependency>
    <groupId>com.hubspot.jinjava</groupId>
    <artifactId>jinjava</artifactId>
    <version>{ LATEST_VERSION }</version>
  </dependency>

where LATEST_VERSION is the latest version from CHANGES.

or if you're stuck on java 7:

  <dependency>
    <groupId>com.hubspot.jinjava</groupId>
    <artifactId>jinjava</artifactId>
    <version>2.0.11-java7</version>
</dependency>

Example usage:

my-template.html:

<div>Hello, {{ name }}!</div>

java code:

Jinjava jinjava = new Jinjava();
Map<String, Object> context = Maps.newHashMap();
context.put("name", "Jared");

String template = Resources.toString(Resources.getResource("my-template.html"), Charsets.UTF_8);

String renderedTemplate = jinjava.render(template, context);

result:

<div>Hello, Jared!</div>

Voila!

Advanced Topics

Template loading

Jinjava needs to know how to interpret template paths, so it can properly handle tags like:

{% extends "foo/bar/base.html" %}

By default, it will load only a ClasspathResourceLocator. If you want to allow Jinjava to load any file from the
file system, you can add a FileResourceLocator. Be aware the security risks of allowing user input to prevent a user
from adding code such as {% include '/etc/password' %}.

You will likely want to provide your own implementation of
ResourceLoader to hook into your application's template repository, and then tell jinjava about it:

JinjavaConfig config = new JinjavaConfig();

Jinjava jinjava = new Jinjava(config);
jinjava.setResourceLocator(new MyCustomResourceLocator());

To use more than one ResourceLocator, use a CascadingResourceLocator.

JinjavaConfig config = new JinjavaConfig();

Jinjava jinjava = new Jinjava(config);
jinjava.setResourceLocator(new MyCustomResourceLocator(), new FileResourceLocator());

Custom tags, filters and functions

You can provide custom jinja tags, filters, and static functions to the template engine.

// define a custom tag implementing com.hubspot.jinjava.lib.Tag
jinjava.getGlobalContext().registerTag(new MyCustomTag());
// define a custom filter implementing com.hubspot.jinjava.lib.Filter
jinjava.getGlobalContext().registerFilter(new MyAwesomeFilter());
// define a custom public static function (this one will bind to myfn:my_func('foo', 42))
jinjava.getGlobalContext().registerFunction(new ELFunctionDefinition("myfn", "my_func", 
    MyFuncsClass.class, "myFunc", String.class, Integer.class);

// define any number of classes which extend Importable
jinjava.getGlobalContext().registerClasses(Class<? extends Importable>... classes);

See also

主要指標

概覽
名稱與所有者HubSpot/jinjava
主編程語言Java
編程語言HTML (語言數: 3)
平台
許可證Apache License 2.0
所有者活动
創建於2014-10-13 17:39:22
推送於2025-05-15 09:46:17
最后一次提交2025-05-13 14:42:53
發布數30
最新版本名稱jinjava-2.8.0 (發布於 )
第一版名稱jinjava-1.0.0 (發布於 2014-10-14 10:32:19)
用户参与
星數727
關注者數164
派生數170
提交數3.6k
已啟用問題?
問題數233
打開的問題數98
拉請求數919
打開的拉請求數20
關閉的拉請求數70
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?