SlimPHP - template compiler for PHP 5.4+
SlimPHP is a high performance template compiler heavily influenced by Slim, which is implemented for PHP 5.4 or greater., (*1)
Features
- high performance parser
- great readability
- contextual error reporting at compile & run time
- HTML5 mode (using the doctype html)
- combine dynamic and static tag classes
- no tag prefix
- clear & beautiful HTML output
- filters
- :php
- :cdata
- :css
- :javascript
- you even can write & add own filters throught API
Public API
$dumper = new PHPDumper([
'tabSize' => 4 // Tab size for output. Default value: 2
]);
$dumper->registerVisitor('tag', new AutotagsVisitor());
$dumper->registerFilter('javascript', new JavaScriptFilter());
$dumper->registerFilter('cdata', new CDATAFilter());
$dumper->registerFilter('php', new PHPFilter());
$dumper->registerFilter('css', new CSSFilter());
// Initialize parser & SlimPHP
$parser = new Parser(new Lexer([
'tabSize' => 2 // Tab size for input. Default value: 2
]));
$slim = new SlimPHP($parser, $dumper);
// Parse a template (either filename or content string)
echo $slim->render($template);
Syntax
Line Endings
CRLF and CR are converted to LF before parsing., (*2)
Indentation
As it's meant to be, SlimPHP supports an arbitrary length indent. Just keep the indent tree consistent throughout the slim template file., (*3)
A tag is simply a leading word:, (*4)
html
for example is converted to <html></html>
, (*5)
tags can also have ids:, (*6)
div#container
which would render <div id="container"></div>
, (*7)
how about some classes?, (*8)
div.user-details
renders <div class="user-details"></div>
, (*9)
multiple classes? and an id? sure:, (*10)
div#foo.bar.baz
renders <div id="foo" class="bar baz"></div>
, (*11)
div div div sure is annoying, how about:, (*12)
#foo
.bar
which is syntactic sugar for what we have already been doing, and outputs:, (*13)
<div id="foo"></div><div class="bar"></div>
SlimPHP has a feature, called "autotags". It's just snippets for tags. Autotags will expand to basic tags with custom attributes. For example:, (*14)
input:text
will expand to <input type="text" />
& it's the same as input( type="text" )
, but shorter.
Another examples:, (*15)
input:submit( value="Send" )
will become <input type="submit" value="Send" />
., (*16)
It also supports new HTML5 tags such as (input:email
=> <input type="email"/>
)., (*17)
Tag Text
Simply place some content after the tag:, (*18)
p wahoo!
renders <p>wahoo!</p>
., (*19)
well cool, but how about large bodies of text:, (*20)
p
| foo bar baz
rawr rawr
super cool
go Slim go
renders <p>foo bar baz rawr.....</p>
, (*21)
Actually want <?= $something ?>
for some reason? Use #{}
instead:, (*22)
p #{$something}
now we have <p><?= $something ?></p>
, (*23)
What if you want to output #{}
just as it is? You can escape the '#' character in this case:, (*24)
p \#{$notSoSpecial}
then we have <p>#{$notSoSpecial}</p>
, (*25)
Verbatim Text
The pipe tells SlimPHP to just copy the line. It essentially escapes any processing. Each following line that is indented greater than the pipe is copied over., (*26)
body
p
| This line is on the left margin.
This line will have one space in front of it.
This line will have two spaces in front of it.
And so on...
Inline html <
You can write html tags directly in SlimPHP which allows you to write your templates in a more html like style with closing tags or mix html and Slim style. The leading < works like an implicit |:, (*27)
<html>
head
title Example
<body>
- if ($articles):
- else:
table
- foreach ($articles as $a):
<tr><td>#{$a->name}</td><td>#{$a->description}</td></tr>
</body>
</html>
Nesting
ul
li one
li two
li three
Attributes
SlimPHP currently supports '(' and ')' as attribute indicator and colon(,) or space as delimitor., (*28)
a (href='/login', title='View login page' data-id="13") Login
We need to escape opening parenthesis if it comes to the very beginning character of text node, otherwise no need to escape:, (*29)
a (href='/login', title='View login page') \(Login)
a (href='/login', title='View login page') Login (with Twitter)
Boolean attributes are also supported:, (*30)
input(type="checkbox", checked)
Boolean attributes with code will only output the attribute when true
:, (*31)
input(type="checkbox", checked=someValue)
Another possibly awesome feature goes here:, (*32)
input:checkbox (#{$user->isAdmin() ? 'checked' : ''} name=is_admin)
Will render just as follows:, (*33)
<input <?= $user->isAdmin() ? 'checked' : '' ?> name="is_admin" type="checkbox" />
Note: Leading / trailing whitespace is ignored for attr pairs., (*34)
Doctypes
To add a doctype simply use doctype
followed by an optional value:, (*35)
doctype
Will output the transitional doctype, however:, (*36)
doctype html
(or simply doctype 5
), (*37)
Will output HTML5's doctype. Below are the doctypes
defined by default, which can easily be extended:, (*38)
$doctypes = array(
'xml' => '<?xml version="1.0" encoding="utf-8" ?>',
'xml ISO-8859-1' => '<?xml version="1.0" encoding="iso-8859-1" ?>',
'html' => '<!DOCTYPE html>',
'5' => '<!DOCTYPE html>',
'1.1' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
'strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
'frameset' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
'mobile' => '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">',
'basic' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">',
'transitional' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
);
SlimPHP supports sharp comments (/ COMMENT
). So SlimPHP block:, (*39)
/ SLIMPHP
- $foo = "<script>";
p
- switch ($foo):
- case 2:
p.foo= $foo
/- case 'strong':
- strong#name= $foo * 2
- case 5:
p some text
will be compiled into:, (*40)
<?php $foo = "<script>"; ?>
<p>
<?php switch ($foo): ?>
<?php case 2: ?>
<p class="foo"><?= $foo ?></p>
<?php break; ?>
<?php case 5: ?>
<p>some text</p>
<?php break; ?>
<?php endswitch; ?>
</p>
SlimPHP supports HTML comments (/! comment
). So block:, (*41)
peanutbutterjelly
/! This is the peanutbutterjelly element
| I like sandwiches!
will become:, (*42)
<peanutbutterjelly>
<!-- This is the peanutbutterjelly element -->
I like sandwiches!
</peanutbutterjelly>
As with multiline comments:, (*43)
/!
p This doesn't render...
div
h1 Because it's commented out!
that compile to:, (*44)
<!--
<p>This doesn't render...</p>
<div>
<h1>Because it's commented out!</h1>
</div>
-->
Also, SlimPHP supports IE conditional comments, so:, (*45)
/! [if IE]
a( href = 'http://www.mozilla.com/en-US/firefox/' )
h1 Get Firefox
will be parsed to:, (*46)
<!--[if IE]>
<a href="http://www.mozilla.com/en-US/firefox/">
<h1>Get Firefox</h1>
</a>
<![endif]-->
Filters
Filters are prefixed with :
, for example :javascript
or :cdata
and
pass the following block of text to an arbitrary function for processing. View the features
at the top of this document for available filters., (*47)
body
:php
$data = 40;
$data /= 2;
echo $data;
Renders:, (*48)
<body>
<?php
$data = 40;
$data /= 2;
echo $data;
?>
</body>
Code
Buffered / Non-buffered output
SlimPHP currently supports two classifications of executable code. The first
is prefixed by -
, and is not buffered:, (*49)
- var $foo = 'bar';
This can be used for conditionals, or iteration:, (*50)
- foreach ($items as $item):
p= $item
Due to SlimPHP's buffering techniques the following is valid as well:, (*51)
- if ($foo):
ul
li yay
li foo
li worked
- else:
p hey! didnt work
Second is echoed code, which is used to
echo a return value, which is prefixed by =
:, (*52)
- $foo = 'bar'
= $foo
h1= $foo
Which outputs, (*53)
<?php $foo = 'bar' ?>
<?= $foo ?>
<h1><?= $foo ?></h1>
Code blocks
Also, SlimPHP has Code Blocks, that supports basic PHP template syntax:, (*54)
ul
- while (true):
li item
Will be rendered to:, (*55)
<ul>
<?php while (true): ?>
<li>item</li>
<?php endwhile; ?>
</ul>
But don't forget about colons :
after instructions start (- if(true) :
)., (*56)
There's bunch of default ones: if
, else
, elseif
, while
, for
, foreach
, switch
, case
., (*57)
Here's another convenient way to write multiline PHP code block:, (*58)
- $user = [ \
'username' => 'clthck',
'first_name' => 'Joey',
];
This will be interpreted as:, (*59)
<?php
$user = [
'username' => 'clthck',
'first_name' => 'Joey',
];
?>