Jun 16, 2023 Replies: 6 A wrap of Apache Velocity Engine library for B4A and B4J What is Velocity?Velocity is a Java-based template engine. It permits anyone to use a simple yet powerful template language to reference objects defined in Java code. Source: https://velocity.apache.org/ Download additional libraries: velocity-engine-core-2.3.jar commons-lang3-3.12.0.jar slf4j-api-2.0.7.jar (required if not using jServer) slf4j-simple-2.0.7.jar (required if not using jServer) Based on this original code snippet: Velocity Template Language (VTL) Change Logs: v1.01 - Changed to b4xlib. Modified Initialize method. v1.02 - Added evaluate method. Added example project. v1.03 - Renamed Text method to ToString. Removed redundant references for slf4j libraries in manifest.txt which already existed in jServer. Velocity Author: aeric, Daestrum Version: 1.03 Methods: Initialize (Directory As String) Initializes the object. You can pass a default directory inside Objects directory put (Key As Object, Value As Object) Put Context with Key Value pair putMap (m As Map) Put Context by passing a Map merge Merge Template with Context and pass to StringWriter mergeTemplate (TemplateFile As String, Encoding As String) Merge a Template with Context and pass to StringWriter evaluate (Tag As String, TemplateString As String) Evaluate TemplateString Properties: get (Key As String) As Object [read only] Get the Context by Key Template As String [write only] Load a Template file ToString As Object [read only] Return StringWriter as String (generated from http://b4a.martinpearman.co.uk/xml2bb/ and edited) Attachments Velocity.b4xlib Velocity.b4xlib 1.2 KB · Views: 583 VelocityExample.zip VelocityExample.zip 14.8 KB · Views: 596 Last edited: Jul 18, 2023 Click to expand...
A wrap of Apache Velocity Engine library for B4A and B4J What is Velocity?Velocity is a Java-based template engine. It permits anyone to use a simple yet powerful template language to reference objects defined in Java code. Source: https://velocity.apache.org/ Download additional libraries: velocity-engine-core-2.3.jar commons-lang3-3.12.0.jar slf4j-api-2.0.7.jar (required if not using jServer) slf4j-simple-2.0.7.jar (required if not using jServer) Based on this original code snippet: Velocity Template Language (VTL) Change Logs: v1.01 - Changed to b4xlib. Modified Initialize method. v1.02 - Added evaluate method. Added example project. v1.03 - Renamed Text method to ToString. Removed redundant references for slf4j libraries in manifest.txt which already existed in jServer. Velocity Author: aeric, Daestrum Version: 1.03 Methods: Initialize (Directory As String) Initializes the object. You can pass a default directory inside Objects directory put (Key As Object, Value As Object) Put Context with Key Value pair putMap (m As Map) Put Context by passing a Map merge Merge Template with Context and pass to StringWriter mergeTemplate (TemplateFile As String, Encoding As String) Merge a Template with Context and pass to StringWriter evaluate (Tag As String, TemplateString As String) Evaluate TemplateString Properties: get (Key As String) As Object [read only] Get the Context by Key Template As String [write only] Load a Template file ToString As Object [read only] Return StringWriter as String (generated from http://b4a.martinpearman.co.uk/xml2bb/ and edited)
aeric Expert Licensed User Longtime User Jun 21, 2023 #2 update: version 1.02 - added evaluate method. - example project attached.
aeric Expert Licensed User Longtime User Jun 21, 2023 #3 Tips: You can load more than one template files. B4X: Copy Velocity.mergeTemplate("mainLayout.vm", "UTF-8") Velocity.mergeTemplate("index.vm", "UTF-8") You can use the $!bodyContent variable. mainLayout.vm: HTML: Copy #macro(mainLayout) <head> ... </head> <body> $!bodyContent </body> #end index.vm: HTML: Copy #@mainLayout() <h1>Index page</h1> #end Apache Velocity "main template"? I'm very new to Apache Velocity, and I'm having a little trouble figuring out the optimal way to structure my templates. In most of the guides I have seen the pages have been built like this: #pa... stackoverflow.com
Tips: You can load more than one template files. B4X: Copy Velocity.mergeTemplate("mainLayout.vm", "UTF-8") Velocity.mergeTemplate("index.vm", "UTF-8") You can use the $!bodyContent variable. mainLayout.vm: HTML: Copy #macro(mainLayout) <head> ... </head> <body> $!bodyContent </body> #end index.vm: HTML: Copy #@mainLayout() <h1>Index page</h1> #end Apache Velocity "main template"? I'm very new to Apache Velocity, and I'm having a little trouble figuring out the optimal way to structure my templates. In most of the guides I have seen the pages have been built like this: #pa... stackoverflow.com
aeric Expert Licensed User Longtime User Jul 18, 2023 #4 Update v1.03 - Renamed Text property to ToString. - Removed redundant references for slf4j libraries in manifest.txt which already existed in jServer. If not building jServer app, add the following to the Main module: B4X: Copy #AdditionalJar: slf4j-api-2.0.7 #AdditionalJar: slf4j-simple-2.0.7 Check the updated example in post #1. Last edited: Jul 18, 2023
Update v1.03 - Renamed Text property to ToString. - Removed redundant references for slf4j libraries in manifest.txt which already existed in jServer. If not building jServer app, add the following to the Main module: B4X: Copy #AdditionalJar: slf4j-api-2.0.7 #AdditionalJar: slf4j-simple-2.0.7 Check the updated example in post #1.
byz Active Member Licensed User Nov 9, 2024 #5 Is it possible to set it to double curly braces to parse the template. For example: {{myname}}
aeric Expert Licensed User Longtime User Nov 10, 2024 #6 byz said: Is it possible to set it to double curly braces to parse the template. For example: {{myname}} Click to expand... Not sure, I think it is possible. Or you may want to consider other template engine like FreeMarker, Jinjava or Thymeleaf.
byz said: Is it possible to set it to double curly braces to parse the template. For example: {{myname}} Click to expand... Not sure, I think it is possible. Or you may want to consider other template engine like FreeMarker, Jinjava or Thymeleaf.
aeric Expert Licensed User Longtime User Feb 18, 2025 #7 byz said: Is it possible to set it to double curly braces to parse the template. For example: {{myname}} Click to expand... deepseek's answer: B4X: Copy String template = "Hello, {{myname}}!"; template = template.replace("{{", "$").replace("}}", "");
byz said: Is it possible to set it to double curly braces to parse the template. For example: {{myname}} Click to expand... deepseek's answer: B4X: Copy String template = "Hello, {{myname}}!"; template = template.replace("{{", "$").replace("}}", "");