Vide
Dažkārt saturs blokos ir jāparāda tikai tad, ja tas tiek attēlots Gutenberg redaktorā, bet ne priekšgalā. Šis paņēmiens ir ļoti noderīgs, lai sniegtu lietotājiem noderīgu informāciju un vietturus, ja nepieciešamie dati vēl nav ievadīti.
Redaktors
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 %}
Priekšskatījums
Blockstudio pievieno vēl vienu vides mainīgo, kas noderēs bloku izstrādātājiem. $isPreview ļauj nosacīti attēlot saturu bloka priekšskatījuma logā, kad, virzot kursoru virs bloka ievietošanas rīkā, tiek parādīts bloka priekšskatījums.
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 %}
Ārpus veidnēm
$isEditor un $isPreview pastāv tikai veidnes iekšienē. Kods, kas darbojas tās pašas renderēšanas laikā, bet ārpus veidnes, piemēram, tēmas palīgfunkcija, ko izsauc veidne, vai filtrs, piemēram, blockstudio/blocks/components/rich_text/render, var izsaukt bs_is_editor() un bs_is_preview(). Tie atgriež tās pašas vērtības, ko saņem veidne.
function theme_card_title(string $title): string {
if ($title === '' && bs_is_editor()) {
return '<span class="placeholder">Add a title</span>';
}
return esc_html($title);
}
Bloku tagi vienmēr renderē priekšgala marķējumu, tāpēc blokā, kas renderēts no bloka taga, abas funkcijas atgriež false, tāpat kā $isEditor un $isPreview.