DOT

VVM

 DOCS

Version: 1.0 | 1.1 |

Literal

in namespace DotVVM.Framework.Controls

Renders a text into the page.

Properties

Name Type Description Notes Default Value
ClientIDMode ClientIDMode Gets or sets the client ID generation algorithm.
attribute
inner element
static value
bindable
default
1
DataContext Object Gets or sets a data context for the control and its children. All value and command bindings are evaluated in context of this value.
attribute
inner element
static value
bindable
default
FormatString String Gets or sets the format string that will be applied to numeric or date-time values.
attribute
inner element
static value
bindable
default
ID String Gets or sets the unique control ID.
attribute
inner element
static value
bindable
default
InnerText String Gets or sets the inner text of the HTML element.
attribute
inner element
static value
bindable
default
RenderSpanElement Boolean Gets or sets whether the literal should render the wrapper span HTML element.
attribute
inner element
static value
bindable
default
True
Text Object Gets or sets the text displayed in the control.
attribute
inner element
static value
bindable
default
ValueType FormatValueType Gets or sets the type of value being formatted - Number or DateTime.
attribute
inner element
static value
bindable
default
0
Visible Boolean Gets or sets whether the control is visible.
attribute
inner element
static value
bindable
default
True

Events

Name Type Description

Usage & Scenarios

This control is used to render a text in the page. If you use the binding with double curly braces {{value: Something}}, it generates the Literal control.

If you set the FormatString property, you can apply a format string to numbers and date-time values. Most of .NET Framework format strings is supported.

By default, the Literal encodes the text, so you don't have to worry about the script injection attacks.

If you want to render raw HTML content in the page, use HtmlLiteral control instead.

HTML Rendered by the Control

In the Client rendering mode, the control renders a span with Knockout data-bind

<span data-bind="text: expression"></span>

If you set the RenderSpanElement to false, the span won't be rendered. In this mode you cannot apply HTML attributes to the control and use some properties (e.g. Visible, ID).

<!-- ko text: expression --><!-- /ko -->

In the Server rendering mode, the text is rendered directly to the response. This helps the search engines to understand the page content.

<span>Text</span>

If you turn the span element off, only the raw text will be rendered.

Sample 1: Basic Literal

Use the Text property to supply the value to be rendered.

If the value is a number or a DateTime, you can set the FormatString property to specify the format of the value. Make sure that the correct culture is set on the server side. See the Globalization tutorial.

<dot:Literal Text="{value: Text}" />

<dot:Literal Text="{value: Date}" FormatString="d.M.yyyy H:mm:ss" />
using System;
using System.Threading;
using DotVVM.Framework.ViewModel;

namespace DotvvmWeb.Views.Docs.Controls.builtin.Literal.sample1
{
    public class ViewModel : DotvvmViewModelBase
    {
        public string Text => "Hello DotVVM";

        public DateTime Date => DateTime.Now;
    }
}

Sample 2: Disabling the SPAN element

Sometimes you just need to render the text itself without any wrapper element. You can use the RenderSpanElement property to disable rendering the <span> element around the text.

Please keep in mind that if you disable the span element, you won't be able to add HTML attributes to the Literal (e.g. class, style) and you won't be able to set the Visible property because there is no element on which this properties could be applied.

<dot:Literal Text="{value: Text}" RenderSpanElement="false" />
using System;
using System.Threading;
using DotVVM.Framework.ViewModel;

namespace DotvvmWeb.Views.Docs.Controls.builtin.Literal.sample2
{
    public class ViewModel : DotvvmViewModelBase
    {
        public string Text => "Hello DotVVM";

        public DateTime Date => DateTime.Now;
    }
}

Suggestions


Help & Contribute

The documentation is maintained on GitHub.
We'll be happy if you edit the page and create a pull request.

Send message