PHPackages                             guym4c/twig-prop-types - 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. guym4c/twig-prop-types

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

guym4c/twig-prop-types
======================

Prop Types for Twig components

v2.1.0(5y ago)613.3k1[1 issues](https://github.com/guym4c/twig-prop-types/issues)MITPHP

Since Apr 26Pushed 5y ago1 watchersCompare

[ Source](https://github.com/guym4c/twig-prop-types)[ Packagist](https://packagist.org/packages/guym4c/twig-prop-types)[ Docs](https://github.com/guym4c/twig-prop-types)[ RSS](/packages/guym4c-twig-prop-types/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (4)Dependencies (2)Versions (5)Used By (0)

`twig-prop-types`
=================

[](#twig-prop-types)

React prop types, ported to Twig.

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

[](#installation)

With Composer:

```
composer require guym4c/twig-prop-types
```

Usage
-----

[](#usage)

This package makes [guym4c/prop-types](https://github.com/guym4c/prop-types-php) accessible in Twig. For documentation on how to use the prop types themselves, refer to that package.

*Props* in the Twig sense refers to the current context. You can use the `only` keyword in Twig to stop the context scope being inherited.

```
$twigEnv->addExtension(new Guym4c\TwigProps\PropTypesExtension($twigEnv, $bypass));
```

The extension constructor requires the current Twig environment and a flag to bypass the prop checker. You will probably want to prevent the validator from running in production, to avoid any performance issues. (This will not prevent default prop values being set.)

Validation
----------

[](#validation)

To validate your props, put the following at the top of a template file:

```
{{
    props({
        optionalNumber: PropTypes.number(),
        aString: PropTypes.string().isRequired(),
        anObject: PropTypes.instanceof('An.Instance.Of.This.Class'),
        heey: PropTypes.shape({
            maca: PropTypes.string().isRequired(),
            rena: PropTypes.string().isRequired(),
        }),
    })
}}
```

### Twig-specific differences

[](#twig-specific-differences)

When giving a fully-qualified classname when checking for an instance type, you may use dots instead of backslashes to simplify string escaping issues, and the leading backslash or dot is optional.

### Advanced

[](#advanced)

You may pass additional properties to the constructor:

```
public function __construct(
    Twig\Environment $view,
    bool $bypass,
    string $typesGlobal = 'PropTypes',
    array $exclude = [],
    bool $allowExtraProperties = false
) {
```

- `$twigEnv`: the current Twig environment
- `$bypass`:
- `$typesGlobal` (optional): a custom name for the `PropTypes` global
- `$exclude` (optional): context variables to exclude from validation. Variables prefixed with an underscore `_` (and the types global) are automatically excluded
- `$allowExtraProperties` (optional): Whether prop validation should fail if unexpected unexcluded extra props are found in the context (false by default)

Default values
--------------

[](#default-values)

If required, you must set default values of required props after the `props()` call to ensure that required props fail correctly if not provided.

```
{{
    props({
        someProp: PropTypes.string(),
    })
    ~
    defaults({
        someProp: 'foo',
    })
}}
```

The `props()` and `defaults()` calls are separated by the Twig concatenate operator (`~`) so that they may be called in the same `{{` output block `}}` without throwing an error. Both of these functions never return a value, so it is safe to do so.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity59

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 ~13 days

Total

4

Last Release

2168d ago

Major Versions

v1.0.1 → v2.0.02020-05-03

### Community

Maintainers

![](https://www.gravatar.com/avatar/9db8acda212740c63090beab8438300c1e158e824a9642fa49babef315136c4c?d=identicon)[guym4c](/maintainers/guym4c)

---

Top Contributors

[![guym4c](https://avatars.githubusercontent.com/u/2103489?v=4)](https://github.com/guym4c "guym4c (6 commits)")

---

Tags

twigpropsproptypes

### Embed Badge

![Health badge](/badges/guym4c-twig-prop-types/health.svg)

```
[![Health](https://phpackages.com/badges/guym4c-twig-prop-types/health.svg)](https://phpackages.com/packages/guym4c-twig-prop-types)
```

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

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

A Twig extension for Intl

36663.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

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

A Twig extension to allow inlining CSS

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

Twig components for Symfony

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

PHPackages © 2026

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