
# Prostředí

Někdy je potřeba zobrazit obsah bloků výhradně při vykreslení uvnitř editoru Gutenberg, nikoli na frontendu. Tato technika je užitečná pro poskytování nápovědy a zástupného obsahu uživatelům, pokud ještě nezadali potřebná data.

## Editor


### PHP

```php title="index.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

```twig title="index.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 %}
```

## Náhled

Blockstudio přidává další proměnnou prostředí, která se hodí vývojářům bloků. `$isPreview` umožňuje podmíněně vykreslit obsah v okně náhledu bloku při najetí na blok v nabídce vkládání bloků.


### PHP

```php title="index.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

```twig title="index.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 %}
```

## Mimo šablony

`$isEditor` a `$isPreview` existují pouze uvnitř šablony. Kód spuštěný během stejného vykreslení mimo šablonu, například pomocná funkce šablony volaná z šablony nebo filtr jako `blockstudio/blocks/components/rich_text/render`, může zavolat `bs_is_editor()` a `bs_is_preview()`. Vrátí stejné hodnoty, jaké získá šablona.

```php title="functions.php"
function theme_card_title(string $title): string {
  if ($title === '' && bs_is_editor()) {
    return '<span class="placeholder">Add a title</span>';
  }

  return esc_html($title);
}
```

Značky bloků vždy vykreslují značky frontendu, takže uvnitř bloku vykresleného ze značky bloku vrátí obě funkce hodnotu `false` stejně jako `$isEditor` a `$isPreview`.
