PHPackages                             acalvino4/twig-embed-implicit-default - 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. [Templating &amp; Views](/categories/templating)
4. /
5. acalvino4/twig-embed-implicit-default

ActiveLibrary[Templating &amp; Views](/categories/templating)

acalvino4/twig-embed-implicit-default
=====================================

Extends the twig embed tag to allow an implicit default block

1.0.1(3y ago)0534↓50%[1 issues](https://github.com/acalvino4/twig-embed-implicit-default/issues)1mitPHPPHP &gt;=8.0.2

Since Apr 27Pushed 3y ago1 watchersCompare

[ Source](https://github.com/acalvino4/twig-embed-implicit-default)[ Packagist](https://packagist.org/packages/acalvino4/twig-embed-implicit-default)[ Docs](https://github.com/acalvino4/twig-embed-with-default-block)[ RSS](/packages/acalvino4-twig-embed-implicit-default/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (4)Versions (5)Used By (1)

Twig Embed with Implicit Default Block
======================================

[](#twig-embed-with-implicit-default-block)

[![License](https://camo.githubusercontent.com/0abc851549ebd9267f5270f102b3158164c3f328646ef86e410124d84c6c4ead/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6163616c76696e6f342f747769672d656d6265642d696d706c696369742d64656661756c74)](https://camo.githubusercontent.com/0abc851549ebd9267f5270f102b3158164c3f328646ef86e410124d84c6c4ead/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6163616c76696e6f342f747769672d656d6265642d696d706c696369742d64656661756c74)[![Build Status](https://camo.githubusercontent.com/1799414944b6f7fb73fc4d267362bd1f92111d5d1e97662c128eade15df16cf7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6163616c76696e6f342f747769672d656d6265642d696d706c696369742d64656661756c742f71612e796d6c)](https://camo.githubusercontent.com/1799414944b6f7fb73fc4d267362bd1f92111d5d1e97662c128eade15df16cf7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6163616c76696e6f342f747769672d656d6265642d696d706c696369742d64656661756c742f71612e796d6c)

[![Test Coverage](https://raw.githubusercontent.com/acalvino4/twig-embed-implicit-default/image-data/coverage.svg)](https://raw.githubusercontent.com/acalvino4/twig-embed-implicit-default/image-data/coverage.svg)[![Phpstan Level](https://camo.githubusercontent.com/d117944b58da8146f96b4ef7403807610a20eeb3fbcaaaf95157bbcdad1686eb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230382d627269676874677265656e2e737667)](https://camo.githubusercontent.com/d117944b58da8146f96b4ef7403807610a20eeb3fbcaaaf95157bbcdad1686eb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230382d627269676874677265656e2e737667)[![Easy Coding Standard](https://camo.githubusercontent.com/2ba5a2176d1be288f5bdfb6c46a6730d4c76ba622a73efd1ad20f341b798f778/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f45617379253230436f64696e672532305374616e646172642d253230656e61626c65642d627269676874677265656e2e737667)](https://camo.githubusercontent.com/2ba5a2176d1be288f5bdfb6c46a6730d4c76ba622a73efd1ad20f341b798f778/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f45617379253230436f64696e672532305374616e646172642d253230656e61626c65642d627269676874677265656e2e737667)

TLDR
----

[](#tldr)

```
{% embed 'wrapper.twig' %}
  Default block content
{% endembed %}

{# wrapper.twig #}

    {% block default '' %}{# --> renders "Default block content" #}

```

> **Warning**This extension introduces a breaking change to twig. However, it will most likely not affect too many of your files, is probably quite easy to work around, and is most definitely worth it. See [details](#breaking-change).

Contents
--------

[](#contents)

1. [Rationale](#rationale)
2. [Usage](#usage)
    - [Details](#notes)
    - [Breaking Change](#breaking-change)
3. [Getting Started](#getting-started)
    - [Craft CMS](#craft-cms)

Rationale
---------

[](#rationale)

In vanilla twig, to override a slot of a parent template via an embed, we need to declare a block.

```
{% embed 'wrapper.twig' %}
  {% block content %}
    This is default block content
  {% endblock %}
{% endembed %}

{# example wrapper.twig #}

    {% block content %}{% endblock %}

```

When following best practices to keep code modular and dry, you'll typically create components for various code chunks, but this results in rather bloated and unreadable markup, especially as you start to nest (and this is only two levels!):

```
{% embed 'wrapper.twig' %}
  {% block default %}

      This is a CTA, so naturally it includes a
      {% embed 'button.twig' %}
        {% block default %}Button{% endblock %}
      {% endembed %}

  {% endblock %}
{% endembed %}
```

The explicit block declarations are handy for extending layouts (where you'll need 'header', 'footer', and 'sidebar', for example), but in most cases, `embed` is probably just being used to include components where you need to pass in some content, and for *these* cases, typically only one content slot is required.

So we've sacrificed simplicity of the base case for greater flexibility ... but what if we could have both?

Usage
-----

[](#usage)

This twig extension allows the use of an implicit default block in embeds:

```
{% embed 'wrapper.twig' %}
  This is default block content
{% endembed %}

{# same as this, which still will work #}
{% embed 'my_subtemplate.twig' %}
  {% block default %}
    This is default block content
  {% endblock %}
{% endembed %}
```

The longer example above could become

```
{% embed 'wrapper.twig' %}

      This is a CTA, so naturally it includes a
      {% embed 'button.twig' %}Button{% endembed %}

{% endembed %}
```

To specify where that default content goes in the component template, follow the `wrapper.twig` example above, or, if your default block has no/simple default content, you can use twig's [block shortcut syntax](https://twig.symfony.com/doc/3.x/tags/extends.html#block-shortcuts):

```
{% block default %}{% endblock %}
{# same as #}
{% block default '' %}
```

This works because all this extension does is parse the initial content within the embed into a block named 'default'.

### Notes

[](#notes)

The implicit default content can be combined with (any number of) named blocks:

```
{% embed 'wrapper.twig' %}
  This is default block content
  {% block special_slot %}
    Special content
  {% endblock %}
{% endembed %}

{# example wrapper.twig #}

    {% block default '' %}
    {% block special_slot '' %}

```

The implicit default content needs to be placed *before* any explicit blocks (See [this issue](https://github.com/acalvino4/twig-embed-implicit-default/issues/1)):

```
{# Will throw error: "A template that extends another one cannot include content outside Twig blocks." #}
{% embed 'wrapper.twig' %}
  {% block special_slot %}
    Special content
  {% endblock %}
  This is default block content
{% endembed %}
```

If you have both implicit default content and an explicit default block, an error will be thrown (just as when you declare two blocks with the same name):

```
{# Will throw error: "The block 'default' has already been defined" #}
{% embed 'wrapper.twig' %}
  This is default block content
  {% block default %}
    Other default content???
  {% endblock %}
{% endembed %}
```

### Breaking Change

[](#breaking-change)

Twig, by default, does not allow content *that outputs* within `embed` tags but outside `block` tags. In this respect, this extension doesn't change any defined behavior; it only defines what previously was an error. However, twig does allow *non-outputting content* in such a context, for example:

```
{% embed 'wrapper.twig' %}
  {% set a = 'foo bar' %}
  {% block content %}
    {{ a }}
  {% endblock %}
{% endembed %}
```

Using this extension, that `set` tag would get absorbed as part of the default block, and `a` would be undefined in the following block.

In this case, the solution is simple: move the `set` tag outside the `embed` scope. I suspect most instances of this change will be equally straightforward to adapt for this extension. If you run into something more complex, please file an issue and I will try to deal with or document that case.

Getting Started
---------------

[](#getting-started)

```
composer require acalvino4/twig-embed-implicit-default
```

Then register the extension as appropriate for your framework.

- [Vanilla](https://twig.symfony.com/doc/3.x/advanced.html#extending-twig)
- [Craft CMS](https://craftcms.com/docs/4.x/extend/extending-twig.html#register-a-twig-extension)
- [Symphony](https://symfony.com/doc/current/templates.html#register-an-extension-as-a-service)

### Craft CMS

[](#craft-cms)

Since I am primarily a Craft developer, I created a plugin that wraps this package, so including it in your site is as easy an any other plugin.

```
composer require acalvino/craft-embed-implicit-default
./craft plugin/install embed-implicit-default
```

Or just find it on the plugin store and install from there.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

3

Last Release

1110d ago

Major Versions

0.1 → 1.0.02023-05-02

### Community

Maintainers

![](https://www.gravatar.com/avatar/3bc1dd885fe50d32f5ad8cac47ea66427ad08c71a4852b735ef0d4da62331d98?d=identicon)[acalvino4](/maintainers/acalvino4)

---

Top Contributors

[![acalvino4](https://avatars.githubusercontent.com/u/43254861?v=4)](https://github.com/acalvino4 "acalvino4 (31 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/acalvino4-twig-embed-implicit-default/health.svg)

```
[![Health](https://phpackages.com/badges/acalvino4-twig-embed-implicit-default/health.svg)](https://phpackages.com/packages/acalvino4-twig-embed-implicit-default)
```

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91492.0M315](/packages/twig-extra-bundle)[twig/intl-extra

A Twig extension for Intl

36763.2M221](/packages/twig-intl-extra)[rcrowe/twigbridge

Adds the power of Twig to Laravel

9105.9M50](/packages/rcrowe-twigbridge)[twig/string-extra

A Twig extension for Symfony String

22046.0M133](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

22918.5M55](/packages/twig-cssinliner-extra)[symfony/ux-twig-component

Twig components for Symfony

21914.8M162](/packages/symfony-ux-twig-component)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
