RDF2h manual

This is a step by step demonstration and occasional explanation of the RDF2h features. You can edit all examples in the browser and executed scripts again to see the effects of changes in the code.

Defining a renderer

The following is a renderer graph that defines a renderer for rdfs:Resource Applied to the following data graph: return new RDF2h(renderers).render(data, "http://example.org/");

Property paths

The previous example contained a mustache renderer following a property ( dc:title )to its value. The renderer defined in the following graph follows the foaf:knows property backwards and then the foaf:knows property forwards. Applied to the following data graph: return new RDF2h(renderers).render(data, "http://example.org/a");

Loops and conditions

We can use mustache syntax to iterate over the different values of a property. The same also works with inverse properties. The following renderer will render all friends of a Person. Applied to the following data graph: return new RDF2h(renderers).render(data, "http://example.org/a");

Additional features

Additionally to the possibility of looping the values of a property RDF2h supporrts prefixing the property path with + , the resulting expression evaluates to true when the path exists and to false otherwise. Note that this doesn't change the evaluation context, so that the value of . doesn't change and the property needs to be accesses like outside the block. It may be used in combination with a loop over the property.
Example:

Delegating back to renderer

By invoking :render a resource is given back to the rendering engine for rendering. Typically :render is invoked between triple-braces to avoid double escaping. Applied to the following data graph: return new RDF2h(renderers).render(data, "http://example.org/a");

Selection of renderer

With the following renderers there's a more specific renderer for foaf:OnlineChatAccount that can be used instead of the more generic renderer for foaf:OnlineAccount . To allow RDF2h to know which type is more specific we also added the statement foaf:OnlineChatAccount rdfs:subClassOf foaf:OnlineAccount . return new RDF2h(renderers).render(data, "http://example.org/a");

Javascript instead of mustache

Mustache provides a very simple way to render resources. However occasionally its possibilities just arent enough. As an alternative you can use the r2h:javaScript property which points to some js code writing that returns the rendering result. The code has access to the following variables: n , context , $rdf and to the function render(graphNode, context) . Applied to the following data graph: return new RDF2h(renderers).render(data, "http://example.org/a"); Note the recursive invocation of the renderer. This would lead to an infinite loop if one of the persons known by Alice would know her back. A way to avoid this would be to change context by invoking the render function with a second argument and to provide a renderer that doesn't further expands the value of the foaf:knows property.

Have a look at the Github Repository

RDF2h is developed by FactsMission