Class HtmlBuilder
- Namespace
- JJMasterData.Core.UI.Html
- Assembly
- JJMasterData.Core.dll
Represents a mutable string of HTML tags.
public sealed class HtmlBuilder
- Inheritance
-
HtmlBuilder
- Inherited Members
Examples
Simple way for creating HTML string with fluent method syntax of C#.
All Web Components generate a HtmlBuilder.
Single element
var builder = new HtmlBuilder(HtmlTags.Input)
.WithAttribute("type", "text")
.WithAttribute("name", "id1")
.WithAttribute("placeholder", "Enter a text");
string actualHtml = builder.ToString();
// Builder result
// <input type="text" name="id1" placeholder="Enter a text" />
Complex element
var builder = new HtmlBuilder(HtmlTag.Div)
.WithNameAndId("id1")
.WithCssClass("class1 class2")
.Append(HtmlTag.H1, h1 =>
{
h1.AppendText("Simple Title");
h1.Append(HtmlTag.Small, s =>
{
s.AppendText("This is a subtitle");
});
});
var actualHtml = builder.ToString(indentHtml: true);
// Builder result
//<div id="id1" name="id1" class="class1 class2">
// <h1>
// Simple Title
// <small>
// This is a subtitle
// </small>
// </h1>
//</div>
Constructors
HtmlBuilder()
Initializes a new instance of the HtmlBuilder class.
public HtmlBuilder()
HtmlBuilder(HtmlTag)
Initializes a new instance of the HtmlBuilder class.
public HtmlBuilder(HtmlTag tag)
Parameters
tag
HtmlTag
HtmlBuilder(string)
Initializes a new instance of the HtmlBuilder class.
public HtmlBuilder(string rawText)
Parameters
rawText
string
Methods
Append(HtmlBuilder?)
Insert a HTML builder as a child of caller builder.
public HtmlBuilder Append(HtmlBuilder? builder)
Parameters
builder
HtmlBuilder
Returns
Append(HtmlTag, Action<HtmlBuilder>?)
Appends a new HTML element with the specified tag to the current builder.
public HtmlBuilder Append(HtmlTag tag, Action<HtmlBuilder>? builderAction = null)
Parameters
tag
HtmlTagThe HTML tag representing the element to be appended.
builderAction
Action<HtmlBuilder>An optional action used to configure the child HTML builder.
Returns
- HtmlBuilder
The current HtmlBuilder instance with the new element appended.
AppendBr()
public HtmlBuilder AppendBr()
Returns
AppendComponent(HtmlComponent?)
Insert a HtmlComponent as a child of caller builder.
public HtmlBuilder AppendComponent(HtmlComponent? component)
Parameters
component
HtmlComponent
Returns
AppendDiv(Action<HtmlBuilder>?)
Appends a new <div> element to the current builder.
public HtmlBuilder AppendDiv(Action<HtmlBuilder>? builderAction = null)
Parameters
builderAction
Action<HtmlBuilder>An optional action used to configure the child HTML builder.
Returns
- HtmlBuilder
The current HtmlBuilder instance with the new <div> element appended.
AppendDiv<TState>(TState, Action<TState, HtmlBuilder>)
Appends a new <div> element to the current builder using a stateful configuration.
public HtmlBuilder AppendDiv<TState>(TState state, Action<TState, HtmlBuilder> builderAction)
Parameters
state
TStateThe external state passed to the builder action.
builderAction
Action<TState, HtmlBuilder>A static delegate that configures the child HTML builder using the provided state.
Returns
- HtmlBuilder
The current HtmlBuilder instance with the new <div> element appended.
Type Parameters
TState
The type of the external state object.
AppendHiddenInput(string)
Append a hidden input to the element tree.
public HtmlBuilder AppendHiddenInput(string name)
Parameters
name
string
Returns
AppendHiddenInput(string, string)
Append a hidden input to the element tree.
public HtmlBuilder AppendHiddenInput(string name, string value)
Parameters
Returns
AppendHr()
public HtmlBuilder AppendHr()
Returns
AppendIf(bool, HtmlTag, Action<HtmlBuilder>?)
Conditional insert HTML builder as a child of caller builder.
public HtmlBuilder AppendIf(bool condition, HtmlTag tag, Action<HtmlBuilder>? builderAction = null)
Parameters
condition
booltag
HtmlTagbuilderAction
Action<HtmlBuilder>
Returns
AppendIf(bool, Func<HtmlBuilder>)
Conditional insert a HTML builder from a return of a function. Use this to improve performance.
public HtmlBuilder AppendIf(bool condition, Func<HtmlBuilder> func)
Parameters
condition
boolfunc
Func<HtmlBuilder>
Returns
AppendInput(Action<HtmlBuilder>?)
Appends a new <input> element to the current builder.
public HtmlBuilder AppendInput(Action<HtmlBuilder>? builderAction = null)
Parameters
builderAction
Action<HtmlBuilder>An optional action used to configure the child HTML builder.
Returns
- HtmlBuilder
The current HtmlBuilder instance with the new <input> element appended.
AppendInput<TState>(TState, Action<TState, HtmlBuilder>)
Appends a new <input> element to the current builder using a stateful configuration.
public HtmlBuilder AppendInput<TState>(TState state, Action<TState, HtmlBuilder> builderAction)
Parameters
state
TStateThe external state passed to the builder action.
builderAction
Action<TState, HtmlBuilder>A static delegate that configures the child HTML builder using the provided state.
Returns
- HtmlBuilder
The current HtmlBuilder instance with the new <input> element appended.
Type Parameters
TState
The type of the external state object.
AppendLabel(Action<HtmlBuilder>?)
Appends a new <label> element to the current builder.
public HtmlBuilder AppendLabel(Action<HtmlBuilder>? builderAction = null)
Parameters
builderAction
Action<HtmlBuilder>An optional action used to configure the child HTML builder.
Returns
- HtmlBuilder
The current HtmlBuilder instance with the new <label> element appended.
AppendLabel<TState>(TState, Action<TState, HtmlBuilder>)
Appends a new <label> element to the current builder using a stateful configuration.
public HtmlBuilder AppendLabel<TState>(TState state, Action<TState, HtmlBuilder> builderAction)
Parameters
state
TStateThe external state passed to the builder action.
builderAction
Action<TState, HtmlBuilder>A static delegate that configures the child HTML builder using the provided state.
Returns
- HtmlBuilder
The current HtmlBuilder instance with the new <label> element appended.
Type Parameters
TState
The type of the external state object.
AppendLink(string, string)
public HtmlBuilder AppendLink(string text, string link)
Parameters
Returns
AppendRange(IEnumerable<HtmlBuilder>)
Insert a list of HTML builder as a child of caller builder.
public HtmlBuilder AppendRange(IEnumerable<HtmlBuilder> htmlEnumerable)
Parameters
htmlEnumerable
IEnumerable<HtmlBuilder>
Returns
AppendScript(string)
Insert a script tag with a raw JavaScript script.
public HtmlBuilder AppendScript(string rawScript)
Parameters
rawScript
string
Returns
AppendScriptIf(bool, string)
Insert a script tag with a rawScript
public HtmlBuilder AppendScriptIf(bool condition, string rawScript)
Parameters
Returns
AppendSpan(Action<HtmlBuilder>?)
Appends a new <span> element to the current builder.
public HtmlBuilder AppendSpan(Action<HtmlBuilder>? builderAction = null)
Parameters
builderAction
Action<HtmlBuilder>An optional action used to configure the child HTML builder.
Returns
- HtmlBuilder
The current HtmlBuilder instance with the new <span> element appended.
AppendSpan<TState>(TState, Action<TState, HtmlBuilder>)
Appends a new <span> element to the current builder using a stateful configuration.
public HtmlBuilder AppendSpan<TState>(TState state, Action<TState, HtmlBuilder> builderAction)
Parameters
state
TStateThe external state passed to the builder action.
builderAction
Action<TState, HtmlBuilder>A static delegate that configures the child HTML builder using the provided state.
Returns
- HtmlBuilder
The current HtmlBuilder instance with the new <span> element appended.
Type Parameters
TState
The type of the external state object.
AppendText(int)
Insert string representation of the integer as a child of caller builder.
public HtmlBuilder AppendText(int value)
Parameters
value
int
Returns
AppendText(string?)
Insert raw text as a child of caller builder.
public HtmlBuilder AppendText(string? rawText)
Parameters
rawText
string
Returns
AppendTextIf(bool, string?)
Conditional insert raw text as a child of caller builder.
public HtmlBuilder AppendTextIf(bool condition, string? rawText)
Parameters
Returns
Append<TState>(HtmlTag, TState, Action<TState, HtmlBuilder>)
Appends a new HTML element with the specified tag to the current builder, using a stateful configuration.
public HtmlBuilder Append<TState>(HtmlTag tag, TState state, Action<TState, HtmlBuilder> builderAction)
Parameters
tag
HtmlTagThe HTML tag representing the element to be appended.
state
TStateThe external state passed to the builder action.
builderAction
Action<TState, HtmlBuilder>A static delegate that configures the child HTML builder using the provided state.
Returns
- HtmlBuilder
The current HtmlBuilder instance with the new element appended.
Type Parameters
TState
The type of the external state object.
GetAttribute(string)
public string GetAttribute(string key)
Parameters
key
string
Returns
Prepend(HtmlBuilder?)
public HtmlBuilder Prepend(HtmlBuilder? builder)
Parameters
builder
HtmlBuilder
Returns
PrependComponent(HtmlComponent?)
public HtmlBuilder PrependComponent(HtmlComponent? component)
Parameters
component
HtmlComponent
Returns
ToString()
Renders the instance to a String
public override string ToString()
Returns
ToString(bool)
Gets current builder HTML.
public string ToString(bool indentHtml)
Parameters
indentHtml
boolGenerate html with indentation if true.
Returns
WithAttribute(string)
Set attribute to the HTML builder.
public HtmlBuilder WithAttribute(string nameAndValue)
Parameters
nameAndValue
string
Returns
WithAttribute(string, int)
public HtmlBuilder WithAttribute(string name, int value)
Parameters
Returns
WithAttribute(string, string)
Set attribute to the HTML builder.
public HtmlBuilder WithAttribute(string name, string value)
Parameters
Returns
WithAttributeIf(bool, string)
Set attribute to the HTML builder on condition.
public HtmlBuilder WithAttributeIf(bool condition, string nameAndValue)
Parameters
Returns
WithAttributeIf(bool, string, string)
Set attribute to the HTML builder on condition.
public HtmlBuilder WithAttributeIf(bool condition, string name, string value)
Parameters
Returns
WithAttributeIfNotEmpty(string, string?)
Set attribute to the HTML builder on condition.
public HtmlBuilder WithAttributeIfNotEmpty(string name, string? value)
Parameters
Returns
WithCssClass(string?)
Set CSS classes attributes, if already exists it will be ignored.
public HtmlBuilder WithCssClass(string? classes)
Parameters
classes
string
Returns
WithCssClassIf(bool, string?)
Conditional to set classes attributes, if already exists it will be ignored.
public HtmlBuilder WithCssClassIf(bool conditional, string? classes)
Parameters
Returns
WithDataAttribute(string, string)
Set a custom Bootstrap data attribute to HTML builder.
public HtmlBuilder WithDataAttribute(string name, string value)
Parameters
Returns
WithHref(string)
public HtmlBuilder WithHref(string link)
Parameters
link
string
Returns
WithId(string)
public HtmlBuilder WithId(string id)
Parameters
id
string
Returns
WithName(string)
public HtmlBuilder WithName(string name)
Parameters
name
string
Returns
WithNameAndId(string?)
Set the HTML builder name and id.
public HtmlBuilder WithNameAndId(string? id)
Parameters
id
string
Returns
WithOnChange(string)
public HtmlBuilder WithOnChange(string value)
Parameters
value
string
Returns
WithOnClick(string)
public HtmlBuilder WithOnClick(string value)
Parameters
value
string
Returns
WithStyle(string)
public HtmlBuilder WithStyle(string value)
Parameters
value
string
Returns
WithToolTip(string?)
Sets a tooltip to the HTML Tag
public HtmlBuilder WithToolTip(string? tooltip)
Parameters
tooltip
string
Returns
WithValue(string)
Set custom data attribute to HTML builder.
public HtmlBuilder WithValue(string value)
Parameters
value
string