How to preserve whitespace in string fields

Knowledge Drop

Last tested: Sep 3, 2020
 

The Problem

By default, Looker does not preserve whitespace in string fields. If you have long strings that are broken up by line breaks or other whitespace, these can become difficult to read when stripped of their whitespace.

A Solution

The html field parameter can be used in combination with the rendered_value Liquid variable to specify that whitespace should be preserved.

The following LookML code can be added to the field definition:

html: <p><span style="white-space:pre-wrap;">{{ rendered_value }}</span></p>;;

{{ rendered_value }} returns the value of the field with Looker’s default formatting. The HTML <p> tag defines a paragraph, and the <span> tag is an inline container used to style text. The white-space CSS property specifies how whitespace should be handled for an element, and the pre-wrap value for this property specifies that whitespace should be preserved by the browser, and that text should wrap when necessary and on line breaks. More information on this property can be found in W3Schools' CSS white-space Property documentation.

This content is subject to limited support.                

Version history
Last update:
‎06-24-2021 11:53 PM
Updated by: