Table of Contents

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

HtmlBuilder

Append(HtmlTag, Action<HtmlBuilder>?)

Insert a HTML builder as a child of caller builder.

public HtmlBuilder Append(HtmlTag tag, Action<HtmlBuilder>? builderAction = null)

Parameters

tag HtmlTag
builderAction Action<HtmlBuilder>

Returns

HtmlBuilder

AppendAsync(HtmlTag, Func<HtmlBuilder, Task>)

public Task<HtmlBuilder> AppendAsync(HtmlTag tag, Func<HtmlBuilder, Task> builderFunc)

Parameters

tag HtmlTag
builderFunc Func<HtmlBuilder, Task>

Returns

Task<HtmlBuilder>

AppendAsync(Func<Task<HtmlBuilder>>)

public Task<HtmlBuilder> AppendAsync(Func<Task<HtmlBuilder>> builderFunc)

Parameters

builderFunc Func<Task<HtmlBuilder>>

Returns

Task<HtmlBuilder>

AppendBr()

public HtmlBuilder AppendBr()

Returns

HtmlBuilder

AppendComponent(HtmlComponent?)

Insert a HTMLComponent as a child of caller builder.

public HtmlBuilder AppendComponent(HtmlComponent? component)

Parameters

component HtmlComponent

Returns

HtmlBuilder

AppendComponentIf(bool, Func<HtmlComponent?>)

public HtmlBuilder AppendComponentIf(bool condition, Func<HtmlComponent?> componentFunc)

Parameters

condition bool
componentFunc Func<HtmlComponent>

Returns

HtmlBuilder

AppendControlAsync(ControlBase?)

public ValueTask<HtmlBuilder> AppendControlAsync(ControlBase? control)

Parameters

control ControlBase

Returns

ValueTask<HtmlBuilder>

AppendDiv(Action<HtmlBuilder>?)

Insert a HTML div as a child of caller builder.

public HtmlBuilder AppendDiv(Action<HtmlBuilder>? builderAction = null)

Parameters

builderAction Action<HtmlBuilder>

Returns

HtmlBuilder

AppendHiddenInput(string)

Append a hidden input to the element tree.

public HtmlBuilder AppendHiddenInput(string name)

Parameters

name string

Returns

HtmlBuilder

AppendHiddenInput(string, string)

Append a hidden input to the element tree.

public HtmlBuilder AppendHiddenInput(string name, string value)

Parameters

name string
value string

Returns

HtmlBuilder

AppendHr()

public HtmlBuilder AppendHr()

Returns

HtmlBuilder

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 bool
tag HtmlTag
builderAction Action<HtmlBuilder>

Returns

HtmlBuilder

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 bool
func Func<HtmlBuilder>

Returns

HtmlBuilder

AppendIfAsync(bool, Func<Task<HtmlBuilder>>)

public Task AppendIfAsync(bool condition, Func<Task<HtmlBuilder>> func)

Parameters

condition bool
func Func<Task<HtmlBuilder>>

Returns

Task

AppendInput(Action<HtmlBuilder>?)

public HtmlBuilder AppendInput(Action<HtmlBuilder>? builderAction = null)

Parameters

builderAction Action<HtmlBuilder>

Returns

HtmlBuilder

AppendLabel(Action<HtmlBuilder>?)

public HtmlBuilder AppendLabel(Action<HtmlBuilder>? builderAction = null)

Parameters

builderAction Action<HtmlBuilder>

Returns

HtmlBuilder
public HtmlBuilder AppendLink(string text, string link)

Parameters

text string
link string

Returns

HtmlBuilder

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

HtmlBuilder

AppendScript(string)

Insert a script tag with a raw JavaScript script.

public HtmlBuilder AppendScript(string rawScript)

Parameters

rawScript string

Returns

HtmlBuilder

AppendScriptIf(bool, string)

Insert a script tag with a rawScript

public HtmlBuilder AppendScriptIf(bool condition, string rawScript)

Parameters

condition bool
rawScript string

Returns

HtmlBuilder

AppendSpan(Action<HtmlBuilder>?)

public HtmlBuilder AppendSpan(Action<HtmlBuilder>? builderAction = null)

Parameters

builderAction Action<HtmlBuilder>

Returns

HtmlBuilder

AppendText(int)

Insert string representation of the integer as a child of caller builder.

