PHPackages                             averay/tokenised-strings - 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. averay/tokenised-strings

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

averay/tokenised-strings
========================

A lightweight system for rendering tokens in templated strings.

v1.0.0(2y ago)0260MITPHPPHP &gt;=8.2CI passing

Since Feb 23Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/adamaveray/tokenised-strings)[ Packagist](https://packagist.org/packages/averay/tokenised-strings)[ RSS](/packages/averay-tokenised-strings/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

Tokenised Strings
=================

[](#tokenised-strings)

A lightweight system for rendering tokens in templated strings using a familiar syntax inspired by Jinja or Twig.

Motivation
----------

[](#motivation)

The library aims to provide a more flexible substitute for PHP’s `str_replace` function.

Manual string concatenation does not allow templating, and simple `str_replace` substitutions offer limited functionality and require repetition. Meanwhile, a full templating engine such as Twig provides a lot of power at the cost of significantly increased complexity.

This library aims to fill a middle ground between the two options, allowing templates to be defined with a standard syntax and rendered throughout a system without needing to manage a full templating engine. The library is intended for rendering short tokens defined separately from where they are rendered, such as strings stored in configuration files or a database.

Syntax
------

[](#syntax)

The default configuration delimits tokens using double curly braces (e.g. `{{ token_name }}`). Nested values can be accessed using periods (e.g. `{{ parent.child }}`. Modifiers a.k.a. filters can be applied using pipes (e.g. `{{ value | modifier }}`) and multiple can be applied. All whitespace between components is optional. The syntax is completely customisable (see below).

```
Report ID: {{ id }}
Name: {{ user.name }}
Profile: https://www.example.com/user?id={{ user.id | urlencode }}

```

No in-template logic such as assignments, conditions, or loops are supported (i.e. any functionality Twig’s `{% ... %}` tags provide), nor are comments.

Usage
-----

[](#usage)

The primary class provides a method for rendering templated strings, and two additional helper methods for rendering URLs and HTML:

```
