DotVVM DOCS

Version: 1.0 | 1.1 | 2.0 |

Literal

in namespace DotVVM.Framework.Controls

Renders a text into the page.

Properties

Name Type Description Notes Default Value
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
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

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