RESTful resourcesΒΆ

RESTful resource are defined by using & as a prefix. The resources can be used to load data into data sections.

In the following example the resource named ‘db’ is configured with a wikipedia api hostname. The ZML implementation loads the JSON data from the path /w/api.php?action=query&list=search&format=json&srsearch=rest. The JSON will be converted and is accessible by using the ‘pages’ context variable.

#import components
#inherit base

&db:
  host: 'en.wikipedia.org'

$pages: @db/w/api.php?action=query&list=search&format=json&srsearch=rest

*content:
  %for p in pages.query.search:
    p: {p.title}

The rendered result showing the wikipedia pages for the term ‘rest’:

<!DOCTYPE html>
<html>
  <head>
    <title>zml</title>
  </head>
  <body>
    <p>Representational state transfer</p>
    <p>Rest</p>
    <p>ReStructuredText</p>
    <p>Rest (music)</p>
    <p>Shady Rest, California</p>
    <p>Rest area</p>
    <p>Travelers Rest</p>
    <p>Pilgrim&#39;s Rest, Arkansas</p>
    <p>Note value</p>
    <p>Pilgrim&#39;s Rest</p>
  </body>
</html>

In the following example the resource named ‘db’ is configured with hostname, port, username and password. The ZML implementation loads the JSON data from the path http://localhost:5984/blog/_design/app/_view/posts. The JSON will be converted and is accessible by using the ‘posts’ context variable.

&db:
  host: 'localhost'
  port: 5984
  username: 'zml'
  password: 'secret'

$posts: @db/blog/_design/app/_view/posts


*content:
  %for post in posts.rows:
    p: {post.value.title}

The rendered result f.e. loading data for 3 blog posts from a couchdb:

<!DOCTYPE html>
<html>
  <head>
    <title>zml</title>
  </head>
  <body>
    <p>First blog post title</p>
    <p>Second post</p>
    <p>Third post</p>
  </body>
</html>