Skip to content

Instantly share code, notes, and snippets.

@emchateau
Forked from apb2006/actions.xml
Created June 27, 2014 16:44
Show Gist options
  • Save emchateau/c6575e46aa1286b07548 to your computer and use it in GitHub Desktop.
Save emchateau/c6575e46aa1286b07548 to your computer and use it in GitHub Desktop.
<div>
<h2>{$heading} - {count($actions/generate)}</h2>
<p>Actions are processes that generate a new item from an existing item.</p>
<div>
{$partial("action1.xml","action",$actions/generate )}
</div>
</div>
<div>
<h3>{$action/@id/string()}</h3>
<span class="label">{$action/@in/string()}</span> &#x2192; <span class="label">{$action/@out/string()}</span>
<p>{$action/describe}</p>
<pre>{$action/xquery}</pre>
</div>
<html>
<head><title>{$title}</title>
<script src="...."></script>
</head>
<body>
<div>..nav bar..</div>
{$body}
</body>
</html>
let $map:=map{"greeting":="hello","who":="world","title":="full page"}
return txq:render("view1.xml", $map, "layout.xml")
(: result :)
<html>
<head><title>full page</title>
<script src="...."></script>
</head>
<body>
<div>..nav bar..</div>
<div>hello world</div>
</body>
</html>
declare function txq:render($template,$map){
xquery:invoke($template,$map)
}
(:~
: template function with wrapping layout
: @param layout
: @return updated doc from map
:)
declare function render($template as xs:string,$map as map(*),
$layout as xs:string){
let $content:=render($template,$map)
let $map:=map:new(($map,map{"body":=$content}))
return render($layout,$map)
};
txq:render("view1.xml",map{"greeting":="hello","who":="world"})
(: result :)
<div>hello world</div>
(:~
: A(nother) templating Engine for XQuery (BaseX 7.5 specific)
: specials:
: partial(file,name,sequence)
:
: @author andy bunce
: @since sept 2012
:)
module namespace txq = 'apb.txq';
declare default function namespace 'apb.txq';
import module namespace xquery = "http://basex.org/modules/xquery";
(:~
: template function
: @param template url to fill
: @param map name and value to apply
: @return updated doc from map
:)
declare function render($template as xs:string,$map as map(*)){
let $map:=map:new(($map,map{"partial":=partial(?,?,?,$map,$template)}))
return xquery:invoke($template,$map)
};
(:~
: template function with wrapping layout
: @param layout
: @return updated doc from map
:)
declare function render($template as xs:string,$map as map(*),$layout as xs:string){
let $content:=render($template,$map)
let $map:=map:new(($map,map{"body":=$content}))
return render($layout,$map)
};
(:~
: partial template function: evaluate part for each value in sequence
: @return updated doc from map
:)
declare function partial($part as xs:string,$name,$seq,$map,$base){
for $s in $seq
let $map:=map:new(($map,map{$name:=$s}))
return render(fn:resolve-uri($part,$base),$map)
};
<div>{$greeting} {$who}</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment