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 5mo 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 1mo ago

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

18

—

LowBetter than 8% of packages

Maintenance50

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity12

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://www.gravatar.com/avatar/4b8cf485680be70b0e4c3d1966a5e5ef9f5c10362a0e1625b129d8a7a4b39018?d=identicon)[kevinquillen](/maintainers/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

[mustache/mustache

A Mustache implementation in PHP.

3.3k44.6M291](/packages/mustache-mustache)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[whitecube/nova-flexible-content

Flexible Content &amp; Repeater Fields for Laravel Nova.

8053.0M25](/packages/whitecube-nova-flexible-content)[mopa/bootstrap-bundle

Easy integration of twitters bootstrap into symfony2

7042.9M33](/packages/mopa-bootstrap-bundle)[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3871.2M](/packages/limenius-react-bundle)[nicmart/string-template

StringTemplate is a very simple string template engine for php. I've written it to have a thing like sprintf, but with named and nested substutions.

2101.7M30](/packages/nicmart-string-template)

PHPackages © 2026

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