Using Modules
Since version 2.2 Ariadne has support for modules and classes in PINP templates. What this means is that you can now call functions in objects directly, so instead of:
<pinp> $object=current(get('/some/path/','system.get.phtml')); call_object($object, 'some.template.html'); </pinp>
You can now do:
<pinp> $object=current(get('/some/path/','system.get.phtml')); $object->call('some.template.html'); </pinp>
While that doesn't seem like such a big deal, you can also call other
methods, like find()
or ls()
on it. Or if the object is of class pphoto
, you
might call pphoto
specific functions like scale()
and build()
.
You still cannot define a new class in PINP, but you can use existing
objects. In addition Ariadne 2.2 also supports modules for use in PINP. A module
is class or set of classes, specifically designed to work in PINP templates.
This means that a module is safe and won't allow access to disk or the ariadne
database directly. The first module we've build is the database module, based on
PEAR DB. It works almost exactly like PEAR DB, but is in fact a wrapper around
it. You do need to have PEAR installed for it to work though. Here's an example
of a module, specifically mod_db
, in action:
<pinp> import("mod_db.php"); $db=DB::connect("mysql://user@localhost/database"); if (DB::isErroR($db)) { echo DB::errorMessage($db); } else { $db->setFetchMode(DB_FETCHMODE_OBJECT); $query="select * from MyTable"; $result=$db->query($query); if (DB::isError($result)) { echo DB::errorMessage($result); } else { // do something with the results } } </pinp>
See the documentation for PEAR DB, at the pear.php.net website for more information.
Building your own module
When creating a new module for use in pinp templates, there are a few rules
to follow. Each class you want to be able to use in a PINP template should have
a prefix of 'pinp_
'. So the class DB used above,
is translated to pinp_DB
in the module code.
In addition each method of the class (except for constructors) that is
accessible in PINP, should have a prefix of '_'. Again in the class DB user
above, the connect()
function is defined as
'_connect()
' in the module code.
Another thing to remember is that currently the 'new' operator is disabled in
PINP, so if you want to instantiate a class, you must provide a 'factory'
method, which returns the new object, just like connect()
does in the DB module.
Finally, you should always take care that methods in your module do not allow a user to circumvent the security model of Ariadne. Specifically it should never allow direct disk access, or database access, without extensive checks. In the DB case, connection id's used to connect to the database are not accessible by PINP templates, and therefor cannot be changed to access the store database directly.