Finding a parent to add more child
If we have already built a page or component tree as MiniHtml, it is difficult to trace back the multi-levels of MiniHtml tags to find the parent tag we want to add more child tag.
Method 1 - using Children
If we know the tag is the n-th child then we can insert a new MiniHtml to the
list of Children.
Let say <html> tag's first child is <head> and we want insert another tag such as comment before <head>, we can use
InsertAt.
Dim main1 As MainView 'class
main1.Initialize
main1.LoadContent(ContainerContent)
Dim page1 As MiniHtml = main1.Render
Dim commment As String = " Generated by MiniHTML3 "
Dim cmt As MiniHtml = MH.CreateTag("").text($"<!--${commment}-->"$)
'Prepend comment before head tag
page1.Children.InsertAt(0, cmt)
Method 2 - using ChildByIndex (or child)
Similar to Children, we must know the tag is
the n-th child of it's parent.
There is a possible risk this function returns null.
Dim tr1 As MiniHtml = CreateOrReadFromCache("Table Row")
tr1.child(0).text2(row.Get("id"))
tr1.child(1).text2(row.Get("name"))
Method 3 - using ChildById
If we knew the
unique id of the tag, it is easier to find the tag.
page1.ChildById("user_name").text(username)
Method 4 - using ChildByName
We can find the
first tag by name using deep search. For example, we have only 1 table in the page.
We can find the <tbody> tag to add more rows.
Dim table1 As MiniHtml = CreateOrReadFromCache("Product Table")
Dim tbody1 As MiniHtml = table1.ChildByName("tbody")
Method 5 - using ChildByClass
We can find the first tag which is
having the specified classes using deep search. Usually useful to find Bootstrap navbar, card or container.
Dim cardbody1 As MiniHtml = page1.ChildByClass("card-body")
Tips:
- It is easier to use Method 3, 4 and 5 to find a parent tag.
- However it is common the project unable to find the correct tag and returning null.
- We can test it first whether it is returning a MiniHtml object.
Otherwise, we will encounter errors and difficult to debug.
Dim nav1 As MiniHtml = page1.ChildByClass("navbar-nav")
MH.NavLinkItem("Home", "/", "bi bi-house me-2", "Home").up(nav1)
Only add a child tag if we able to find the parent.
Dim nav1 As Object = page1.ChildByClass("navbar-nav")
If Initialized(nav1) Then
MH.NavLinkItem("Home", "/", "bi bi-house me-2", "Home").up(nav1)
End If
- Use MH helper class to keep small reusable components.