(:~
 : This is the main XQuery which will (by default) be called by controller.xq
 : to process any URI ending with ".html". It receives the HTML from
 : the controller and passes it to the templating framework.
 :)
xquery version "3.0";

declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";

import module namespace templates="http://exist-db.org/xquery/html-templating";

(: 
 : The following modules provide functions which will be called by the 
 : templating framework.
 :)
import module namespace app="http://my.domain/myapp" at "app.xql";

declare option output:method "html";
declare option output:html-version "5.0";
declare option output:media-type "text/html";

(:
 : We have to provide a lookup function to templates:apply to help it
 : find functions in the imported application modules. The templates
 : module cannot see the application modules, but the inline function
 : below does see them.
 :)
declare variable $lookup := function($functionName as xs:string, $arity as xs:integer) as function(*)? {
    function-lookup(xs:QName($functionName), $arity)
}

(:
 : The HTML is passed in the request from the controller.
 : Run it through the templating framework and return the result.
 :)
templates:apply(
    request:get-data(),
    $lookup,
    () (: the third parameter is the initial model :)
)
