8. Richtext
A richtext type variable is used to represent and manipulate a sequence of characters that holds rich content.
<?
var @rt: richtext
@rt := "<strong>This text is very"
@rt := @rt + ToRichText(" strong</strong>")
?>Implicit HTML Encoding
When you concatenate a text value to a richtext variable, implicit encoding is applied to the text value. This is not always desired; therefore, the ToRichText() function can be used. Let’s dissect the example above.
var @rt: richtextThis line declares the variable @rt as richtext.
@rt := "<strong>This text is very"Since the left-hand side @rt is of type richtext and the expression on the right-hand side is a literal text, the text is treated as richtext. The value of the variable @rt is now <strong>This text is very.
@rt := @rt + ToRichText(" strong</strong>")Once again, the left-hand side @rt is of type richtext, but the expression on the right-hand side is a concatenation of richtext + richtext, resulting in the value <strong>This text is very strong</strong>.
What happens if we don't use the ToRichText() function?
@rt := @rt + " strong</strong>"In this case, the expression is of richtext + text. When evaluating the right text operand, it will be coerced into richtext. During this conversion, the text value will be HTML encoded, resulting in <strong>This text is very strong</strong>. This is not what we wanted in this example, as it generates malformed HTML.
So, be cautious when concatenating with richtext.
Postback
Fortunately, performing a postback with richtext is quite straightforward. You don't have to deal with localization, and updating the property with a new value is simply a matter of sending it back as is.
<? register input @RichTextProperty ?>
<textarea name="<?attr OutputId(@RichTextProperty) ?>"><?= @RichTextProperty ?></textarea>Print Output Overview
Since we are dealing with richtext, the encoding is mostly ignored, except for JavaScript encoding. Let’s assume we have a variable var @rt: richtext that holds the value <p>I am a paragraph.</p>. The following encoding (or lack thereof) will produce the corresponding outputs:
<?= @rt ?>
<p>I am a paragraph.</p>
<?attr @rt ?>
<p>I am a paragraph.</p>
<?js @rt ?>
"<p>I am a paragraph.</p>"
<?raw @rt ?>
<p>I am a paragraph.</p>
Last updated
Was this helpful?