PHPackages                             techworker/nano - 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. techworker/nano

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

techworker/nano
===============

Simple, small and fast String templating functionality ported from the original https://github.com/trix/nano.

012PHP

Since Nov 13Pushed 12y ago1 watchersCompare

[ Source](https://github.com/Techworker/nano)[ Packagist](https://packagist.org/packages/techworker/nano)[ RSS](/packages/techworker-nano/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

nano
====

[](#nano)

[![Build Status](https://camo.githubusercontent.com/cf60b31e9501562500a406db4a2a65a66bcc22b68422f26906cd68047db02af6/68747470733a2f2f7472617669732d63692e6f72672f54656368776f726b65722f6e616e6f2e706e67)](https://travis-ci.org/Techworker/nano)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/9133cd3a5142644bdedda69e4aa7d39d6629c3b8863a3e8640b9852d19028600/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f54656368776f726b65722f6e616e6f2f6261646765732f7175616c6974792d73636f72652e706e673f733d62363164376233666636386361643965646530366337353734313737623637323435386638306139)](https://scrutinizer-ci.com/g/Techworker/nano/)[![Code Coverage](https://camo.githubusercontent.com/0ec1212f72f6fa7f706dd94f59a1f151198c4e37b4983dd3841fde3f1b8621a9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f54656368776f726b65722f6e616e6f2f6261646765732f636f7665726167652e706e673f733d38663062303032656430386133393264396337613865613837316564643336373832356265323661)](https://scrutinizer-ci.com/g/Techworker/nano/)[![PHP >= 5.4](https://camo.githubusercontent.com/156bf26e251d140db8bfb81cca52db05d712a19ba773806aa30fd0d55363cc04/687474703a2f2f696d672e736869656c64732e696f2f7068702f2533453d352e342e706e673f636f6c6f723d726564)](https://camo.githubusercontent.com/156bf26e251d140db8bfb81cca52db05d712a19ba773806aa30fd0d55363cc04/687474703a2f2f696d672e736869656c64732e696f2f7068702f2533453d352e342e706e673f636f6c6f723d726564)

A code readability promoting, placeholder oriented, less defective sprintf functionality, inspired by . It is, of course, up to you if you want accept the overhead compared to the PHP core functionality. I use it to format logging messages or Exceptions, which means mostly speed-independant code-parts. Try it out, it is small, well documented and tested.

### About

[](#about)

This class tries to avoid manual string concenation by replacing code snippets like the following:

```
echo "The Parameter " . $param . " with value " . $value . " in method " .
     __CLASS__ . ":" . __FUNCTION__ . " was simply wrong.";
```

```
echo sprintf("The Parameter %s with value %s in method %s:%s was simply wrong.",
    $param, $value, __CLASS__, __METHOD__
);
```

..with..

```
$message = "The Parameter {param} with value {value} in method {class}:{method} was simply wrong";
echo (new Nano($message))->data([
    'param' => $param,
    'value' => $value,
    'method' => __METHOD__,
    'class' => __CLASS__
]);
```

### Installation

[](#installation)

You have multiple ways to use the `Techworker\nano` class, but at first you have to install it. At best via composerby adding it the require list:

```
{
    "require": {
        "techworker/nano": "dev-master"
    }
}
```

And installing the package:

```
composer install
```

After that you are ready to use `Techworker\Nano`, or you can install the package manually by downloading it (see download links) and installing it manually.

### Documentation

[](#documentation)

You have a lot of possibilities to use the `Techworker\Nano` class. We are going through some examples here but I'd like to invite you to the phpunit\\tests directory to see all examples.

Lets start by explaining the replacement:

- Each placeholder should be within curly brackets, like `{my_placeholder}`.
- You can access nested data by defining the access tree with placeholders divided by a colon, eg `{my_root_element:level1_element:level2_element}`.
- You can assign additional printf and sprintf formatting options, eg `{price|%.2f} &euro;`

To start, add the following `use` Statement to your code to access `Nano` directly or use the complete `\Techworker\Nano` namespace\\class definition:

```
echo Techworker\Nano::tpl("test");

use Techworker\Nano as Nano;
echo Nano::tpl("test");
```

#### Static call

[](#static-call)

The simplest and most non-intrusive method:

```
echo Techworker\Nano::tpl("Agent {number}", array("number" => 7));
// outputs: Agent 7
```

#### Object-Oriented usage

[](#object-oriented-usage)

Short and simple, some examples for the usage.

```
echo (new Techworker\Nano("Agent {number}"))->data(array("number" => 7));
// outputs: Agent 7
echo (new Techworker\Nano("Agent {number|%03d}"))->data(array("number" => 7));
// outputs: Agent 007
```

##### Object Getter

[](#object-getter)

```
try{
    throw new \Exception("Exception Message", 110);
} catch(\Exception $ex) {
    echo new Techworker\Nano("Exception {message} and {code} thrown", $ex);
}
// outputs: Exception Exception Message and 110 thrown
```

##### Deep Structures

[](#deep-structures)

```
echo new Techworker\Nano("Hello {name:firstname} {name:lastname}", [
    'name' => [
        'firstname' => 'Benjamin',
        'lastname' => 'Ansbach'
    ]
]);
// outputs: Hello Benjamin Ansbach
```

##### Simple values

[](#simple-values)

```
echo (new Techworker\Nano())->template("Hello {name}")->value("name", "Benjamin");
// outputs: Hello Benjamin
```

##### Default values

[](#default-values)

```
echo (new Techworker\Nano())->template("Hello {name}")->default("Stranger");
// outputs: Hello Stranger
```

##### Complex and Nested!

[](#complex-and-nested)

```
// Complex Example
$empOfTheMonth = new stdClass();
$empOfTheMonth->name = 'Benjamin Ansbach';
$empOfTheMonth->month = 2;
$empOfTheMonth->year = 2013;

class Income
{
    public function getAmount()
    {
        return 0;
    }
}

$data = [
    'company' => [
        'name' => 'Techworker',
        'employees' => ['Benjamin Ansbach', 'Techworker'],
        'income' => new Income(),
        'empofmonth' => $empOfTheMonth
    ]
];

$company =  'Benjamin', 'lastname' => 'Ansbach'],
    ['firstname' => 'The',      'lastname' => 'Techworker']
];

$nano = new Techworker\Nano("{firstname} {lastname}");
foreach($data as $item) {
    echo $nano->data($item) . "\n";
}

// outputs
// Benjamin Ansbach
// The Techworker
```

###  Health Score

19

—

LowBetter than 9% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

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/56e0f4be9fdda71be24a095e3740a862e5d0870e3dbef4d298b0d9b04ef47abd?d=identicon)[BenjaminAnsbach](/maintainers/BenjaminAnsbach)

### Embed Badge

![Health badge](/badges/techworker-nano/health.svg)

```
[![Health](https://phpackages.com/badges/techworker-nano/health.svg)](https://phpackages.com/packages/techworker-nano)
```

###  Alternatives

[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3851.2M](/packages/limenius-react-bundle)[area17/laravel-auto-head-tags

Laravel Auto Head Tags helps you build the list of head elements for your app

4616.1k](/packages/area17-laravel-auto-head-tags)[jelix/wikirenderer

WikiRenderer is a library to generate HTML or anything else from wiki content.

1712.2k1](/packages/jelix-wikirenderer)[webkinder/sproutset

A Composer package for handling responsive images in Roots Bedrock + Sage + Blade projects.

282.2k](/packages/webkinder-sproutset)

PHPackages © 2026

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