Documentation

Html

Deprecated: Please note that future versions of ThoughtFarmer will not support RazorEngine and Server C#. Please instead use JavaScript and fetch and update data via ajax and the Public REST API.


Raw Method

Description:

Outputs raw HTML for strings containing HTML code.

Syntax:

public IHtmlString Raw(string value)

Parameters:

Parameter Type Description
value string The value to be output in the template

Return value:

Type Description
IHtmlString The string converted to a raw representation of HTML for output
 

Examples:

 @Html.Raw("<div class=\"information\">Note: this will output as raw HTML</div>")


If Method

Outputs the value into the template if it passes the truth condition.

Syntax:

public MvcHtmlString If(bool condition, string html)

Parameters:

Parameter Type Description
condition bool The test condition
html string The HTML to output if the condition evaluates to true

Return value:

Type Description
MvcHtmlString Represents an HTML-encoded string that should not be encoded again.
 

Examples:

 @Html.If(users.Count > 0, "There are some users!")


If Method

Outputs the value into the template if it passes the truth condition, alternatively outputting the else value upon false.

Syntax:

public MvcHtmlString If(bool condition, string html, string @else)

Parameters:

Parameter Type Description
condition bool The test condition
html string The HTML to output if the condition evaluates to true
@else string The HTML to output if the condition evaluates to false

Return value:

Type Description
MvcHtmlString Represents an HTML-encoded string that should not be encoded again.
 

Examples:

 @Html.If(users.Count > 0, "There are some users!", "There are no users!")


If Method

Outputs the value into the template if it passes the truth condition.

Syntax:

public HelperResult If(bool condition, Func<object, HelperResult> template)

Parameters:

Parameter Type Description
condition bool The test condition
template Func<object, HelperResult> The template to render

Return value:

Type Description
HelperResult Represents the result of a helper action as an HTML-encoded string.
 

Examples:

 @Html.If(users.Count > 0, @<div>There are some users!</div>)


If Method

Outputs the value into the template if it passes the truth condition.

Syntax:

public HelperResult If(bool condition, Func<object, HelperResult> template, Func<object, HelperResult> @else)

Parameters:

Parameter Type Description
condition bool The test condition
template Func<object, HelperResult> The template to render
@else Func<object, HelperResult> The alternative template to render

Return value:

Type Description
HelperResult Represents the result of a helper action as an HTML-encoded string.
 

Examples:

 @Html.If(users.Count > 0, @<div>There are some users!</div>, @<div>There are no users!</div>)


ClassIf Method

Outputs a class attribute on an HTML element if it passes the truth test.

Syntax:

public MvcHtmlString ClassIf(bool condition, string @class)

Parameters:

Parameter Type Description
condition bool The test condition
@class string The class to output

Return value:

Type Description
MvcHtmlString Represents an HTML-encoded string that should not be encoded again.
 

Examples:

<ul> @foreach (var user in Api.User.GetAll())
{
    <li @Html.ClassIf(!user.IsAdministrator, "administrator")>user.FullName</li>
}
</ul>


StyleIf Method

Outputs a style attribute on an HTML element if it passes the truth test.

Syntax:

public MvcHtmlString StyleIf(bool condition, string style)

Parameters:

Parameter Type Description
condition bool The test condition
style string The style to output

Return value:

Type Description
MvcHtmlString Represents an HTML-encoded string that should not be encoded again.
 

Examples:

<ul> @foreach (var user in Api.User.GetAll())
{
    <li @Html.StyleIf(user.IsGuest, "display: none;")>user.FullName</li>
}
</ul>


AttributeIf Method

Outputs an attribute on an HTML element if it passes the truth test.

Syntax:

public MvcHtmlString ClassIf(bool condition, string attribute)

Parameters:

Parameter Type Description
condition bool The test condition
attribute string The attribute to output

Return value:

Type Description
MvcHtmlString Represents an HTML-encoded string that should not be encoded again.
 

Examples:

<h1>Toggle administrators</h1>
<ul> @foreach (var user in Api.User.GetAll())
{
    <li><input type="checkbox" @Html.AttributeIf(user.IsAdministrator, "checked") />user.FullName</li>
}
</ul>


AttributeIf Method

Outputs an attribute on an HTML element if it passes the truth test.

Syntax:

public MvcHtmlString AttributeIf(bool condition, string attribute, string value)

Parameters:

Parameter Type Description
condition bool The test condition
attribute string The attribute to output
value string The attribute value

Return value:

Type Description
MvcHtmlString Represents an HTML-encoded string that should not be encoded again.
 

Examples:

<ul> @foreach (var user in Api.User.GetAll())
{
    <li @Html.AttributeIf(!user.IsGuest, "userId", user.UserId.ToString())>user.FullName</li>
}
</ul>