
JASPer 1.0 -- download now size: 8533 bytes
JASPer is a fairly lightweight, simple class for parsing templates written using the JASPer syntax.
Simply save the class file JASPer.php somewhere convenient then include it into your script where needed. See the file test.php in the download package for a quick and dirty example of useage.
In your x/html templates JASPer will treat any strings
beginning with a $ as a placeholder or variable and
replace it with the value of the PHP variable of same name.
One condition is that the $ must be followed by a letter or _
NOT a number, so $var, $_var and $v1 are valid but $1var is not and
will be ignored.
JASPer supports a few special tags that cause JASPer to perform special operations. All tags must be the only entry on a line in your html templates, that is you can't mix regular html with parser tags on the same line as once the parser tag is found the rest of the line is ignored. This may be fixed in the future.
The JASPer tags are:
<if> blocks:
<if condition="some_condition">
some html
</if>
some_condition is evaluated, if true all text inside the <if>
block is parsed and any <else> block is ignored, if false the
<if> block is skipped and a following <else> block is parsed if
it exists. some_condition must be written as a valid
PHP expression and may include PHP functions e.g
<if condition="filesize($filename) > 0)">
<else> blocks:
<if condition="some_condition">
some html
</if>
<else>
some html
</else>
Text inside <else> blocks are only parsed if the preceeding <if>
block evaluated to false.
<include> tags:
<include> tags tell JASPer to fetch the file named in the src
attribute and parse it using the parse type in the parsetype
attribute.
Parse types are:
NO_PARSE - file contents is simply returned as string, no parsing performed.
SIMPLE_PARSE - file parsed with all variables replaced by value.
EXTENDED_PARSE - file parsed as in SIMPLE_PARSE but also parses special tags.
The <include> tag is replaced by the resulting output.
<repeat> tags:
<repeat for="array_name">
some html
</repeat>
The <repeat> tag tells JASPer to repeatedly parse the contents of
the block for each array found in array_name. array_name is the
name of an array of arrays that contains name => value pairs e.g.
$people = array(
array('fname' => 'Fred', 'lname' => 'Smith'),
array('fname' => 'Barry', 'lname' => 'Jones'),
array('fname' => 'Alice', 'lname' => 'Brown')
);
Given the above example JASPer would repeat the parsing for each of
the arrays found in $people, replacing the variables in the template's
<repeat> block with the values found in the current array, great for
building tables. The above array could be used to build a table as such:
<table border="1">
<tr><th>First Name</th><th>Last Name</th></tr>
<repeat for='people'>
<tr><td>$fname</td><td>$lname</td></tr>
</repeat>
</table>
Which would create the following HTML:
<table border="1">
<tr><th>First Name</th><th>Last Name</th></tr>
<tr><td>Fred</td><td>Smith</td></tr>
<tr><td>Barry</td><td>Jones</td></tr>
<tr><td>Alice</td><td>Brown</td></tr>
</table>
Which would create this table:
| First Name | Last Name |
|---|---|
| Fred | Smith |
| Barry | Jones |
| Alice | Brown |
Well, sort of. JASPer can cache files it has already parsed and output
the cached version rather than re-parsing, but the cached files are only
stored in memory (in an array in the JASPer object) and are lost once
the script finishes execution or the JASPer object is destroyed. So at
the moment JASPer does not support caching that is persistant between
requests, unless of course you add your enitre JASPer object to $_SESSION to
keep it persistant, then the cached files will be saved along with all
the other data in your JASPer object. This can be done as such:
if ($_SESSION['jasper'])
$obj =& $_SESSION['jasper'];
else
{
$obj = new JASPer;
$_SESSION['jasper'] =& ;
}
It is on the TODO list to add filesystem and shared memory caching if I get around to it or get enough requests.
YES! Though I do claim copyright and request that any notices to such and to my authorship stay in place. And if you like it spread the word and let me know at brad@ibiscode.com
If you feel you have something to add please contact me.
If you have any questions regarding use of JASPer or wish to report bugs please email the developer.