FormattableMarkup
class FormattableMarkup implements MarkupInterface, Countable (View source)
Formats a string for HTML display by replacing variable placeholders.
When cast to a string, this object replaces variable placeholders in the string with the arguments passed in during construction and escapes the values so they can be safely displayed as HTML. See the documentation of \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details on the supported placeholders and how to use them securely. Incorrect use of this class can result in security vulnerabilities.
In most cases, you should use TranslatableMarkup or PluralTranslatableMarkup rather than this object, since they will translate the text (on non-English-only sites) in addition to formatting it. Variables concatenated without the insertion of language-specific words or punctuation are some examples where translation is not applicable and using this class directly directly is appropriate.
This class is designed for formatting messages that are mostly text, not as an HTML template language. As such:
- The passed in string should contain no (or minimal) HTML.
- Variable placeholders should not be used within the "<" and ">" of an HTML tag, such as in HTML attribute values. This would be a security risk. Examples:
Properties
| protected string | $string | The string containing placeholders. |
|
| protected array | $arguments | The arguments to replace placeholders with. |
Methods
Constructs a new class instance.
Returns markup.
Returns the string length.
Returns a representation of the object for use in JSON serialization.
Replaces placeholders in a string with values.
Details
__construct(string $string, array $arguments)
Constructs a new class instance.
string
__toString()
Returns markup.
int
count()
Returns the string length.
string
jsonSerialize()
Returns a representation of the object for use in JSON serialization.
static protected string
placeholderFormat(string $string, array $args)
Replaces placeholders in a string with values.
static protected string
placeholderEscape(string|MarkupInterface $value)
Escapes a placeholder replacement value if needed.