Fork me on GitHub

Template

  • Simple & Friendly
  • High-Performance
  • High-Quality

Integration

HTTL already onboard popular MVC framework, you may also use the API to integrate HTTL.

API Integration

API List

Engine: (loaded immutable, thread safety, please multiplexed single case)

  • Engine.getEngine() Gets engine single case
  • Engine.getTemplate (name) template-based query template instance name
  • Engine.parseTemplate (src) parsing template source code as a template instance
  • Engine.getResource (name) Gets the resource file
  • Engine.hasResource (name) determine resource file exists
  • Engine.getProperty (key) to get the configuration property values ​​
  • Engine.getName() to get the configuration file name
  • Engine.getVersion() Gets HTTL version

Template: (inherited from Node and Resource, each template prototype instance, after loading immutable class, thread-safe, heat load will produce different instances)

  • Template.render (map, writer) parameter-based rendering template content to the output
  • Template.evaluate (map) is evaluated based on the parameter template content
  • Template.getVariables() to obtain the desired parameter type
  • Template.getMacros() Gets the template macro
  • Template.isMacro() whether the current template macro
  • Node.accept (Visitor) access syntax tree
  • Node.getOffset() Gets the source location fragment offset
  • Node.getParent() Gets the parent node
  • Resource.getInputStream() Gets the template source input stream
  • Resource.getReader() Gets the template source reader
  • Resource.getSource() Gets the template source code
  • Resource.getName() Gets the template source name
  • Resource.getEncoding() Gets the template source code
  • Resource.getLastModified() Gets the template source last modified time
  • Resource.getLocale() Gets the template localized area
  • Resource.getLength() Gets the template source length

Context: (inherited from Map, thread-bound instances, the thread stack non-competitive use, thread safety, please do not cross-thread passes)

  • Context.getContext() Gets the current thread context
  • Context.getParent() Gets the previous one context
  • Context.getTemplate() Gets the currently executing template
  • Context.getEngine() Gets the current execution engine
  • Context.getOut() Get the current output
  • Context.getLevel() Gets the current context level
  • Map.get(String) Gets the context variable
  • Map.put(String, Object) written context variable

API integration example

import httl. *;
import java.util. *;

Mapparameters = new HashMap();
parameters.put ("user", user);
parameters.put ("books", books);

Engine engine = Engine.getEngine();
Template template = engine.getTemplate ("/ books.httl");
template.render (parameters, response.getOutputStream());

Note: the default configuration, HTTL not rely on any party libraries, just JDK1.5 + can.

Note: The default JDK must be used to run, if only the JRE, configured as JavassistCompiler.

Multi configuration:

Engine engine = Engine.getEngine("xxx.properties"); // 不同配置产生不同Engine实例

Configure by programing:

Properties properties = new Properties();
properties.setProperty("loaders", "com.your.YourLoader");
Engine engine = Engine.getEngine("xxx", properties); // 不同ID产生不同Engine实例

Get the property:

// single instance property:
Loader loader = engine.getProperty("loaders", Loader.class); 
// multi instances property:
Loader[] loaders = engine.getProperty("loaders", Loader[].class);
// string property:
String encoding = engine.getProperty("output.encoding");

following call is a demonstration that you can get to the integration of information:

// You can pass Template object, called directly in the template: $! {Template}
// Note: HTTL Template object is found, it will pass down directly to the output, instead of copying the results.
parameters.put ("template", template);

// Render to the output based on the parameter:
// Parameters can be Map, or Pojo object, or Object [], or JSON string.
// Out can be OutputStream or Writer.
template.render (parameters, out);

// You can also execute the template to get the rendering results:
String result = (String) template.evaluate (parameters);
// NOTE: If you just to pass the results of the A template template B, please do not use this first evaluation, re-transmission variable approach.
// Because this will waste time memory copy, please direct incoming template, you can reduce the memory footprint of intermediate variables result.
parameters.put ("template", result); // wrong usage, should be directly passed template object

// You can get the template set assignment:
// Note: If you want to get in after completing the template rendering variables, use # set (title:="foo") wrote the Context superiors.
// Because the template when finished will pop Context rendering stack template Context variables will be gone, and only wrote superiors Context variables retained.
String title = (String) Context.getContext(). Get ("title");

// You can also get a template macro: (you can be understood as the macro fragment template)
Template macro = template.getMacros(). Get ("menus");

// If you want to write test tools, you can get the necessary variables to the template and type of data to Mock.
Map>Variables = template.getVariables();

ScriptEngine integration

You can also use the JDK standard script API, so you can not explicitly rely HTTL any class:

import javax.script. *

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName ("httl");

Bindings bindings = engine.createBindings();
bindings.put ("hello","world");

CompiledScript script = engine.compile ("$ {hello}");
String result = (String) script.eval (bindings);

configuration pom.xml dependency:


	
		
			com.github.httl
			httl-script
			1.0.11
			
	

Extended integration

configuration injection

public MyFilter implements Filter {

private String outputEncoding;

// Will be injected httl.properties in output.encoding = UTF-8 configuration items
public void setOutputEncoding (String outputEncoding) {
this.outputEncoding = outputEncoding;
}

private Compiler compiler;

// The compiler will inject httl.properties configuration items
// And will instantiate and initialize the properties of a good compiler
public void setCompiler (Compiler compiler) {
this.compiler = compiler;
}

private Engine engine;

// Will be injected Engine itself
public void setEngine (Engine engine) {
this.engine = engine;
}

// When the property injection after execution
public void init() {
}

public String filter (String value) {
// ...
}

}

support extension points

# method extends
import.methods = StaticMethodClass

# Template loader
loaders = httl.spi.Loader

# Template parser
template.parser = httl.spi.Parser

