velocity-engine icon indicating copy to clipboard operation
velocity-engine copied to clipboard

refactor the velocity with a more scalable api and spi interfaces

Open xiaoma20082008 opened this issue 3 years ago • 3 comments

I want to refactor the velocity with a more scalable API (for Users) and SPI (for Developers), but I don't know whether it's needed, So I created this pr for ensure it (Please tell me if you need otherwise please close this pr, Thank you! ).

The target is:

  1. compile the template(.vm) to a java class(.java), also provide a interpreter like now.
  2. the template#render will compile the generated java code to a java class and execute it.

The sample usage For User:

Template template = VelocityEngine.getTemplate(name, locale, encoding);
template.render(context, writer);

Sample Implementation is:

Template getTemplate(String name, Locale locale, String encoding) {
  // 0. cache
  Resource r = cache.get(name);
  // 1. load (the loader maybe is ClasspathLoader, FileLoader, JarLoader... )
  r = loader.load(name, locale, encoding);
  // 2. read a .vm file to string with encoding
  String source = r.getSource();
  // 3. convert xxs characters
  source = converter.convert(name, source);
  // 4. compile
  Class<?> templateClass = compiler.compile(source); // maybe a CompiledTemplate or a InterpretedTemplate
  // 5. create template instance
  return templateClass.getConstructor().newInstance();
}

xiaoma20082008 avatar Sep 14 '21 09:09 xiaoma20082008

Nice idea, but we won't introduce another logging abstraction. We have replaced log chutes with SLF4J for very good reasons. Drop that part please.

Got it, Thank you!

xiaoma20082008 avatar Sep 15 '21 03:09 xiaoma20082008

This api looks alot like handlebars. this is pretty slick.

AnthonyJClink avatar Sep 16 '21 19:09 AnthonyJClink