Change record status: 
Project: 
Introduced in branch: 
9.5.x
Introduced in version: 
9.5.0
Description: 

Prior to 9.5.0, it was difficult for theme developers to vary the markup in their block and layout templates between the preview mode in Layout Builder and the actual rendered mode.

From 9.5.0 a new in_preview variable is available to theme developers to utilise in their Twig templates.

For blocks, this will be available to all block templates using the "block" base hook in their hook_theme() definition. For layouts, this will be available to all templates registered via a [mymodule].layouts.yml or [mytheme].layouts.yml.

Theme developers may use this to e.g. conditionally add libraries, or add/remove additional classes to make the preview easier to interact with during layout editing.

Sample usage

{# @file two-column-layout.html.twig #}
<div {{ attributes.addClass(in_preview ? 'lb-preview' : '') }}>
   <div {{ region_attributes.col1 }}>
    {{ content.col1 }}
  </div>
   <div {{ region_attributes.col2 }}>
    {{ content.col2 }}
   </div>
</div>
Impacts: 
Site builders, administrators, editors
Module developers
Themers
Site templates, recipes and distribution developers