Small Axe Basics

Use the source tab below to compare the rendered template to the source to use how you can use Small Axe Templating.


A test of Small Axe Templating

WELCOME TO SMALL AXE TEMPLATING

This is a test of Small Axe Templating. It was written by Adam Scheinberg. It is written in PHP.

The current date is July 29, 2021, which is often written as 2021-07-29 and corresponds to unix timestamp 1627614224, which can also be expressed as 1627614224. You can use arguments found at php.net/date.

It has basic skills, such as escaping text: <i><b>This should be escaped text</b></i>

It supports simple string functions, such as str_rot13 - Fznyy Nkr Grzcyngvat - and juggling case - small axe templating.

It supports custom functions: "Small Axe Templating-SMALL AXE TEMPLATING".

It also can chain functions, such as uppercasing and str_rot13: FZNYY NKR GRZCYNGVAT.

Here is a function that was dynamically added, sha1: 882d0edf97c3d20bdb3547dc7f9a68f62f99fe03

Here are example of a dynamic placeholders 1) a unique identifier: 61036c10bc64a 2) a datetime: 2021-07-29 23:03:44 and 3) a datetime in UTC: 2021-07-30 3:03:44.

You can use substr, here's substr($str,3,10): quick bro. A long string can be quickly shortened using the shortcut function ellipsis: The quick brown fo…

Variables that end with an exclamation point will be removed if they are not matched - for example, it should be blank between the quotes "". But those unmatched will remain untouched, like {{so}}.

<?php

# require the templating library 
require '../source/smallaxe-templating.php'

# first, you instantiate a Small Axe object
$T = new Smallaxe\smallaxe_template();

# set the template path
$T->set_template_path(__DIR__.'/../templates/');

# You can feed the Small Axe object a memcached resource for storing uncompiled templates
# a memory cache system is optional
$memcache = new Memcached();
$memcache->addServer("localhost"11211);
$T->enable_cache($memcache); 

# you can also add functions that the template can parse
# currently, functions are only supported if they can take the value as an argument and return it as a string
# only functions YOU explicitly allow will work in your templates
# for example, this will add the "sha1()" and "str_rot13" functions  
$T->extend(['sha1''str_rot13']); 

# the extend() method will also accept your custom functions
function custom_function($string) { 
    return 
'&quot;<i>'.ucwords(strtolower($string))."-".strtoupper($string).'&quot;</i>'
}
$T->extend(['custom_function']); 

# define our variables
$vars = [
    
'software_title'     => "Small Axe Templating",
    
"author"              => "adam scheinberg"
    
'year'                  => date('Y'),
    
'language'             => "php",
    
'test_text'             => "<i><b>This should be escaped text</b></i>",
    
'random'            => "This is a random string",
    
'longstring'        => "The quick brown fox jumped over the lazy dog"
];

# load template by passing either the file in the templates folder OR a hard coded path 
# when searching the template directory, if no file extension is provided and no file is matched without an extension, 
# it will assume ".tmpl" or ".tpl"
$template     $T->load_template("demo1");

# compile the template 
$html         $T->render($template,$vars); 

# echo the template 
echo $html
<html>
    <head>
        <title>A test of {{software_title|ucwords}}</title>
    </head>
    <body>
        <h1>WELCOME TO {{software_title|upper}}</h1>
        <p>This is a test of {{software_title}}.  It was written by {{author|ucwords}}. It is written in {{language|upper}}.</p>
        <p>The current date is {{date|F d, Y}}, which is often written as {{date|Y-m-d}} and corresponds to unix timestamp {{date|U}}, which can also be expressed as [[timestamp]]. You can use arguments found at <a href="https://php.net/date">php.net/date</a>.</p>
        <p>It has basic skills, such as escaping text: <code>{{test_text|e}}</code></p>
        <p>It supports simple string functions, such as str_rot13 - {{software_title|str_rot13}} - and juggling case - {{software_title|lower}}. </p>
        {* this is a Smarty style comment *}
        <p>It supports custom functions: {{software_title|custom_function}}. </p>
        /* 
        this 
        is 
        a 
        C style 
        comment */
        <p>It also can chain functions, such as uppercasing and str_rot13: {{software_title|upper|str_rot13}}. </p>
        <p>Here is a function that was dynamically added, sha1: {{random|sha1}}</p>
        <p>Here are example of a dynamic placeholders 1) a unique identifier: [[uniqid]] 2) a datetime: [[datetime]] and 3) a datetime in UTC: [[utcdatetime]].</p>
        <p>You can use <b>substr</b>, here's substr($str,3,10): <i>{{longstring|substr:3:10}}</i>. A long string can be quickly shortened using the shortcut function <b>ellipsis</b>: <i>{{longstring|ellipsis:18}}</i></p>
        
        <p>Variables that end with an exclamation point will be removed if they are not matched - for example, it should be blank between the quotes "{{test!}}". But those unmatched will remain untouched, like {{so}}. </p> 
    </body>
</html>