Download the PHP package dana/interpolator without Composer
On this page you can find all versions of the php package dana/interpolator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dana/interpolator
More information about dana/interpolator
Files in dana/interpolator
Package interpolator
Short Description Simple string templating
License MIT
Homepage https://github.com/okdana/interpolator
Informations about the package interpolator
Dana\Interpolator
Interpolator is a very simple string-interpolation library for PHP. It uses a syntax which combines features of Ruby, Jinja/Twig, and bash — including Jinja/Twig-style 'filters'.
Usage
Interpolator's render()
method is its most useful — it performs the actual
interpolation and then returns the result. render()
takes two arguments: the
string to be interpolated, and an array of 'fixtures' (values which might be
injected into the string). When a place-holder (%{foo}
) is encountered, that
place-holder name (foo
) will be looked up in the keys of the fixtures array,
and the associated value will be inserted into the string where the place-holder
used to be.
Filters
Each place-holder may optionally be suffixed by a pipe (|
) and one or more
single-character filter specifiers. Most of the available filters are used for
escaping and hashing — for example, the e
filter specifier passes the fixture
value through escapeshellarg()
before rendering it, and the m
specifier
hashes the value with MD5. There are also filters for case conversion (l
,
u
), white-space manipulation (t
, w
, W
), &c. Filters may be chained
together — for example, lhj
will convert the value to lower-case, then pass it
through htmlspecialchars()
, then pass it through json_encode()
(in that
exact order).
This is a list of the default filter specifiers and their behaviours:
You don't have to use the defaults though — you can override some or all of them
using the setFilter()
and setFilters()
methods:
Interpolator also supports auto filters — filters which are applied
automatically after all other filter processing has completed. This is
particularly useful for creating HTML templates, for example. Auto filters can
be surpressed using the special -
filter specifier.
Misc.
By default, when Interpolator encounters a missing fixture or a fixture that can't be converted to a string, it throws an exception. You can disable this 'strict' behaviour by passing an option to the constructor:
Note that the presence of an illegal filter specifier will continue to raise an exception even when strict mode is disabled.
To do
- Better documentation
- More tests
Licence
MIT.