@Override public void sendContent( OutputStream out, Range range, Map<String, String> params, String contentType ) throws IOException { ITemplate template = editee.getTemplate(); if( template == null ) { log.debug( "-- no template. using default" ); String sTemplate = FileUtils.readResource( this.getClass(), "defaultEdit.vel" ); VelocityContext vc = new VelocityContext(); vc.put( "editee", editee ); vc.put( "targetPage", this ); vc.put( "params", params ); RenderContext rc = new RenderContext( template, editee, null, true ); vc.put( "renderContext", rc ); VelocityInterpreter.evalToStream( sTemplate, vc, out ); out.flush(); } else { log.trace("generate edit page with template: " + template.getName()); RenderContext rc = new RenderContext( template, editee, null, true ); if( params != null && params.size() > 0 ) { editee.preProcess(rc, params, null); } String s = editee.render( rc, params, template, true); //String s = template.render( rc, params, template); if( s == null ) { log.warn( "Got null content for editee: " + editee.getHref() ); return; } else { out.write( s.getBytes() ); } } }
@Test public void testStringTemplateComponent() { final MustacheFactory factory = new RapidoidMustacheFactory(); ITemplate template = new MustacheStringTemplate(factory, "(~x~)-(~y~)"); eq(template.render(U.map("x", 1), U.map("y", "2")), "1-2"); }
@Test public void testFileTemplateComponent() { final MustacheFactory factory = new RapidoidMustacheFactory(); ITemplate template = new MustacheFileTemplate(factory, "main.html"); eq(template.render(U.map("x", 1), U.map("y", "s")), "A-s-B-s-:1"); }
// 模版方法模式:定义一个操作中算法的骨架,而将一些步骤延迟到子类中。 // 模版方法使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定步骤。 public static void main(String[] args) { ITemplate template = new TemplateA(); template.make(); }