PHPackages                             wavelo/const-macro - 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. wavelo/const-macro

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

wavelo/const-macro
==================

Extends Latte templating engine with `{const ?}` macro for destructuring of objects and arrays inspired by ES2015

v0.9.4(6y ago)167MITPHPPHP &gt;=5.5

Since Mar 4Pushed 6y ago2 watchersCompare

[ Source](https://github.com/wavelo/ConstMacro)[ Packagist](https://packagist.org/packages/wavelo/const-macro)[ RSS](/packages/wavelo-const-macro/feed)WikiDiscussions master Synced 2mo ago

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

const-macro
===========

[](#const-macro)

Extends Latte templating engine with `{const ?}` macro for destructuring of objects and arrays inspired by ES2015

### Installation

[](#installation)

Use composer:

```
$ composer require wavelo/const-macro
```

Install in configuration file given Latte\\Engine:

```
$latte = new Latte\Engine;
ConstMacro::install($latte->getCompiler());
```

When using Nette Dependency Injection, you can install in latte config.neon section:

```
latte:
  macros:
    - ConstMacro::install
```

### Basic example

[](#basic-example)

Given variable `$props = ["title"=>"Hello World", "price"=>123.0]` you can take advantage of following syntax:

```
{const [title:, price:, sale:=false] = $props /}
{$title}
Price: {$price}
Sale: YES
```

Renders as follows:

```
Hello World
Price: 123
```

### Features

[](#features)

- {const ?} macros can be nested
- const macro applies block scope for used variable
- can be used inside {foreach ?} instead of $value (or both destructuring and $value)
- every element can have arbitrary default value (even nested arrays or containing function call)
- supports nested destructuring
- supports ...rest operator
- supports destructuring of both arrays and objects

For more examples see tests. Short demonstration:

```
{foreach $data as $key => [title:, image:[src, width, height], ...rest]}
  {* scope of variables $title, $src, etc. is bounded by foreach block! *}
{/foreach}

{foreach $data as $key => $item, [title:]}
  {* variable $item contains full object/array *}
{/foreach}

{const [title:] = $props}
  {* scope of variable $title is bounded by this block const! *}
{/const}

{const [title:] = $props /} {* does not apply block scope *}
```

### Caveats

[](#caveats)

##### Syntax

[](#syntax)

- latte macros can't contain curly brackets, object syntax is achieved via colon character.

##### Syntax examples:

[](#syntax-examples)

destructuring$props$title equals`[title:]``NULL``RuntimeException``[title:]``abc``RuntimeException``[title:]``[]``NULL``[title:=Hi]``[]``"Hi"``[title:]``["title" => "Hello"]``"Hello"``[title=Hi]``[]``"Hi"``[title]``["title" => "Hello"]``NULL``[title]``["Hello"]``"Hello"``[title:]``["Hello"]``NULL`##### Priority when retrieving values of object

[](#priority-when-retrieving-values-of-object)

1. ArrayAccess interface
2. object properties
3. values from Iterator or IteratorAggregate (only available when appropriate section uses rest operator)

##### Rest operator

[](#rest-operator)

- rest operator is only possible as last token (i.e. \[title:, image:, ...item\] = $item)
- when rest operator is used, value must be array or Traversable object
- type of rest value corresponds to type of original value (stdClass when Traversable object, array otherwise)
- keys are preserved when associative array is given or at least one value is accessed via key

##### Block scope

[](#block-scope)

- previously non-defined variable will have NULL value after end-of-scope (instead of calling unset function)
- {foreach ?} has better performance then original Latte {foreach} + {const ?} inside loop ↳ {foreach ?} variable-scope is applied outside of foreach loop
- unpaired {const ? /} macro does not impose variable scope

##### Default values

[](#default-values)

- opposed to ES2016, default value can't reference previously declared tokens
- oneword string tokens in default value do not need to be quoted (similar to Latte short-array syntax)
- when default value contains function/method call, it is called only when required (provided value is NULL or key is not present)
- this extension does not check full syntactic validity of default value
- when default value with valid syntax is given, parser should handle it correctly

##### Others

[](#others)

- evaluation starts from rightmost token (like PHP7 nested list)
- requires at least PHP 5.5, fully supports PHP 7.0
- when destructuring scalar or empty value, ConstMacro\\RuntimeException is thrown (applied recursively)
- thrown exception can be suppressed with default value (i.e. `[title:, image:[src, width, height]=[]] = $item`)

### License

[](#license)

MIT. See full [license](license.md).

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

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

Total

5

Last Release

2501d ago

### Community

Maintainers

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

---

Top Contributors

[![aleskafka](https://avatars.githubusercontent.com/u/1120895?v=4)](https://github.com/aleskafka "aleskafka (17 commits)")

---

Tags

phplattedestructuringconst macroes2015-likerest operator

### Embed Badge

![Health badge](/badges/wavelo-const-macro/health.svg)

```
[![Health](https://phpackages.com/badges/wavelo-const-macro/health.svg)](https://phpackages.com/packages/wavelo-const-macro)
```

###  Alternatives

[contributte/latte

Extra contrib to nette/latte

111.5M2](/packages/contributte-latte)[nepada/form-renderer

Latte template based form renderer for Nette forms with full support for Bootstrap 3, 4 &amp; 5.

11251.0k](/packages/nepada-form-renderer)

PHPackages © 2026

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