Welcome to my little website about mplate.
Introduction
mplate is a new template engine for PHP. Its purpose is to help programmers separate application logic from presentation logic by making it easier and more fun to write PHP-embedded HTML.
mplate has in many ways been inspired by Smarty, a PHP template engine that used to be very popular but is now often considered obsolete. Today, most programmers agree that Smarty creates about as many problems as it solves, thus providing extra overhead without much use.
The goal of mplate is to make writing the presentation layer of your website, often called the view component, easier and more beautiful without requiring you to learn yet another language or imposing any barriers that limit your flexibility.
In short, mplate provides you with some syntactical sugar for writing views less sloppily, without any penalties.
Design principles
- Clean syntax
- Favour flexibility
- Use PHP notation wherever sensible, it’s a good language.
- No bloat
- Accept no speed loss (compared to plain PHP views)
- Encourage security
Example
Without further ado, here’s a simple example:
<html>
<head>
<title>mplate works!</title>
<script type="text/javascript">
//mplate does not break on { and } in scripts and styles:
function setApple()
{
//instead, an mplate tag starts with "#{" here
var apple = #{$apple};
}
</script>
</head>
<body>
<!-- PHP code is permitted, --!>
<?php array_unshift($numbers, 0); ?>
<!--
but discouraged, because mplate has all you need in
the box
--!>
<ul>
{foreach($numbers as $i => $number)}
<li>{$number}</li>
{foreachelse}
<li><em>no numbers!</em></li>
{/foreach}
<ul>
<!--
tag libraries for extensibility and third party
library support
--!>
{zend:formText name="moo" value=1}
</body>
</html>
The above code is an mplate template, in which you define what a web page looks like. $numbers and $apple are PHP variabes that should be passed to mplate by your PHP application before it asks mplate to render the page.
If the page would be called mypage.tpl, you would invoke it from a normal PHP page as follows:
<?php
$mplate = new Mplate();
$mplate->vars['numbers'] = array(6,5,4,3);
$mplate->vars['apple'] = 'banana';
$mplate->display('mypage.tpl')?
?>
Alternatively, you can even run an mplate view right in the scope of your PHP code:
<?php
$mplate = new Mplate();
$numbers = array(6,5,4,3);
$apple = 'banana';
require $mplate->viewfile('mypage.tpl');
//all variables set are available to mypage.tpl now
?>
Features
- Doesn’t Suck™
- Fast:
- mplate compiles template files to plain PHP code
- All cool features and magic functionality are performed in compile time, which happens only once every time you change a template
- Encourages you to also do many things on compile time, making your scripts faster
- If you’re familiar with Smarty, you’ll feel right at home
- If you hate Smarty, you’ll hate mplate less
- A template language which isn’t really a language at all:
- Supports all common PHP constructs just the way you like them
- Many handy built-in template functions that make your life easier
- Plain PHP code permitted, inside template expressions as well as complete PHP code snippets
- In fact, 99% of all pure PHP views will parse as mplate templates
- Helps combat evil user submitted content: Escapes output by default
- Does not try to do more than what it’s supposed to do: It’s a template engine, no more, no less.
- In fact, mplate loves frameworks (CakePHP, Zend, etcetera) like bunnies love carrots
- Zero-configuration extensibility with JSP Tag Library-style plugin architecture:
- Plugin interface designed to make writing and packaging plugins as easy as possible
- Easy to use support for compiler functions, which generate PHP/HTML code snippets instead of being called every pageview
- Namespaced functions per plugin to keep functions nicely grouped together
- Designed to make using third party view libraries or framework “helpers” dead simple
Download
The releases are listed here
What do you think?
I’ve spent a fair amount of time working on this thing to get most of the suck out of it, and now is the point where I’d like to ask the PHP community what they think about mplate. So, what do you think?