Coder Social home page Coder Social logo

mallgroda's Introduction

MALLGRODA

Type-safe server-side templating

The basic idea is that... the Mallgroda annotation processor finds classes provided by you, e.g.

@WidgetModel(
    template = "path/to/hello.html",
    generatee = "org.example.HelloWidget"
)
public class HelloWidgetModel {        
    private String greetee;

    public String greetee() {
        return greetee;
    }

    public HelloWidgetModel(String greetee) {
        this.greetee = greetee;
    }
}   

...and looks up the template html given in the annotation template parameter, for instance ...

<html>
  <head>
    <title>
        Hello
        <span 
          tal:replace="greetee()">Earth</span>
    </title>
  </head>
  <body>           
    Hello, 
    <span tal:content="greetee()">Earth
    </span>!
  </body>
</html>

... and generates, still at compile-time, a renderer widget class ...

public class HelloWidget 
  implements Widget<HelloWidgetModel> {
    public void render(
        HelloWidgetModel model, 
        Writer w) {
      w.write("<html>\n");
      w.write("  <head>\n");
      w.write("    <title>\n");
      w.write("Hello ");
      w.write(model.greeter());
      w.write("    </title>");
      w.write("  </head>\n");
      w.write("  <body>\n");
      w.write("    Hello, ");
      w.write("    <span>");          
      w.write(model.greeter());
      w.write("</span>\n");
      w.write("  </body>\n");
      w.write("</html>");
    }
}

... which will, at runtime, when given an instance of its model class, such as ...

HelloWidgetModel model = new 
  HelloWidgetModel("World");       
new HelloWidget().render(model, someWriterProllyAWebResponse);     

... render a templated fragment ...

<html>
  <head>
    <title>
        Hello World
    </title>
  </head>
  <body>           
    Hello, <span>World</span>
  </body>
</html>

The plan is to start with support for a TAL-like syntax for HTML but build to enable other templating syntaxen, perhaps mustache, and other output formats, perhaps json or sql.

Currently, this is not quite there yet. Check back later

mallgroda's People

Contributors

jensdpersson avatar

Watchers

James Cloos avatar  avatar

mallgroda's Issues

Implement 'include' directive

'Include' should pull a subwidget into the widget. For reusable components.

The include directive should take as value something to locate the correct model method in the current widget model as well as the template for that widget.

The same widget should be includable several times in a widget.
If the widget model needs parameters the value for the directive needs to indicate them.

Suppose there is a subwidget for a list row. The current widget could repeat over a model method each returning a complete model for the row.

Suppose a widget should be included several times with some variations. The current widget could provide one method each inclusion. But perhaps it would be nicer if the model could take parameters.
Perhaps the value should be like an invocation of the model method with parameters, then the submodel class indicates its generated renderer class, as it too needs to be annotated as a WidgetModel. The generated main widget renderer can then get hold of a model from the expression in the directive value, then get a renderer class from the anno on the model class, and output code that invokes that subrenderer, passing the model.

An include directive could then look like, for a hypothetical info message widget renderable at top or bottom:

However, starting with an include directive that does not support parameters might be better, it saves a lot of complexity and gets most of the value.

Another thing, do we replace the element on which include is defined or embed? To keep a previewable template page close to the rendered result, do we check that the element is the same? How about non-html includes? Hmm. Prolly best to keep the tag around, and add the included stuff as content.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.