2017 © Pedro Peláez
 

library nano

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

image

techworker/nano

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

  • Thursday, November 13, 1913
  • by BenjaminAnsbach
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

nano

Build Status Scrutinizer Quality Score Code Coverage PHP >= 5.4, (*1)

A code readability promoting, placeholder oriented, less defective sprintf functionality, inspired by https://github.com/trix/nano. 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., (*2)

About

This class tries to avoid manual string concenation by replacing code snippets like the following:, (*3)

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.., (*4)

$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

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:, (*5)

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

And installing the package:, (*6)

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., (*7)

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., (*8)

Lets start by explaining the replacement:, (*9)

  • 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} €

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


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

Static call

The simplest and most non-intrusive method:, (*11)

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

Object-Oriented usage

Short and simple, some examples for the usage., (*12)

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
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
echo new Techworker\Nano("Hello {name:firstname} {name:lastname}", [
    'name' => [
        'firstname' => 'Benjamin',
        'lastname' => 'Ansbach'
    ]
]);
// outputs: Hello Benjamin Ansbach
Simple values
echo (new Techworker\Nano())->template("Hello {name}")->value("name", "Benjamin");
// outputs: Hello Benjamin
Default values
echo (new Techworker\Nano())->template("Hello {name}")->default("Stranger");
// outputs: Hello Stranger
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 = <<<EOT
Name: {company:name}
Employees: {company:employees:0} and {company:employees:1}
Income: {company:income:amount|%.2f}$
Employee of the Month: {company:empofmonth:name} in {company:empofmonth:year}/{company:empofmonth:month|%02d}
EOT;

echo (new Techworker\Nano($company, $data));

// outputs:
// Name: Techworker
// Employees: Benjamin Ansbach and Techworker
// Income: 0.00$
// Employee of the Month: Benjamin Ansbach in 2013/02

Instance Reusage Example
$data = [
    ['firstname' => '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

The Versions

13/11 1913

dev-master

9999999-dev http://www.techworker.de/

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

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

framework