Share My Creation [WebApp] A Simple Html Template Parser

This is a mini template engine which can generate HTML dynamically by parsing templates on server-side. It is a bit like Django.

1. Templates
A template is a text file which contains variables, tags.

1.1 Variable
There are two types of template variables, one is common variable, the other is conditional variable.
TypeDecriptionUsage
CommonVariable name is put inside {{ }}<h1>Hello {{ firstname }}, how are you?</h1>
Conditional Variable name is put inside tags {%if isLogin %} or {% for menu in menus %}

1.2 Tags
Tags control the logic of the template. they are surrounded in {% %}. Some create text in the output, some control flow by performing loops or logic, and some load external information into the template to be used by later variables.
TagDecriptionUsage
blockDefine a section in a master template that should be replaced by a section in a child template{% block content %}{% endblock %}
if
[ else | elseif ]
endif
An if statement evaluates a boolean variable and executes a block of code if the value is true. {%if isLogin %}
<li class="nav-item"><a href="/logout" class="nav-link">LOGOUT</a></li>
{% else %}
<li class="nav-item"><a href="/login" class="nav-link">LOGIN</a></li>
{% endif %}
for inA for loop is used for iterating over a list. {% for menu in menus %}
<li class="nav-item"><a href={{menu.href}} class="nav-link {{ menu.active }}">{{menu.item}}</a></li>
{% endfor %}
extendsThe extends tag allows you to add a parent template for the current template.This means that you can have one master page that acts like a parent for all other pages{%extends base %}
{% block content %}
<h2>Page-{{uri}} {{username}}
</h2>{% endblock %}
includeThe include tag allows you to include a template inside the current template. This is useful when you have a block of content that is the same for many pages.{% include partial/header %}
body is content what you would like to do
{% include partial/footer %}

2. Use templates in B4J
The template parsing library b4jtpl.0.11.0 is attached.

2.1 Pass variables to templates
In B4J, template variables will are put into a map,each key of the map corresponds to a template variable name

2.2 initialize
HtmlTplParser is a class which generates HTML.
To use the class you need to set templatefile's basepath and extension

2.3 Render
A method render which returns html string by template file and map variale

For a hands-on example of creating HTML pages with templates, please see stplExample.

It's only a creation or a practice not a product, I did it just for learning b4x .
 

Attachments

  • stplExample.zip
    273.2 KB · Views: 157
  • b4jtpl.0.11.0.zip
    12.6 KB · Views: 143
  • 3.png
    3.png
    446 KB · Views: 1,784
  • 2.png
    2.png
    36.1 KB · Views: 189
  • 1.png
    1.png
    377.3 KB · Views: 186
Last edited:

teddybear

Well-Known Member
Licensed User
Hello i try your lib but there is missing lib called b4xencryption.jar, where to find, thanks
Here is the link, It is an internal library from B4A, you can copy it to Additional folder of B4J
 
Last edited:

Inrenbang

Member
Licensed User
Here is the link, It is an internal library from B4A, you can copy it to Additional folder of B4J
thanks its working now, but now different error when i called localhost :

error:
Waiting for debugger to connect...
Program started.
2023-06-06 15:33:14.259:INFO :oejs.Server:main: jetty-11.0.9; built: 2022-03-30T17:44:47.085Z; git: 243a48a658a183130a8c8de353178d154ca04f04; jvm 11.0.1+13
2023-06-06 15:33:14.397:INFO :oejss.DefaultSessionIdManager:main: Session workerName=node0
2023-06-06 15:33:14.426:INFO :oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@58d75e99{/,file:///C:/Users/epnee/Desktop/stplExample/Objects/www/,AVAILABLE}
2023-06-06 15:33:14.461:INFO :oejs.RequestLogWriter:main: Opened C:\Users\epnee\Desktop\stplExample\Objects\logs\b4j-2023_06_06.request.log
2023-06-06 15:33:14.791:INFO :oejs.AbstractConnector:main: Started ServerConnector@35083305{HTTP/1.1, (http/1.1)}{0.0.0.0:80}
2023-06-06 15:33:14.811:INFO :oejs.Server:main: Started Server@dbf57b3{STARTING}[11.0.9,sto=0] @1429ms
Emulated network latency: 100ms
Server is started. port:80
home
Error occurred on line: 37 (tpl)
java.lang.NoClassDefFoundError: anywheresoftware/b4a/object/B4XEncryption
    at xh.htmltplparser.htmltplparser._vv7(htmltplparser.java:226)
    at xh.htmltplparser.htmltplparser._vvv0(htmltplparser.java:543)
    at xh.htmltplparser.htmltplparser._vvv4(htmltplparser.java:358)
    at b4j.example.tpl._handle(tpl.java:119)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:146)
    at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
    at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
    at anywheresoftware.b4a.shell.ShellBA.startMessageLoop(ShellBA.java:121)
    at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:180)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at b4j.example.main.main(main.java:29)
Caused by: java.lang.ClassNotFoundException: anywheresoftware.b4a.object.B4XEncryption
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 33 more
 

teddybear

Well-Known Member
Licensed User
How did you install the B4XEncryption library?
 

Inrenbang

Member
Licensed User
How did you install the B4XEncryption library?
first i download from the link you gave, and compile it to additional library, error occured, second i copy from internal library jb4xencryption and rename it to b4xencryption the same error occured
 

teddybear

Well-Known Member
Licensed User
first i download from the link you gave, and compile it to additional library, error occured, second i copy from internal library jb4xencryption and rename it to b4xencryption the same error occured
It is B4XEncryption library not jb4xencryption
 

teddybear

Well-Known Member
Licensed User

teddybear

Well-Known Member
Licensed User
no B4XEncryption.jar in my internal library it is only jB4XEncryption.jar, my B4j version is 9.80 which is latest

still the same error sir, can you confirm to test it successfully?
IDE B4A not B4J, as I said #post3 It is from B4A
 

teddybear

Well-Known Member
Licensed User
There are a lot of bugs in it. It's only a creation or a practice not a product, I did it just for learning b4x .
 
Top