PHPackages                             daun/statamic-cache-directives - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. daun/statamic-cache-directives

ActiveLibrary[Caching](/categories/caching)

daun/statamic-cache-directives
==============================

Parse conditional comments for dynamic fragments in cached Statamic pages

2.2.1(2d ago)07↓100%MITPHPPHP ^8.4CI passing

Since Jul 5Pushed 2d agoCompare

[ Source](https://github.com/daun/statamic-cache-directives)[ Packagist](https://packagist.org/packages/daun/statamic-cache-directives)[ Docs](https://github.com/daun/statamic-cache-directives)[ RSS](/packages/daun-statamic-cache-directives/feed)WikiDiscussions main Synced today

READMEChangelog (6)Dependencies (8)Versions (7)Used By (0)

Statamic Cache Directives
=========================

[](#statamic-cache-directives)

Parse conditional HTML comment directives after Statamic has rendered a page, so cached pages can still include small dynamic fragments.

This package is a lightweight alternative to Statamic's [`nocache`](https://statamic.dev/tags/nocache) tag. It is useful for tiny auth-, role-, or request-dependent islands where you only need to keep or remove existing markup without the overhead of the nocache data/session pipeline.

It is implemented as a [static caching replacer](https://statamic.dev/advanced-topics/static-caching#replacers) for Statamic's half-measure static cache.

Installation
------------

[](#installation)

Install the package via composer:

```
composer require daun/statamic-cache-directives
```

Registration
------------

[](#registration)

Enable the replacer in `config/statamic/static_caching.php`.

```
+ use Daun\StatamicCacheDirectives\CacheDirectiveReplacer;

  'replacers' => [
      CsrfTokenReplacer::class,
      NoCacheReplacer::class,
+     CacheDirectiveReplacer::class,
  ],
```

Usage
-----

[](#usage)

Wrap markup in conditional comments. Matching blocks are kept when their expression evaluates to `true`; otherwise they are removed from the response.

```

  Account

```

The second format follows the [downlevel-hidden syntax](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/ms537512(v=vs.85)#syntax-of-conditional-comments) for conditional comments.

Syntax
------

[](#syntax)

Expressions use [Symfony Expression Language](https://symfony.com/doc/current/reference/formats/expression_language.html) syntax.

### If

[](#if)

```

  Visible to signed-in users.

```

### Unless

[](#unless)

```

  Visible to guests.

```

### Not

[](#not)

Use either `!` or `not`.

```

  Log in

  Regular user content.

```

### And

[](#and)

Use `and` or `&&`.

```

  Control Panel

```

### Or

[](#or)

Use `or` or `||`.

```

```

### Echo

[](#echo)

Use `echo` to print a variable value. Echo directives can be standalone or block-style. Output is escaped for html contexts.

```
