SemWebDev Weblog

eRDF-T with Tonic and Smarty

Sun Jun 24 2007 at 17:06

I’ve hammered together some classes that lets you use eRDF templating within the Tonic framework. The dependencies are:

The code I’ve written is:

It’s still pretty rough and alpha state, but if anyone would like to play with it, I’d love to hear comments, suggestions, bugs and bug fixes.

So, here’s a simple Blog how-to:

(I’ve bundled tonic, smarty, arc and my code + the blog tutorial code up in a package so you don’t need to cut and paste, or spend ages trying to install things.)

1. Configuration

Configure Tonic

dispatch.php

    
        require_once \'tonic/tonic.php\';

        $tonic =& new Tonic();
        $tonic->config[\'users\'][\'root\'][\'password\'] = \'mypassword\';
        $tonic->config[\'virtual\'][\'/\'] = array(\'/templates/blog-post-list.html\', array(\'class\' => \'eRdfResource\'));
        $tonic->config[\'virtual\'][\'/Posts/[^/]+\'] = array(\'/templates/blog-post.html\', array(\'class\' => \'eRdfResource\'));
        $tonic->config[\'virtual\'][\'/admin/\'] = array(\'/admin/post-list.html\', array(\'class\' => \'eRdfResource\', \'owl:sameAs\' => \'/Posts\',\'permission\'=>\'6666\'));
        $tonic->config[\'virtual\'][\'/admin/delete\'] = array(\'/admin/delete.html\', array(\'class\' => \'deleteErdfResource\',\'permission\'=>\'6666\'));
        $tonic->config[\'virtual\'][\'/admin/[^/]+\'] = array(\'/admin/post-form.html\', array(\'class\' => \'eRdfResource\', \'owl:sameAs\' => \'/Posts\',\'permission\'=>\'6666\'));

        $tonic->init();

        $tonic->load();

        $tonic->exec();
    

$tonic->config[\'users\'][\'root\'][\'password\'] = \'mypassword\'; sets the root password for the app.

$tonic->config[\'virtual\'][\'/admin/[^/]+\'] = array(\'/admin/post-form.html\', array(\'class\' => \'eRdfResource\', \'owl:sameAs\' => \'/Posts\',\'permission\'=>\'6666\')); (and the other lines like this) define the urls you want your app to use, then the template the resource representation will use at that url, then an array of metadata. We tell it to use the eRDFResource class. The owl:sameAs property, when set, will let the eRdfResource use the data from the other url for an alternative representation of the resource. In this case, we are using it to say that /admin/foo should pull in data from the graph at /Posts/foo (instead of displaying it for reading, we will display it in a form for editing). In addition, you can set a metadata property called redirect_url - on a successful posting, the user will be redirected to this location. For info about permissions and other standard Tonic stuff, see the Tonic docs.

Configure ARC

edit the database connection details in lib/arc/arc_config.php

2. Write a template for all the pages to use

templates/erdft.xhtml

    
    
        
             baseUrl}/templates/tonic.css\"/>
                {foreach from=$css item=\"style\"}baseUrl}{/if}{$style}\"/>{/foreach}
           {foreach from=$resource->ns key="ns" item="href"}
        
            {/foreach}
            
            {if $resource->owlSameAs}{/if}
        {$resource->title|escape}
    
    
        {$resource->getBody()}
    


3. Write the admin Section

admin/post-list.html


    

Admin Blog Posts

    {foreach from=\"$blogs\" item=\"blog\"}
  • {$blog.title}

    {$blog.contents}

    Comments

      {foreach from=$blog.comments item=\"blog__comment\"}
    • {$blog__comment.creator}

      {$blog__comment.contents}

    • {/foreach}
  • {/foreach}
base_href()}Posts/-change-my-name-\">New

As you can see, the html uses eRDF to define the semantics of the smarty variables in the template. As I explained in my last post, the ‘blog_comment’ syntax tells the erdft parser how to nest the arrays, so that Smarty can display it when the data comes back. We can use the Smarty functions after the pipe character | to format the values that get passed into the variables. I’ve used base64_encode to avoid having full urls in the @ids of html elements (which is invalid html), and a replace function to change the blog post url into the url to edit the post.

/admin/post-form.html

    
base_href()}\" method=\"post\" accept-charset=\"utf-8\">
Blog Post: (edit this to the uri you want the post to appear at)

The points to notice here are: - the @names use square brackets to create an associative array structure that mimics RDF/XML - it is transformed to RDF/XML on the server. - the class names that begin with f_ are names of functions that will process the value on the server. f_set_iri sets the graph iri to this value.

4. Write the frontend

/templates/blog-post-list.html (the names and location of the template files are irrelevant, as long as you tell Tonic where to find them in dispatch.php)

    

Blog Posts

    {foreach from=\"$blogs\" item=\"blog\"}
  • {$blog.title}
  • {/foreach}

/templates/blog-post.html

            

{$title}

{$blogtext}

{foreach from=\"$comments\" item=\"comment\"}
{$comment.creator}
{$comment.contents}
{/foreach}
base_href()}\" method=\"post\" accept-charset=\"utf-8\">
Comment? base_href()}\" /> base_href()}#comment-{$smarty.now}\" class=\"f_set_iri f_set_redirect\"/>

Again, much the same as before. I use a smarty {$smarty.now} to autogenerate an id number for the comment iri - which is a pretty bad idea on reflection, as a user would be able to edit that value in the input and overwrite a previous comment. If I were you, I would write a function to autogenerate the comment iri during processing. The $comment.iri_optional syntax means that the variable is optional: a post doesn’t _have_ to have comments.

5. Finished

That should be just about it. Add new namespaces to namespaces.meta - play around with the permissions and other meta data, either in the config array in dispatch, or in .meta files (eg: blog-post.html.meta is the metadata for the representation displayed by blog-post.html).

Things I\'d like to develop further

ARC semwebdev PHP Smarty eRDF REST eRDF-T