PHPackages                             kevinquillen/twig-partials - 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. kevinquillen/twig-partials

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

kevinquillen/twig-partials
==========================

Django 6 style partial template support for Twig.

11PHPCI passing

Since Dec 10Pushed 6mo agoCompare

[ Source](https://github.com/kevinquillen/twig-partials)[ Packagist](https://packagist.org/packages/kevinquillen/twig-partials)[ RSS](/packages/kevinquillen-twig-partials/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Twig Partials
=============

[](#twig-partials)

A Twig extension that adds Django-style partial template rendering. Define reusable template fragments and render them inline or via AJAX requests.

Requirements
------------

[](#requirements)

- PHP 8.3 or higher
- Twig 3.15 or higher

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

[](#installation)

```
composer require kevinquillen/twig-partials
```

Usage
-----

[](#usage)

### Defining Partials

[](#defining-partials)

Use the `partialdef` tag to define a reusable fragment within a template:

```
{% partialdef view_count %}
  {{ video.views }} views
{% endpartialdef %}
```

### Rendering Partials Inline

[](#rendering-partials-inline)

Use the `partial` tag to render a defined partial within the same template:

```
{% partialdef info %}

    {{ video.title }}
    {{ video.views }} views

{% endpartialdef %}

  {% partial info %}

```

### Full Example

[](#full-example)

```
{# video.twig #}

{% partialdef view_count %}
  {{ video.views }} views
{% endpartialdef %}

{% partialdef comments_section %}

    {% for comment in video.comments %}
      {{ comment.text }}
    {% endfor %}

{% endpartialdef %}

  {{ video.title }}
  {% partial view_count %}

  {% partial comments_section %}

```

### Rendering the Full Template

[](#rendering-the-full-template)

```
$twig->render('video.twig', ['video' => $video]);
```

This renders the entire template including all partials.

### Rendering Only a Fragment (AJAX)

[](#rendering-only-a-fragment-ajax)

The extension supports rendering only a specific partial using fragment syntax:

```
$twig->render('video.twig#view_count', ['video' => $video]);
```

This renders only the `view_count` partial, which is useful for AJAX updates.

Setup
-----

[](#setup)

### Standard Twig

[](#standard-twig)

```
use Twig\Environment;
use TwigPartials\Extension\PartialExtension;
use TwigPartials\Loader\PartialLoader;

$loader = new PartialLoader(['/path/to/templates']);
$twig = new Environment($loader);
$twig->addExtension(new PartialExtension());
```

### Symfony

[](#symfony)

Register the extension as a service:

```
services:
  TwigPartials\Extension\PartialExtension:
    tags: ['twig.extension']
```

To enable fragment rendering, replace the default loader:

```
services:
  TwigPartials\Loader\PartialLoader:
    decorates: twig.loader.native_filesystem
    arguments: ['@TwigPartials\Loader\PartialLoader.inner']
```

How It Works
------------

[](#how-it-works)

The `partialdef` tag compiles into a separate method on the template class. When you use `partial`, it calls that method and outputs the result. The content has access to the same context variables as the rest of the template.

When using the fragment syntax (`template.twig#fragment_name`), the loader recognizes the fragment identifier and the runtime can render only that specific partial instead of the full template.

Running Tests
-------------

[](#running-tests)

```
composer install
./vendor/bin/phpunit
```

License
-------

[](#license)

MIT

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance46

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity13

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/362176?v=4)[Kevin](/maintainers/kevinquillen)[@kevinquillen](https://github.com/kevinquillen)

---

Top Contributors

[![kevinquillen](https://avatars.githubusercontent.com/u/362176?v=4)](https://github.com/kevinquillen "kevinquillen (24 commits)")

### Embed Badge

![Health badge](/badges/kevinquillen-twig-partials/health.svg)

```
[![Health](https://phpackages.com/badges/kevinquillen-twig-partials/health.svg)](https://phpackages.com/packages/kevinquillen-twig-partials)
```

###  Alternatives

[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3851.2M](/packages/limenius-react-bundle)[ktquez/laravel-tinymce

TinyMCE editor for Laravel and Lumen Framework

2525.4k](/packages/ktquez-laravel-tinymce)[jelix/wikirenderer

WikiRenderer is a library to generate HTML or anything else from wiki content.

1712.3k1](/packages/jelix-wikirenderer)[webkinder/sproutset

A Composer package for handling responsive images in Roots Bedrock + Sage + Blade projects.

282.2k](/packages/webkinder-sproutset)

PHPackages © 2026

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