# Expression parser
expression.parser = httl.spi.Parser

# Template compilation converter
translator = httl.spi.Translator

# JAVA compiler
compiler = httl.spi.Compiler

# Log output
loggers = httl.spi.Logger

# Attribute decision making unit
resolvers = httl.spi.Resolver

# Template cache
template.cache = java.util.Map

# Render parameters of the converter, the return value must be a Map
map.converters = httl.spi.Converter

# Render output converter, the return value must be a Writer or OutputStream
out.converters = httl.spi.Converter

# Interpolation formatter
formatters = httl.spi.Formatter

# Object codec
codecs = httl.spi.Codec

# HTML Dynamic interpolation filter
value.filters = httl.spi.Filter

# HTML static text filter
text.filters = httl.spi.Filter

# Dynamic interpolation position switch
value.filter.switchers = httl.spi.Switcher

# Static text position switch
text.filter.switchers = httl.spi.Switcher

# JS dynamic interpolation filter
script.value.filters = httl.spi.Filter

# JS static text filter
script.text.filters = httl.spi.Filter

# CSS Dynamic interpolation filter
style.value.filters = httl.spi.Filter

# CSS Static text filtering
style.text.filters = httl.spi.Filter

MVC integration

HTTL in MVC position:

MVC

configured search order

(1) First, find / WEB-INF/web.xml in the context-param specified configuration:


	httl.properties
	/ WEB-INF/httl.properties

(Note: If you configure the path begins with a /, said in a Web application directory, or look under the ClassPath)

(2) If not configured, then find the default WEB-INF path :/ WEB-INF/httl.properties

(3) If there is no WEB-INF, then search ClassPath root directory: httl.properties

(4) If there is no ClassPath root directory, use the standard configuration.

variable search order

to $ {foo} as an example:

(1) First, find the current template # set variable assignment.

(2) Find a Business Controller and then return variables.

(3) and then find the requested attributes: request.getAttribute ("foo")

(4) and then look for a request parameter: request.getParameter ("foo")

(5) and then look for the request header: request.getHeader ("foo")

(6) and then look for the temporary session attributes: session.getAttribute ("foo")

(7) and then look for a wild session attributes: cookie.get ("foo")

(8) and then find the application properties: servletContext.getAttribute ("foo")

You can also specify access

domain:

(1) $ {request.foo} returns the requested attributes: request.getAttribute ("foo")

(2) $ {parameter.foo} return request parameters: request.getParameter ("foo")

(3) $ {header.foo} return request header: request.getHeader ("foo")

(4) $ {session.foo} return temporary session attributes: session.getAttribute ("foo")

(5) $ {cookie.foo} return persistent session attributes: cookie.get ("foo")

(6) $ {application.foo} return application attributes: servletContext.getAttribute ("foo")

Servlet integration

you need in your business Servlet in processing the business, the business parameters are written request.setAttribute() in.

HttlFilter Servlet in business execution, read from the template directory with the same name as the request path suffix replaced. httl template, and then request variables for rendering.

To perform non-dot template, you can forward the request to the specified template, HttlServlet read in the template directory path forward over the same name as the template, and then request variables for rendering. For example: request.getRequestDispatcher ("foo.httl"). Forward (request, response);

Configuration / WEB-INF/web.xml:





yourServlet
com.foo.YourServlet
1



yourServlet
*. do



httlServlet
httl.web.servlet.HttlServlet
2



httlServlet
*. httl



httlFilter
httl.web.servlet.HttlFilter



httlFilter
*. do



Configuration / WEB-INF/httl.properties:

import.packages + = com.your.domain
template.directory = / WEB-INF/templates
message.basename = / WEB-INF/messages
input.encoding = UTF-8
output.encoding = UTF-8
reloadable = false
precompiled = false
localized = false

configuration pom.xml dependency:


	
		
			com.github.httl
			httl-servlet
			1.0.11
		
	

sample source code repository:httl-servlet-demo

sample pack download see:download

SpringMVC integration

Configuration / WEB-INF/web.xml:





org.springframework.web.context.ContextLoaderListener



springmvc
org.springframework.web.servlet.DispatcherServlet
1



springmvc
/ *



Configuration / WEB-INF/springmvc-servlet.xml:




	
		
	

configuration pom.xml dependency:


	
		
			com.github.httl
			httl-springmvc
			1.0.11
		
	

sample source code repository:httl-springmvc-demo

sample pack download see:download

Struts integration

Configuration / WEB-INF/web.xml:





struts
org.apache.struts2.dispatcher.FilterDispatcher



struts
/ *



configuration classpath: struts.xml:




	
		
			/ hello_world.httl

	

Configuration / WEB-INF/httl.properties:

import.packages + = com.your.domain
template.directory = / WEB-INF/templates
message.basename = / WEB-INF/messages
input.encoding = UTF-8
output.encoding = UTF-8
reloadable = false
precompiled = false
localized = false

configuration pom.xml dependency:


	
		
			com.github.httl
			httl-struts
			1.0.11
		
	

sample source code repository:httl-struts-demo

sample pack download see:download

Webx integration

Configuration / WEB-INF/web.xml:





webx
com.alibaba.citrus.webx.servlet.WebxFrameworkFilter

excludes
*. css, *. js, *. jpg, *. gif, *. png




webx
/ *



Configuration / WEB-INF/webx.xml:


 Webx Root Context Configuration.



		



Configuration / WEB-INF/httl.properties:

import.packages + = com.your.domain
template.directory = / WEB-INF/templates
message.basename = / WEB-INF/messages
input.encoding = UTF-8
output.encoding = UTF-8
reloadable = false
precompiled = false
localized = false

configuration pom.xml dependency:


	
		
			com.github.httl
			httl-webx
			1.0.11