Nano a simple PHP Template & Translation Engine

❗NEW❗ Image color palette picker

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

This small and straightforward template engine I forked from https://github.com/trix/nano 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();
$nano->setTemplate(
  "
    {user.greeting()} {user.function(2)} {user.function('test')} {user.first_name} {user.last name}! 
    Your account is {user.account.status} 
    {user.nonexistingnode}
  "
);
$nano->setData($aData);
$nano->setShowEmpty(true);

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 = "{user.greeting()} {user.first_name} {user.last name}! Your account is {user.account.status} {user.nonexistingnode}";

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

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();
$oTranslation->setBasePath('vendor/azettl/php-nano-translation/tests/translations/');
$oTranslation->setFileNamePattern('test.%s.json');

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

This sample returns "My test Value".

Converting Colors

Converting Colors allows you to convert between 17 different color formats like RGB, CMYK, HSV, HSL, CIELab, Android, Decimal, and YUV.

Made with πŸ’˜, 🍺 and 🍫. If you have questions send me an email to [email protected]!

Privacy Policy  ·  Imprint

Menu

Support