sviperll / static-mustache

Template engine for java with statically checked and compiled templates. Compilation is performed alone with java sources.
BSD 3-Clause "New" or "Revised" License
28 stars 4 forks source link

Add support for annotating interface methods #10

Open sviperll opened 5 years ago

sviperll commented 5 years ago

Now we have the following code structure:

@GenerateRenderableAdapter(
    template = "user.mustache",
    adapterName = "RenderableHtmlUserAdapter"
)
public class User {
    // ...
}
class Main {
    public static void main(String[] args) throws IOException {
        User user = new User("John Doe", 21, new String[] {"Knowns nothing"}, list);
        Renderable<Html> renderable = new RenderableHtmlUserAdapter(user);

        // Any appendable will do: StringBuilder, Writer, OutputStream
        Renderer renderer = renderable.createRenderer(System.out);

        // Write rendered template
        renderer.render();
    }
}

It's sometimes better to centralize template references in one place like this:

// No annotations, just plain old Java-object
class User {
    // ...
}
// No annotations, just plain old Java-object
class Order {
    // ...
}
@GenerateRenderablesFacade(className = "DefaultMyRenderables")
interface MyRenderables {
    @RenderableFactoryMethod(tamplate = "com/github/sviperll/mysuperall/user.mustache")
    Renderable<Html> renderableUser(User user);

    @RenderableFactoryMethod(tamplate = "com/github/sviperll/mysuperall/order.mustache")
    Renderable<Html> renderableOrder(Order user);
}
class Main {
    public static void main(String[] args) throws IOException {
        User user = new User("John Doe", 21, new String[] {"Knowns nothing"}, list);
        Order order = new Order(BigDecimal.valueOf(145), Money.USD);
        MyRenderables renderables = new DefaultMyRenderables();
        Renderable<Html> renderableUser = renderables.renderableUser(user);
        Renderable<Html> renderableOrder = renderables.renderableOrder(order);
        // ...
    }
}