- As application architectures progress, it is important to support application developer roles. It used to be ok to have a designer learn a templating engine and force them to use a heavy J2EE servers and tools. As the front end engineer roles continue to become prominent we need to be creative and support ways they can work with the right tools.
- Being able to render a template on the server or client side gives you flexibility. It allows you to create a site completely disconnected from the server by using mock data. This is helpful when you are trying to put together prototypes or ship off production demos.
- It allows roles to be working in parallel. While a front end developer is working on creating a production ready front end, a middle tier developer can be working on assembling or piecing together data. An important piece in this model is that roles must work together on defining a data contract.
Create Nashhorn Script Engine
ScriptEngineManager.getEngineByName we will gain reference to the nashorn
ScriptEngine. Next we will load dustjs library by using a FileReader to pass the source of the script to be executed.
Once we load the dustjs library we need gain reference to the dust engine by calling the
Load or register template to dust context
Immediately following the compile process we will register the compiled template with the dust engine.
There is a four steps to render or mesh data with the template.
Define data to pass to the template
Bind objects to js engine
Create a Bindings object and put elements that should be bound to nashorn.
At first I defined the render script below, notice the line
writer.write( data ).
StringWriter has a few overloaded methods which I assumed by default it would call
write( string ). When running I received the error below which trigger the change to
write(char cbuf, int off, int len)