No-template

From unkrig.de
(Difference between revisions)
Jump to: navigation, search
m (Solution)
(Replaced content with "A super-small Java library for templating, i.e. generating text files (HTML, XML, whatever) from a "template" text file. The project home page is [https://github.com/aunk...")
 
Line 1: Line 1:
 
A super-small Java library for templating, i.e. generating text files (HTML, XML, whatever) from a "template" text file.
 
A super-small Java library for templating, i.e. generating text files (HTML, XML, whatever) from a "template" text file.
  
== Background ==
+
The project home page is [https://github.com/aunkrig/no-template here].
 
+
Working with the common templating frameworks (Freemarker, JSP, PHP, ...), you probably agree on the following inconveniences and problems:
+
 
+
* Each framework has its own syntax and semantics for implementing the dynamic part of a document (variables, control structures, in-source documentation, ...).
+
* Because the nesting of control structures and the indentation of the static content are generally not identical, it is very hard to write easily-readable code.
+
* Most templating frameworks have no static typing, i.e. you must always be careful when using (or worse: re-using) variables, because there is no "compile-time checking" of types.
+
* Powerful debuggers are not available in many cases, so you typcially revert to looking at log files.
+
* Because there are so many different templating engines, you favorite IDE possibly offers no advanced editor with code completion, refactoring, and the like.
+
* If the templating engine involves some kind of an "translation" step, then that must be executed on each and every code change - at worst manually.
+
 
+
== Solution ==
+
 
+
The approach of <code>no-template</code> is as simple as it could be: Write the templates in plain Java! Static context maps to string literals; variables, control structures and in-source documentation map to the respective Java constructs.
+
 
+
The no-template framework basically consists of a single method
+
 
+
public static final <T extends NoTemplate, EX extends Exception> void
+
NoTemplate.render(final Class<T> templateClass, File outputFile, final ConsumerWhichThrows<? super T, EX> renderer);
+
 
+
That method creates the ''outputFile'', instantiates the ''templateClass'', and eventually calls the ''renderer''. The latter typically generates the output by calling the <code>l(String...)</code> and <code>p(String)</code> methods.
+
 
+
Done! That's why it's called "no-template": Next to no footprint...
+
 
+
== Code Example ==
+
 
+
<nowiki> package com.acme.notemplatedemo;
+
+
import de.unkrig.notemplate.*;
+
+
public
+
class MyTemplate extends NoTemplate {
+
+
    // It is good practice to declare a method "render()" with
+
    // exactly the parameters of the document to be rendered.
+
+
    public void
+
    render(String firstName, String lastName, int age) {
+
+
        // Method "l(String...)" prints its arguments as lines.
+
        // Notice that we put the arguments on column one,
+
        // so the indentation of the document is preserved.
+
        this.l(
+
"<!DOCTYPE html>",
+
"<html>",
+
"  <head>",
+
"    <title>My first template</title>",
+
"  </head>",
+
"  <body>",
+
"    This is my very first template.",
+
"    &lt;br />",
+
"    Hello " + firstName + " " + lastName + ",",
+
"    you must be " + age + " years old!",
+
"  </body>",
+
"</html>"
+
        );
+
    }
+
}</nowiki>
+
 
+
// Demonstrate how to render a document with this template:
+
NoTemplate.render(
+
    MyTemplate.class,
+
    new File("JohnDoe99.html"),
+
    (MyTemplate t) -> {
+
        t.render("John", "Doe", 99);
+
    }
+
);
+
 
+
== Resources ==
+
 
+
Find the binaries [http://no-template.unkrig.de/download/ here].
+
 
+
Find the source code [https://github.com/aunkrig/no-template here].
+
 
+
Find the JAVADOC [http://no-template.unkrig.de/javadoc/index.html here].
+
 
+
== Licensing ==
+
 
+
No-template is distributed under the [http://no-template.unkrig.de/new_bsd_license.txt New BSD license].
+

Latest revision as of 13:13, 3 June 2016

A super-small Java library for templating, i.e. generating text files (HTML, XML, whatever) from a "template" text file.

The project home page is here.

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox