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>?)
Insert a HTML builder as a child of caller builder.
public HtmlBuilder Append(HtmlTag tag, Action<HtmlBuilder>? builderAction = null)
Parameters
tag
HtmlTagbuilderAction
Action<HtmlBuilder>
Returns
AppendAsync(HtmlTag, Func<HtmlBuilder, Task>)
public Task<HtmlBuilder> AppendAsync(HtmlTag tag, Func<HtmlBuilder, Task> builderFunc)
Parameters
tag
HtmlTagbuilderFunc
Func<HtmlBuilder, Task>
Returns
AppendAsync(Func<Task<HtmlBuilder>>)
public Task<HtmlBuilder> AppendAsync(Func<Task<HtmlBuilder>> builderFunc)
Parameters
builderFunc
Func<Task<HtmlBuilder>>
Returns
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
AppendComponentIf(bool, Func<HtmlComponent?>)
public HtmlBuilder AppendComponentIf(bool condition, Func<HtmlComponent?> componentFunc)
Parameters
condition
boolcomponentFunc
Func<HtmlComponent>
Returns
AppendControlAsync(ControlBase?)
public ValueTask<HtmlBuilder> AppendControlAsync(ControlBase? control)
Parameters
control
ControlBase
Returns
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
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
AppendIfAsync(bool, Func<Task<HtmlBuilder>>)
public Task AppendIfAsync(bool condition, Func<Task<HtmlBuilder>> func)
Parameters
condition
boolfunc
Func<Task<HtmlBuilder>>
Returns
AppendInput(Action<HtmlBuilder>?)
public HtmlBuilder AppendInput(Action<HtmlBuilder>? builderAction = null)
Parameters
builderAction
Action<HtmlBuilder>
Returns
AppendLabel(Action<HtmlBuilder>?)
public HtmlBuilder AppendLabel(Action<HtmlBuilder>? builderAction = null)
Parameters
builderAction
Action<HtmlBuilder>
Returns
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>?)
public HtmlBuilder AppendSpan(Action<HtmlBuilder>? builderAction = null)
Parameters
builderAction
Action<HtmlBuilder>
Returns
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
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 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