Tumblr-like Template Syntax PHP Library

DEPRECATED: This post has been marked as deprecated and may no longer contain industry best-practices.

I'm working on a PHP template library to provide a Tumblr-like syntax which can be digested by PHP apps. Here are my notes so far. Note that this class is not ready yet, but once it is, I'll host it at my GitHub account (as well as integrate it into my SleekMVC project).

This language will be translated into raw PHP and cached so that if your website is making use of APC eAccelerator, the result will be cached in RAM (making it as quick as normal PHP).

Another feature is that it can be configured for three different modes. The first is where the template is compiled at every execution. The second is where the template is checked to see if it was modified after the cache, if so recompile, if not use the cache. The third is where the template files are never checked and the cached PHP is always used (a function will exist for clearing out the compiled files). These settings could be configured on a per-environment (development vs production) basis, allowing your local site to be slow but update often and your production to be fast and updated when you update your website.

The only real differences so far is that we use {block:*} for enabling/disabling sections, and {repeat:*} for looping over sections. Tumblr uses block code for both operations and doesn't have a repeat code.

<!-- PHP CODE -->
<?php
// Procedural version
$data['Text'] = TRUE;
$data['WebsiteName'] = "Tom's Cool Site";

$data['Posts'][0] = array(
    'Title'     => 'First Post',
    'Id'        => 1,
    'Subtitle'  => 'This post is cool',
);
$data['Posts'][1] = array(
    'Title'     => 'Second Post',
    'Id'        => 2,
    'Subtitle'  => 'This post is super cool',
);
View::render($data, 'view/file/path');

// OOP version
$view = new View('view/file/path');
$view->Text = TRUE;
$view->WebsiteName = "Tom's Cool Site";
$view->Post = array(
    array(
        'Title'     => 'First Post',
        'Id'        => 1,
        'Subtitle'  => 'This post is cool'
    ),
    array(
        'Title'     => 'Second Post',
        'Id'        => 2,
        'Subtitle'  => 'This post is super cool'
    ),
);
$view->render();
?>

<!-- TEMPLATE CODE -->
<h1>{WebsiteName}</h1>
{block:Text}
<ul>
    {repeat:Post}
    <li class="post text">
        <h3><a href="/post/{Post.Id}">{Post.Title} - {Post.Subtitle}</a></h3>
    </li>
    {/repeat:Post}
</ul>
{/block:Text}

<!-- RENDERED HTML -->
<h1>Tom's Cool Site</h1>
<ul>
    <li class="post text">
        <h3><a href="/posts/1">First Post - This post is cool</a></h3>
    </li>
    <li class="post text">
        <h3><a href="/posts/2">Second Post - This post is super cool</a></h3>
    </li>
</ul>

<!-- CACHED PHP FILE -->
<h1><?=$view->WebsiteName?></h1>
<?php if ($view->Text) { ?>
<ul>
<?php foreach($view->Post AS $Post) { ?>
    <li class="post text">
        <h3><a href="/posts/<?=$Post['Id']?>"><?=$Post['Title']?> - <?=$Post['Subtitle']?></a></h3>
    </li>
<?php } // foreach Post ?>
</ul>
<?php } // if Text ?>
Tags: #php
Thomas Hunter II Avatar

Thomas is the author of Advanced Microservices and is a prolific public speaker with a passion for reducing complex problems into simple language and diagrams. His career includes working at Fortune 50's in the Midwest, co-founding a successful startup, and everything in between.