Lingkungan
Terkadang Anda perlu menampilkan konten di blok hanya saat dirender di dalam editor Gutenberg, bukan di frontend. Teknik ini sangat berguna untuk memberikan informasi dan placeholder yang membantu pengguna jika data yang diperlukan belum dimasukkan.
Editor
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 %}
Pratinjau
Blockstudio menambahkan variabel lingkungan lain yang berguna bagi pengembang blok. $isPreview memungkinkan Anda menampilkan konten secara kondisional di dalam jendela pratinjau blok saat mengarahkan kursor ke blok di inserter blok.
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 %}
Di luar template
$isEditor dan $isPreview hanya tersedia di dalam template. Kode yang berjalan selama render yang sama tetapi berada di luar template, seperti helper tema yang dipanggil dari template atau filter seperti blockstudio/blocks/components/rich_text/render, dapat memanggil bs_is_editor() dan bs_is_preview(). Keduanya mengembalikan nilai yang sama dengan yang diterima template.
function theme_card_title(string $title): string {
if ($title === '' && bs_is_editor()) {
return '<span class="placeholder">Add a title</span>';
}
return esc_html($title);
}
Tag blok selalu merender markup frontend, sehingga di dalam blok yang dirender dari tag blok, kedua fungsi ini mengembalikan false, sama seperti $isEditor dan $isPreview.