NAME
RDF::Lazy::Tutorial - Tutorial to get started with RDF::Lazy
A short introduction to RDF::Lazy as RDF templating system
Three kinds of nodes
The basic object in RDF::Lazy is a node (RDF::Lazy::Node). A node represents an RDF node in an RDF graph. Every nody can be stringified to its value:
"$x" Perl syntax
[% x %] Template Toolkit syntax
{$x} Smarty syntax
There are three kinds of nodes:
- resource nodes
-
implemented by RDF::Lazy::Resource and stringified to their URI, for instance
http://example.org/foo
. - literal nodes
-
implemented by RDF::Lazy::Literal and stringified to their string value. instance
hello world
. - blank nodes
-
implemented by RDF::Lazy::Literal and stringified to
_:
followed by their internal blank node identifier, for instance_:n0815
.
Nodes have a couple of useful methods. For instance you can check the kind of a node x
with the methods is_resource
(or its alias is_uri
), is_literal
, and is_blank
:
$x->is_literal Perl syntax
[% x.is_literal %] Template Toolkit syntax
{$x->is_literal} or {$x.is_literal} Smarty syntax
The method str
is automatically called to stringify a node, so "$x"
and <$x-
str>> are equivalent. In HTML templates you can use the method esc
to HTML/XML-escape the stringified node value:
[% x.esc %] Template Toolkit syntax
{$x->esc} or {$x.esc} Smarty syntax
No nodes without graph
Each node in RDF::Lazy belongs to a particular RDF graph (RDF::Lazy). You can access a node's graph by its graph
method, if needed. Graphs have some factory methods to create new node objects:
$g->uri("http://example.org/foo") A resource node, belonging to $g
$g->literal("hello world") A literal node, belonging to $g
$g->blank("n0815") A blank node, belonging to $g
The graph methods ttl
and ttlpre
are handy o dump the whole graph in Turtle syntax. The latter wraps and escapes Turtle for HTML output:
[% g.ttlpre %]
For instance you can show the number of triples in a node's graph like this:
<p>The node's graph contains [% x.graph.size %] triples.</p>
Traversing the graph
One can traverse the RDF graph from any node. For instance, given a foaf:Person
node, one can get another resource linked via foaf:knows
:
if ( $x->type('foaf:Person') ) {
$another_person = $x->foaf_knows;
}