
# Kontekst

Kui plokke kasutatakse tsükli sees (näiteks Query ploki puhul), käivitatakse plokk tsükli igal kordusel. Blockstudio pakub mugavaid otseteid praegusele tsüklile ja ümbritsevale kontekstile juurdepääsemiseks.


## PHP

```php title="index.php"
<div>
  <h1>
    This is the data of current
    element inside the loop:
    <?php echo $block['context']['postId'] ?>
    <?php echo $block['context']['postType'] ?>
  </h1>
  <h1>
    This is the data the current post:
    <?php echo $block['postId'] ?>
    <?php echo $block['postType'] ?>
  </h1>
</div>
```

## Twig

```twig title="index.twig"
<div>
  <h1>
    This is the data of current
    element inside the loop:
    {{ block.context.postId }}
    {{ block.context.postType }}
  </h1>
  <h1>
    This is the data the current post:
    {{ block.postId }}
    {{ block.postType }}
  </h1>
</div>
```

## Saadaolevad muutujad

Plokimallides on teil juurdepääs järgmistele muutujatele:

| Muutuja | Lühikuju | Kirjeldus |
|----------|-----------|-------------|
| `$attributes` | `$a` | Ploki atribuudid |
| `$block` | `$b` | Ploki andmed (nimi, pealkiri, postituse ID, postituse tüüp, kontekst jne) |
| `$context` | `$c` | Vanemploki kontekst |
| `$innerBlocks` | - | InnerBlocks’i sisu |

## Ploki andmed

Muutuja `$block` / `$b` sisaldab järgmist:

| Atribuut | Kirjeldus |
|----------|-------------|
| `name` | Ploki nimi (e.g., `my-theme/my-block`) |
| `title` | Ploki pealkiri |
| `dir` | Ploki kausta tee |
| `url` | Ploki kataloogi URL |
| `postId` | Praeguse postituse ID |
| `postType` | Praeguse postituse tüüp |
| `context` | Tsükli kontekst (Query ploki sees) |

Query-ploki sees jääb `postId` vaadatavaks postituseks, samal ajal kui `$post_id` ja `context.postId` järgivad praegust tsükli elementi. Kui päritud postitust pole, näiteks REST päringu või WP-CLI puhul, on `postId` postitus, mille jaoks plokki renderdatakse; see võetakse `postId` ploki kontekstist või globaalsest postitusest.

## Vanemkontekst

Kui kasutate `usesContext`-i, pääsete vanemploki andmetele juurde käsuga `$context`:

```php title="index.php"
<?php
$parent = $context['parent-block/name'];
echo $parent['someAttribute'];
?>
```

Blockstudio haldab `providesContext`-i automaatselt. Lisada tuleb ainult `usesContext` alamplokki. Vanemploki block.json-is pole vaja `providesContext` käsitsi määrata, sest Blockstudio registreerib selle kõigi atribuutide jaoks.

Lisateavet leiate jaotisest [InnerBlocks’i kontekst](https://blockstudio.dev/et/docs/blocks/react-components/innerblocks/#context).


> **[Ploki kontekst ja suhtlus](https://blockstudio.dev/et/guides/block-context/)**
>
> Praktilised mustrid: kaardivõrgustikud, hinnatabelid ja jaotiste ümbrised, kus plokid peavad andmeid jagama.
