Nano a simple PHP Template & Translation Engine

The Nano template and translation classes give you a basic template and translation functionality for projects where more prominent frameworks might not be needed.

Andreas Zettl | 3 minute read

This small and straightforward template engine I forked from and extended it by allowing functions too. There are PHP and Javascript versions, both working the same way. It enables you to replace text like {test} in a string, getting the data from a JSON object.

To use the PHP version, you load the object, set the template and data, and define whether empty tags should appear in the output or not.

$nano = new com\azettl\nano\template();
    {user.greeting()} {user.function(2)} {user.function('test')} {user.first_name} {user.last name}! 
    Your account is <strong>{user.account.status}</strong> 

echo $nano->render(); 

The data array, in this case, would look like the following.

    "user" => [
        "first_name" => "Anon",
        "last name" => "Ymous",
        "account" => [
            "status" => "active",
            "expires_at" => "2016-12-31"
        "greeting" => function(){
            return 'Hello';
        "function" => function($param){
            return 'Test' . $param;

Similar to the PHP call, the Javascript call would look like the following.

data = {
    user: {
        login: "demo",
        first_name: "Anon",
        "last name": "Ymous",
        account: {
            status: "active",
            expires_at: "2016-12-31"
        greeting: function(){
            return 'Hello';

var nanoString = "<p>{user.greeting()} {user.first_name} {user.last name}! Your account is <strong>{user.account.status}</strong> {user.nonexistingnode}</p>";

console.log(nano(nanoString, data)); // output: "<p>Hello Anon Ymous! Your account is <strong>active</strong> </p>"

This template engine should give you enough functionality for small use cases or whenever you do not want to use a framework.

Similar to the nano template engine, the translation version offers a small class to do simple translations via JSON files, which contain the translations.

This class is currently only available in PHP, and the only requirement is the PHP nano template engine to render the output.

The translation data is in a straightforward JSON file per language, and you store it like translations.en.json.

  "MY_KEY": "My Value",
  "MY_KEY_WITH_VARS": "My {variable} Value"

It is also possible to use some variables in the output. For this, I use the nano template engine.

The PHP call for the translation key, including the variable, is then the following.

require __DIR__ . '/vendor/autoload.php';

$oTranslation = new com\azettl\nano\translation();

echo $oTranslation->translate('MY_KEY_WITH_VARS', 'en', ['variable' => 'test']);

This sample returns "My test Value".

🔙 Back to the article overview | Share on Twitter

      FontExplorer X - Makes Everyone an Expert

Advertisment: These links are affiliate (commission) links. If you click on such a link and buy something on that website, I get a commission. For you, the price does not change.