Last active
October 13, 2015 00:27
-
-
Save apb2006/4110256 to your computer and use it in GitHub Desktop.
TXQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<h3>{$action/@id/string()}</h3> | |
<span class="label">{$action/@in/string()}</span> → <span class="label">{$action/@out/string()}</span> | |
<p>{$action/describe}</p> | |
<pre>{$action/xquery}</pre> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head><title>{$title}</title> | |
<script src="...."></script> | |
</head> | |
<body> | |
<div>..nav bar..</div> | |
{$body} | |
</body> | |
</html> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare function txq:render($template,$map){ | |
xquery:invoke($template,$map) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(:~ | |
: 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) | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
txq:render("view1.xml",map{"greeting":="hello","who":="world"}) | |
(: result :) | |
<div>hello world</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(:~ | |
: 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) | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div>{$greeting} {$who}</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment