Quantcast
Channel: Lynk IT Blog » Template
Viewing all articles
Browse latest Browse all 2

CMS / Templating language a bit like smarty

$
0
0

Ive been developing this for a few months now so I really ought to write some notes on how to actually use it. Having torn apart smarty quite a bit in the past, I had the idea to create a HTML control language that was much simpler to use, and a fraction of the size, albeit with fewer complex features. A lighter alternative to smarty.

Our CMS has been built with the principles of optimisation and tight, benchmarked code. It has been reduced to about a dozen highly optimised class files built around a central "Registry" class, and that includes the ecommerce processor, form processor, content and blocks and the new TPL compiler, so it simply wouldnt make any sense to bolt on the complete Smarty engine into our CMS and effectively quadruple the size of the CMS and introduce sloppy code integration.

Whats it for?

I originally designed the compiler for manipulating blocks and elements within a web page, for example you may have a call-to-action box, or a form, or a menu such as a sitemap that you want to embed within a page or several pages. Built-in commands achieve this easily...

Commands

There are about a dozen "commands" available such as "block", "form", "menu", "link", "dl", "news" etc. which allow you to embed elements within your page such as repeating content blocks or images, trackable external links (useful if you wish to dabble in selling a pay-per-click service from your site or affiliate marketing), downloads (includes controls to limit the number of downloads per user, make files private etc.) and several more.

<h1>Welcome to our website</h1>
{block=banner}
{menu=sitemap}
{form=contact}
...

The three elements above correspond to a graphic banner, a navigation menu and a contact form. These are all defined as elements of "blocks" within the CMS and can be included in any other page by using the above methods. These are known as commands. In line 1, you are commanding the compiler to fetch a block called "banner" and display this within your page.

Variables and Properties

For the most basic functioning, there are some system properties that are available system wide, such as date, IP and user data:

The current year is {$year}
Your IP Address is {$IP}

Would output "The current year is 2010 Your IP address is xxx.xxx.xx.xx" (Or whatever year it happens to be now, at the time of writing it was 2010 and unfortunately wordpress wont convert this for me next year, and the xxx would obviously be replaced by your IP address).

Some server globals are automatically available in the TPL and can be accesed using the following syntax:

{$_SERVER.REQUEST_URI}
{$_GET.some_get_variable}
{$_POST.some_post_variable}

You can of course create your own variables, either within the accompanying PHP files, or by using the "assign" command. More on this here.

Programming constructs

IF ELSEIF ELSE ENDIF

It supports IF constructs, so:

{if $year > 2012}
    How was the London olympics?
{elseif $year == 2012}
    London olympics this year
{else}
    London olympics is coming up...
{/if}

Im sure you can work out what happens here.

FOREACH

You can use FOREACH constructs within the TPL or CMS, this can be useful for generating tables or iterating over arrays and is particularly useful in the ecommerce module for listing categories and products - study the catalogue.tpl file for a detailed example of this in action:
{foreach $myarray as $myvalue}
{$myvalue}
{/foreach}

You can nest IF and FOREACH:

{foreach $table as $tablerow}
    {if $data}
        <tr>
            {foreach $data as $datum}
                <td>{$datum}</td>
            {/foreach}
        </tr>
    {/if}
{/foreach}

Multi-dimensional arrays

The compiler accepts multi-dimension arrays. To access the values of the inner array, you must use the dot notation. Assume we have defined the following array in PHP:

$theme->assign("myarray", array(
    'person1'=>array(
        'name'=>'John Doe',
        'phone'=>'0123456789',
        'age'=>'38'
    ), 'person2'=>array(
        'name'=>'Jane Doe',
        'phone'=>'987654321',
        'age'=>'41'
    )
));

We can now process this array within the CMS/TPL:

{foreach $myarray as $person=>$details}
    Viewing {$person} details:<br />
    Name: {$details.name}<br />
    Telephone: {$details.phone}<br />
    Age: {$details.age}<br />
{/foreach}

This would output:

Viewing person1 details:
Name: John Doe
Telephone: 0123456789
Age: 38

Viewing person2 details:
Name: Jane Doe
Telephone: 987654321
Age: 41

Functions

You can call PHP functions from within the CMS. Sounds a bit dangerous? Well, the PHP functions have to exist as user created functions in the Functions class so its not really that dangerous. Many general use functions exist, but you can add more by extending the Functions class. For now I'll just demonstrate some of the existing ones.

The way to call a function is with the func keyword, followed by the command (with parameters in brackets):

{func mencode("me@domain.com")}

This function will output a browser encoded email address. The workings of it are defined in the class.func.php class as follows:

public static function mencode($address, $alt) {
    $code='<a href="mailto:';
    for($i=0;$i < strlen($address);$i++) {
        $code .= '%'  .dechex(ord($address{$i}));
    }
    $code.='">'.$alt.'</a>';
    return $code;
}

For things like news, blog or product descriptions, its useful to be able to show just part of the message or description, so you can use the truncate function for this:

{func truncate($productdescription, 100)}

This will display the first 100 characters of the description, truncated at the nearest space. Other functions include:

{func getRealDate($date, false, "F jS, Y")}
{func redirect($url)}
{func getPageCodeFromURI()}
...

The first of these will output the data provided in a human friendly format. If no parameters are passed, it takes the current date. The second parameter is true/false whether to display the time, and the third parameter is the PHP date format to use.

There are other functions such as checkEmailAddress($email) that may be of limited use to advanced users. The most useful of this will probably be the math function:

Math function

{func math(2 * 4.4 / 3.2 - 3 + 28)}

In this, you can use any mathematical operator including mod (%), + - / *. You cannot use ^ for exponents however. The math function respects BODMAS and performs exactly as you would expect it to directly within PHP. This is used in the ecommerce module for calculating shopping cart line totals for example:

Line Total: {func math($line.qty * $line.price)}

One final note, there is a facility to format the number inline rather than subsequently using {func number_format($val,2)}. Simply pass an Integer to math as the second parameter:

{func math($line.qty * $line.price , 2)}

This would output for eg: "3.00" rather than "3", or "5.24" instead of "5.24212364".


Viewing all articles
Browse latest Browse all 2

Trending Articles