Ympäristö
Joskus lohkoissa on näytettävä sisältöä vain Gutenberg-editorissa, ei käyttöliittymässä. Tällä tekniikalla käyttäjille voi tarjota hyödyllisiä tietoja ja paikkamerkkejä, jos tarvittavia tietoja ei ole vielä syötetty.
Editori
PHP
<?php if ($isEditor) : ?>
This content is only going to be rendered inside the editor.
<?php else : ?>
This content is only going to be rendered on the frontend.
<?php endif; ?>
Twig
{% if isEditor %}
This content is only going to be rendered inside the editor.
{% else %}
This content is only going to be rendered on the frontend.
{% endif %}
Esikatselu
Blockstudio lisää toisen ympäristömuuttujan, josta on hyötyä lohkojen kehittäjille. $isPreview-muuttujan avulla sisältöä voi renderöidä ehdollisesti lohkon esikatseluikkunassa, kun osoitin viedään lohkon päälle lohkojen lisäysvalikossa.
PHP
<?php if ($isPreview) : ?>
This content is only going to be rendered inside the block preview.
<?php else : ?>
This content is only going to be rendered on the frontend and editor.
<?php endif; ?>
Twig
{% if isPreview %}
This content is only going to be rendered inside the block preview.
{% else %}
This content is only going to be rendered on the frontend and editor.
{% endif %}
Mallien ulkopuolella
$isEditor ja $isPreview ovat käytettävissä vain mallin sisällä. Saman renderöinnin aikana mallin ulkopuolella suoritettava koodi, kuten mallista kutsuttu teeman apufunktio tai suodatin kuten blockstudio/blocks/components/rich_text/render, voi kutsua funktioita bs_is_editor() ja bs_is_preview(). Ne palauttavat samat arvot kuin mallille välitetään.
function theme_card_title(string $title): string {
if ($title === '' && bs_is_editor()) {
return '<span class="placeholder">Add a title</span>';
}
return esc_html($title);
}
Lohkotunnisteet renderöivät aina käyttöliittymän merkinnät, joten lohkotunnisteesta renderöidyn lohkon sisällä molemmat funktiot palauttavat arvon false, kuten myös $isEditor ja $isPreview.