@Test public void testProcessTemplateContext() throws Exception { final CacheManager cacheManager = context.mock(CacheManager.class); final Cache cache = context.mock(Cache.class); context.checking(new Expectations() {{ allowing(cacheManager).getCache("contentService-templateSupport"); will(returnValue(cache)); allowing(cache).get("<% \n %>${context.name} is awesome!"); will(returnValue(null)); allowing(cache).put(with(equal("<% \n %>${context.name} is awesome!")), with(any(Object.class))); allowing(cache).get(any(String.class)); will(returnValue(null)); }}); final TemplateSupport templates = new GroovySimpleTemplateSupportImpl(cacheManager); final TemplateProcessorGroovyImpl support = new TemplateProcessorGroovyImpl(templates); final String out = support.processTemplate("${context.name} is awesome!", "en", new HashMap<String, Object>() {{ put("name", "YC"); }}); assertEquals("YC is awesome!", out); context.assertIsSatisfied(); }
@Override public String processTemplate(final String template, final String locale, final Map<String, Object> context) { final StringBuilder templateWithFuncs = new StringBuilder(); appendCustomFunctions(templateWithFuncs); templateWithFuncs.append(template); final Map<String, Object> fullContext = new HashMap<>(context); fullContext.put("locale", locale); fullContext.put("localeObject", lazyLoad(locale)); fullContext.putAll(functionsCtx); fullContext.put("context", fullContext); return this.templateSupport.get(templateWithFuncs.toString()).make(fullContext); }
TemplateProcessorGroovyImpl implements TemplateProcessor { @Override public String processTemplate(final String template, final String locale, final Map<String, Object> context) { final StringBuilder templateWithFuncs = new StringBuilder(); appendCustomFunctions(templateWithFuncs); templateWithFuncs.append(template); final Map<String, Object> fullContext = new HashMap<>(context); fullContext.put("locale", locale); fullContext.put("localeObject", lazyLoad(locale)); fullContext.putAll(functionsCtx); fullContext.put("context", fullContext); return this.templateSupport.get(templateWithFuncs.toString()).make(fullContext); } }
TemplateProcessorGroovyImpl implements TemplateProcessor { @Override public String processTemplate(final String template, final String locale, final Map<String, Object> context) { final StringBuilder templateWithFuncs = new StringBuilder(); appendCustomFunctions(templateWithFuncs); templateWithFuncs.append(template); final Map<String, Object> fullContext = new HashMap<>(context); fullContext.put("locale", locale); fullContext.put("localeObject", lazyLoad(locale)); fullContext.putAll(functionsCtx); fullContext.put("context", fullContext); return this.templateSupport.get(templateWithFuncs.toString()).make(fullContext); } TemplateProcessorGroovyImpl(final TemplateSupport templateSupport); }
TemplateProcessorGroovyImpl implements TemplateProcessor { @Override public String processTemplate(final String template, final String locale, final Map<String, Object> context) { final StringBuilder templateWithFuncs = new StringBuilder(); appendCustomFunctions(templateWithFuncs); templateWithFuncs.append(template); final Map<String, Object> fullContext = new HashMap<>(context); fullContext.put("locale", locale); fullContext.put("localeObject", lazyLoad(locale)); fullContext.putAll(functionsCtx); fullContext.put("context", fullContext); return this.templateSupport.get(templateWithFuncs.toString()).make(fullContext); } TemplateProcessorGroovyImpl(final TemplateSupport templateSupport); @Override boolean supports(final String template, final String locale, final Map<String, Object> context); @Override String processTemplate(final String template, final String locale, final Map<String, Object> context); @Override void registerFunction(final String name, final FunctionProvider functionProvider); }
TemplateProcessorGroovyImpl implements TemplateProcessor { @Override public String processTemplate(final String template, final String locale, final Map<String, Object> context) { final StringBuilder templateWithFuncs = new StringBuilder(); appendCustomFunctions(templateWithFuncs); templateWithFuncs.append(template); final Map<String, Object> fullContext = new HashMap<>(context); fullContext.put("locale", locale); fullContext.put("localeObject", lazyLoad(locale)); fullContext.putAll(functionsCtx); fullContext.put("context", fullContext); return this.templateSupport.get(templateWithFuncs.toString()).make(fullContext); } TemplateProcessorGroovyImpl(final TemplateSupport templateSupport); @Override boolean supports(final String template, final String locale, final Map<String, Object> context); @Override String processTemplate(final String template, final String locale, final Map<String, Object> context); @Override void registerFunction(final String name, final FunctionProvider functionProvider);  }