A New Paradigm Shift
MiniHtml is a B4X library to create html markup. Instead of writing html using opening and closing tags, we declare tag object and chaining it's methods to generate the attributes and finally output the object as String.Below is what ChatGPT understandings of this library:
Pros and When to Use It
- Advantages:
- Type-safe or more structured way to build HTML compared to raw strings.
- Easier to maintain and refactor web-generation code in B4X.
- Integrates well with Bootstrap / CSS frameworks since you can set classes, styles, etc.
- HTMX support means you can build interactive front-ends without a heavy front-end framework.
- Ideal Scenarios:
- Building server-side web apps using B4J.
- Generating email HTML templates.
- Prototyping simple web UIs via B4X code.
- Using when you already use B4X for your project and want to stay within its ecosystem.
- Use B4J as a single IDE for both backend and frontend logic
- Use B4J server as live development server
- Avoid B4J IDE auto-capitalize some keywords inside String Literals which will break JavaScripts
- Easily use B4X to temporary comment out certain part of code
- Focus on writing in B4X mindset
- Direct access and mix the tag objects with B4X code
How to Use
- Reference the library in Libraries Manager.
- Declare a variable as Tag object.
- Call the methods such as Div.text("This is an inner text")
Create a new Tag
There are 2 ways to create a html tagMethod 1: Html module and create method
B4X:
Html.create("main") ' create <main></main>
B4X:
Form.init ' create <form></form>
Add a Child tag to a Parent tag
There are 2 ways to add a child tagLet say we have a parent tag div1 and we want to add div2 as it's child.
HTML:
<div class="p-1">
<div class="mb-2">
</div>
</div>
B4X:
Div.cls("p-1").add(Div.cls("mb-2"))
B4X:
Dim div1 As Tag = Div.cls("p-1")
Div.cls("mb-2").up(div1)
Div.cls("mb-2 text-primary").up(div1)
If we are going to use a tag multiple times, it is better to declare the parent as a tag then it's children can use the up() method to add to it. This is the recommended way.
Chaining multiple methods
- You can chained or nested multiple methods.
- However, I suggest to avoid writing a long nested line by declaring a new tag.
- Example:
It is better to write as:B4X:Div.up(col1).hxGet("/hx/pages/list").hxTrigger("load").text("Loading...")
B4X:Dim container1 As Tag = Div.up(col1) container1.hxGet("/hx/pages/list") container1.hxTrigger("load") container1.text("Loading...")
Debugging
- We can use the following methods:
- PrintMe
- PrintChildren
to print the output to the Logs. - We can also use Log
B4X:Log(div1.Build)
Return the Document
- We can use the Document class to generate a valid html document.
B4X:Dim doc As Document doc.Initialize doc.AppendDocType doc.Append(page1.Build) App.WriteHtml(Response, doc.ToString) - We use Append() to add raw text into the document.
- EndsMeet can write the text as server reponse using WriteHtml() method.
Raw html to tag
- We can use Parse() method to convert html string into tag object.
B4X:Dim s As String = $"<form action="forgot-password" method="post"> <label for="email">Email:</label> <input type="email" id="email" required> <input type="submit" value="Submit"> </form>"$ Dim form1 As Tag = Html.Parse(s) form1.up(body1)
Commonly Used Methods
- up()
Add a tag to a parent tag
B4X:Div.up(body1) - cls() - short for addClass
Add inline css class to the tag
B4X:Div.cls("row mt-3") - sty() - short for addStyle
Add inline style to the tag
B4X:Td.sty("text-align: right") - script()
Add a source of Javascript file
B4X:body1.script("$SERVER_URL$/assets/js/app.js") - attr(key, value)
Add an attribute to the list of attributes of a tag. Use this when a method is not available.
B4X:toast1.attr("role", "alert") - id()
Add an unique id to a tag usually useful as a css selector. - name()
Add a name to a tag usually for a form input. - text()
Add inner text to a tag - hxGet()
Add a hx-get attribute to make a Get request using HTMX. - Build
Convert the tag object to String.
Current version of MiniHtml integrated well with Bootstrap 5 and HTMX so you need to concern less on the responsive CSS styling and JavaScript ajax calls.
Bootstrap
B4X:
Button.cls("btn btn-primary w-100 py-2")
HTMX
B4X:
Dim form1 As Tag = Form.init
form1.hxPut($"/hx/topics"$)
form1.hxTarget("#modal-messages")
form1.hxSwap("innerHTML")
Last edited: