Skip to content
timrdf edited this page Dec 21, 2012 · 61 revisions

The [LODSPeaKr templating system](Templating system in LODSPeaKr) comprises model queries that feed view templates. Values obtained by applying model queries are available to subsequent model queries and view templates. This page describes the values that are available in both of these situations.

Using values in templates

Default Values in Template

LODSPeaKr uses Haanga as its template engine. For detailed examples on how to use Haanga, please refer to their documentation.

Result values can be used in views as well as in other models. Values have the following syntax (where periods (.) are literal and <model> and <variable> should be replaced with the name of your model and variable, respectively.

{{models(.<model>)+.<variable>.value}}

What you can get from variables

So far, what you can get from variables are

  • {{models(.<model>)+.<variable>.value}} the actual value
  • {{models(.<model>)+.<variable>.uri}} contains value "1" if it is a URI
  • {{models(.<model>)+.<variable>.blank}} contains value "1" if it is a Blank Node
  • {{models(.<model>)+.<variable>.literal}} contains value "1" if it is a Literal
  • {{models(.<model>)+.<variable>.curie}} contains the curie form of the URI (if it is a literal, it contains the same value as .value)
  • {{models(.<model>)+.<variable>.localname}} contains the last part of the curie (for example, if a curie is foaf:name, the localname will be name)
  • {{models(.<model>)+.<variable>.mirroredUri}} contains the original URI (if it has been mirrored)
  • {{models(.<model>)+.<variable>.type}} contains the datatype, if the variable contains a typed literal
  • {{models(.<model>)+.<variable>.lang}} contains the language tag of the literal, if exist.

###Taking the first value

  • {{first(.<model>)+.<variable>.value}} the actual value
  • {{first(.<model>)+.<variable>.uri}} contains value "1" if it is a URI
  • {{first(.<model>)+.<variable>.blank}} contains value "1" if it is a Blank Node
  • {{first(.<model>)+.<variable>.literal}} contains value "1" if it is a Literal
  • {{first(.<model>)+.<variable>.curie}} contains the curie form of the URI (if it is a literal, it contains the same value as .value)
  • {{first(.<model>)+.<variable>.localname}} contains the last part of the curie (for example, if a curie is foaf:name, the localname will be name)
  • {{first(.<model>)+.<variable>.mirroredUri}} contains the original URI (if it has been mirrored)
  • {{first(.<model>)+.<variable>.type}} contains the datatype, if the variable contains a typed literal
  • {{first(.<model>)+.<variable>.lang}} contains the language tag of the literal, if exist.

The tree structure is

models/
     |
     -> type.foaf:Person/  # This directory contains queries to apply to instances of class  
                        |
                        ->html.queries/  
                                      |   
                                      -> main.query # This file contains a SPARQL query
                                      |
                                      -> friends1.query 
                                      |
                                      ->endpoint.dbpedia/
                                                        |
                                                        ->friends2.query

Also, lets suppose friends1 and friends2 contains the following query

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?s
WHERE {
  ?s ?p ?o .
}

In this case we can access the variables in friends1 by writing

{%for i in models.friends1%}
   {{i.s.value}} {{i.p.curie}}
{%endfor%}

While friends2

{%for i in models.dbpedia.friends2%}
   {{i.s.value}} {{i.p.curie}}
{%endfor%}

Obtain the first value

To obtain only the first value, we can use a shortcut

{{first.friends1.name.value}} is the FIRST name of the list
{{first.dbpedia.friends2.name.value}} is the FIRST name of the other list

Which is equivalent to

{%for i in models.friends1%}
   {%if forloop.first%}
      "{{i.name.value}}" is the FIRST name of the list
   {%endif%}
{%endfor%}

{%for i in models.dbpedia.friends2%}
   {%if forloop.first%}
      "{{i.name.value}}" is the FIRST name of the other list
   {%endif%}
{%endfor%}

Not the last value

{%if !forloop.last%} can be used for "not last value", as illustrated below in a simple JSON view template:

[
   {% for row in r.main %}
      "{{row.input.value}}"{%if !forloop.last%},{%endif%}
   {% endfor %}
]

Handling an optional value

If a model query selects an optional value, the {%if row.dump%}{%endif%} construct can be used to omit the value in the view template. {%else} can also be used.

    {% for row in models.main %}
      <tr>
        <td>{{row.name.value}}</td>
        <td>{%if row.dump%}<a href="{{row.dump.value}}">dump</a>{%else%}not found{%endif%}</td>
      </tr>
    {% endfor %}

How to obtain only the first value

Sometimes it is cumbersome to do a for just to get the first (sometimes the only) value. For that purpose it is possible to replace r for first and access directly the first variable

{%for i in models.details%}
   {%if forloop.first%}
      "{{i.name.value}}" is the FIRST name of the list
   {%endif%}
{%endfor%}

is equivalent to

{%for i in models.details%}
   "{{first.details.name.value}}" is the FIRST name of the list
{%endfor%}

Variables filled by default

TODO: are these available in model queries AND views?

There are a few variables available by default

  • {{uri}} Contains the URI that is being requested (this is a shortcut for {{lodspk.this.value}})
  • {{lodspk.this.value}} is the URI requested and {{lodspk.this.curie}} in its CURIE form
  • {{lodspk.this.contentType}} The content type that will be served
  • {{lodspk.local.value}} is the mirrored/local URI requested (if the data is in domain http://a/ buy is served in domain http://b/, the mirrored URI will use the second domain). Analogously, {{lodspk.this.curie}} is the mirrored/local uri in CURIE form.
  • {{lodspk.local.contentType}} The content type that will be served
  • {{lodspk.ns.NAMESPACE}} The URI for a particular namespace (e.g., {{lodspk.ns.foaf}}, {{lodspk.ns.rdfs}})
  • {{lodspk.queries}} contains the URLs of the queries used by this component. The format is the same as in models. For more details, see Obtaining URLs of the queries used in a component

Filters

Haanga supports multiple filters (i.e., functions that preprocess the values passed). They are applied by using | after the variable.

{%for i in models.names%}
   {{i.name.value|title}}
{%endfor%}

Will turn the first letter to uppercase and the rest to lowercase. The best place to find the filters available in Haanga is to look at its repository. In addition to those provided by Haanga, LODSPeaKr provides [filters](Filters in LODSPeaKr) that can be used to display data using different visualizations.

URI's local name

{% for row in models.main %}
        <li>  {{row.resource.value|safe|urifragment}} </li>
{% endfor %}

Values that are available everywhere

Craziness

You can handle crazy query results using this pattern like this does:

{%for row in models.all_property_details%}

   {%ifchanged row.property.value%}
      {%if !forloop.first%}
      </div>
      </table>
      {%endif%}
      <div id="{{row.property.value|safe|urifragment}}">
      <table class="prov-detail">
         <tr><th colspan="2" style="background-color:#EAF2D3; color:black;">
             <a href="#{{row.property.value|safe|urifragment}}">{{row.property.curie}}</a></th>
         </tr>
         <tr><td>IRI</td><td><span style="font-family: sans-serif">{{row.property.value}}</span></td></tr>
   {%endifchanged%}

         {%if row.comment%}
            <tr><td>comment</td><td>{{row.comment.value}}}</a></td></tr>
         {%endif%}
         {%if row.domain%}
            <tr><td>has domain</td>
                <td><a href="#{{row.domain.value|safe|urifragment}}">{{row.domain.curie}}</a></td>
            </tr>
         {%endif%}
         {%if row.range%}
            <tr><td>has range</td>
                <td><a href="#{{row.range.value|safe|urifragment}}">{{row.range.curie}}</a></td>
            </tr>
         {%endif%}
         {%if row.inverse%}
            <tr><td>has inverse</td>
                <td><a href="#{{row.inverse.value|safe|urifragment}}">{{row.inverse.curie}}</a></td>
            </tr>
         {%endif%}

   {%if forloop.first%}
      </table>
      </div>
   {%endif%}
{%endfor%}

What's next?

Clone this wiki locally