public interface HtmlBuilder
An html builder interface/class (e.g. Html4
)
contains piece types (e.g. Html4.DIV
)
and builder methods (e.g. Html4._div()
).
Application code usually accesses them through inheritance,
for example,
class MyDoc extends Html4Doc//which implements Html4 {{ _body( ()-> { DIV div = _div(); }); }}
A piece type (e.g. Html4.DIV
) describes a specific type of html pieces.
It contains state changing methods (e.g. to add attributes/children).
A builder method (e.g. Html4._div()
)
creates a piece and add it to the context parent
.
A builder method should be an instance method.
Its name should start with an underscore (to visually distinguish it from "normal" methods).
This interface (HtmlBuilder) contains the class ContextParent
,
and some basic builder methods like _text(Object...)
.
Modifier and Type | Interface and Description |
---|---|
static class |
HtmlBuilder.ContextParent
A utility class dealing with context parents.
|
Default Methods | |
---|---|
HtmlText |
_text(Object... args)
Add textual content.
|
HtmlText |
_textf(String format,
Object... args)
Add formatted textual content.
|
HtmlComment |
_comment(Object... args)
Add an html comment.
|
HtmlRaw |
_raw(Object... args)
Add raw content.
|
HtmlNewLine |
_newline()
Add a new line ("\n").
|
default HtmlText _text(Object... args)
This method is equivalent to `ContextParent.add(new HtmlText(args))`.
Example:
_text("there are ", 5, " apples.");
default HtmlText _textf(String format, Object... args)
This method is equivalent to `_text( String.format(format, args) )`.
See String.format(format, args)
.
Example:
_textf("there are %d apples.", 5);
default HtmlComment _comment(Object... args)
This method is equivalent to `ContextParent.add(new HtmlComment(args))`.
Example:
_comment("begin article #", 5); // <!-- begin article #5 -->
default HtmlRaw _raw(Object... args)
This method is equivalent to `ContextParent.add(new HtmlRaw(args))`.
Example:
_raw("<span>abc</span>");
Raw content is dangerous, since no escaping is done. If it contains strings from end user inputs, make sure they are sanitized or escaped first.
default HtmlNewLine _newline()
This method is equivalent to `ContextParent.add(HtmlNewLine.instance)`.
A new line can be inserted between two inline elements for formatting purpose (if it's ok to insert spaces between them). For example
_div( _input(), _newline(), _input() ) // <div> // <input> // <input> // </div>
Note that a new line corresponds to a "\n", not a "<br>".
This method is usually not needed before/after a block element.