A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/alangrafu/lodspeakr/wiki/How-to-use-results below:

How to use results · alangrafu/lodspeakr Wiki · GitHub

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

###Taking the first value

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%}

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%}

{%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

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.

{% for row in models.main %}
        <li>  {{row.resource.value|safe|urifragment}} </li>
{% endfor %}
Values that are available everywhere

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%}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3