public HtmlBuilder AppendText(int value)

Parameters

value int

Returns

HtmlBuilder

AppendText(string?)

Insert raw text as a child of caller builder.

public HtmlBuilder AppendText(string? rawText)

Parameters

rawText string

Returns

HtmlBuilder

AppendTextIf(bool, string)

Conditional insert raw text as a child of caller builder.

public HtmlBuilder AppendTextIf(bool condition, string rawText)

Parameters

condition bool
rawText string

Returns

HtmlBuilder

GetAttribute(string)

public string GetAttribute(string key)

Parameters

key string

Returns

string

Prepend(HtmlBuilder?)

public HtmlBuilder Prepend(HtmlBuilder? builder)

Parameters

builder HtmlBuilder

Returns

HtmlBuilder

PrependComponent(HtmlComponent?)

public HtmlBuilder PrependComponent(HtmlComponent? component)

Parameters

component HtmlComponent

Returns

HtmlBuilder

ToString()

Renders the instance to a String

public override string ToString()

Returns

string

ToString(bool)

Gets current builder HTML.

public string ToString(bool indentHtml)

Parameters

indentHtml bool

Generate html with indentation if true.

Returns

string

WithAttribute(string)

Set attribute to the HTML builder.

public HtmlBuilder WithAttribute(string nameAndValue)

Parameters

nameAndValue string

Returns

HtmlBuilder

WithAttribute(string, int)

public HtmlBuilder WithAttribute(string name, int value)

Parameters

name string
value int

Returns

HtmlBuilder

WithAttribute(string, string)

Set attribute to the HTML builder.

public HtmlBuilder WithAttribute(string name, string value)

Parameters

name string
value string

Returns

HtmlBuilder

WithAttributeIf(bool, string)

Set attribute to the HTML builder on condition.

public HtmlBuilder WithAttributeIf(bool condition, string nameAndValue)

Parameters

condition bool
nameAndValue string

Returns

HtmlBuilder

WithAttributeIf(bool, string, string)

Set attribute to the HTML builder on condition.

public HtmlBuilder WithAttributeIf(bool condition, string name, string value)

Parameters

condition bool
name string
value string

Returns

HtmlBuilder

WithAttributeIfNotEmpty(string, string?)

Set attribute to the HTML builder on condition.

public HtmlBuilder WithAttributeIfNotEmpty(string name, string? value)

Parameters

name string
value string

Returns

HtmlBuilder

WithCssClass(string?)

Set CSS classes attributes, if already exists it will be ignored.

public HtmlBuilder WithCssClass(string? classes)

Parameters

classes string

Returns

HtmlBuilder

WithCssClassIf(bool, string?)

Conditional to set classes attributes, if already exists it will be ignored.

public HtmlBuilder WithCssClassIf(bool conditional, string? classes)

Parameters

conditional bool
classes string

Returns

HtmlBuilder

WithDataAttribute(string, string)

Set a custom Bootstrap data attribute to HTML builder.

public HtmlBuilder WithDataAttribute(string name, string value)

Parameters

name string
value string

Returns

HtmlBuilder

WithHref(string)

public HtmlBuilder WithHref(string link)

Parameters

link string

Returns

HtmlBuilder

WithId(string)

public HtmlBuilder WithId(string id)

Parameters

id string

Returns

HtmlBuilder

WithName(string)

public HtmlBuilder WithName(string name)

Parameters

name string

Returns

HtmlBuilder

WithNameAndId(string?)

Set HTML builder name and ID.

public HtmlBuilder WithNameAndId(string? id)

Parameters

id string

Returns

HtmlBuilder

WithOnChange(string)

public HtmlBuilder WithOnChange(string value)

Parameters

value string

Returns

HtmlBuilder

WithOnClick(string)

public HtmlBuilder WithOnClick(string value)

Parameters

value string

Returns

HtmlBuilder

WithStyle(string)

public HtmlBuilder WithStyle(string value)

Parameters

value string

Returns

HtmlBuilder

WithToolTip(string?)

Sets a tooltip to the HTML Tag

public HtmlBuilder WithToolTip(string? tooltip)

Parameters

tooltip string

Returns

HtmlBuilder

WithValue(string)

Set custom data attribute to HTML builder.

public HtmlBuilder WithValue(string value)

Parameters

value string

Returns

HtmlBuilder