Templating
Friday, 17 October 2014 · 5 messages · last reply 18 Oct 2014
EC
Emmanuel Chateau17 Oct 2014 19:03 #
Hi,
I’ve been trying to use the templating proposed by Andy Bunce here http://cubeb.blogspot.fr/2012/11/xquery-templating-engines-and-txq.html https://mailman.uni-konstanz.de/pipermail/basex-talk/2012-November/004209.html
I’ve tried with txq module in different ways. But, I can’t figure out why I couldn’t reproduce it. Here is the simpler exemple I’ve tried :
In the webapp folder an xml file, named
view1.xml wich contains :<div>{map:get($map, 'greeting')}</div>In webapp a restxq file with :
module namespace test = 'test.test';
import module namespace xquery = "http://basex.org/modules/xquery";
declare function test:render($template as xs:string, $map as map(*)){
xquery:invoke($template,$map)
};
declare %restxq:path('/test')
%restxq:GET
function test:test(){
let $map := map{ "greeting" : "hello",
"who": "world"}
return test:render('view1.xml', $map )
};Arrêté à /Users/emmanuelchateau/Documents/basex/webapp/view1.xq, 1/15: [XPST0008] Undefined variable $map.
I’m running BaseX version 7.9
Is there still anybody with this kind of templating running ?
Thanks a lot for your help.
Emmanuel
AB
Andy Bunce17 Oct 2014 22:37 #
Hi Emmanuel,
At some point BaseX was changed so that variables must be declared. Now you must declare the variables used in the template. I do this see [1] This loses the advantages of the templates being XML but otherwise works fine.
/Andy
[1] https://github.com/Quodatum/app-doc/tree/master/src/doc/templates
Quoted text · Emmanuel Chateau · 47 lines
On 17 October 2014 18:03, Emmanuel Chateau <emchateau at laposte.net> wrote: > Hi, > > I’ve been trying to use the templating proposed by Andy Bunce here > http://cubeb.blogspot.fr/2012/11/xquery-templating-engines-and-txq.html > > https://mailman.uni-konstanz.de/pipermail/basex-talk/2012-November/004209.html > > I’ve tried with txq module in different ways. But, I can’t figure out why > I couldn’t reproduce it. > Here is the simpler exemple I’ve tried : > > In the webapp folder an xml file, named `view1.xml` wich contains : > ```xml > <div>{map:get($map, 'greeting')}</div> > ``` > > In webapp a restxq file with : > > ```xquery > > module namespace test = 'test.test'; > import module namespace xquery = "http://basex.org/modules/xquery"; > > declare function test:render($template as xs:string, $map as map(*)){ > xquery:invoke($template,$map) > }; > > declare %restxq:path('/test') > %restxq:GET > function test:test(){ > let $map := map{ "greeting" : "hello", > "who": "world"} > return test:render('view1.xml', $map ) > }; > ``` > > Arrêté à /Users/emmanuelchateau/Documents/basex/webapp/view1.xq, 1/15: > [XPST0008] Undefined variable $map. > > > I’m running BaseX version 7.9 > Is there still anybody with this kind of templating running ? > > Thanks a lot for your help. > > Emmanuel
AB
Andy Bunce17 Oct 2014 22:51 #
To be more clear. The render arguments that referenced xml files now reference xquery files. e.g.
return render("main.xq",map{})
Quoted text · Andy Bunce · 63 lines
On 17 October 2014 21:37, Andy Bunce <bunce.andy at gmail.com> wrote: > Hi Emmanuel, > > At some point BaseX was changed so that variables must be declared. > Now you must declare the variables used in the template. I do this see [1] > This loses the advantages of the templates being XML but otherwise works > fine. > > /Andy > > [1] https://github.com/Quodatum/app-doc/tree/master/src/doc/templates > > On 17 October 2014 18:03, Emmanuel Chateau <emchateau at laposte.net> wrote: > >> Hi, >> >> I’ve been trying to use the templating proposed by Andy Bunce here >> http://cubeb.blogspot.fr/2012/11/xquery-templating-engines-and-txq.html >> >> https://mailman.uni-konstanz.de/pipermail/basex-talk/2012-November/004209.html >> >> I’ve tried with txq module in different ways. But, I can’t figure out why >> I couldn’t reproduce it. >> Here is the simpler exemple I’ve tried : >> >> In the webapp folder an xml file, named `view1.xml` wich contains : >> ```xml >> <div>{map:get($map, 'greeting')}</div> >> ``` >> >> In webapp a restxq file with : >> >> ```xquery >> >> module namespace test = 'test.test'; >> import module namespace xquery = "http://basex.org/modules/xquery"; >> >> declare function test:render($template as xs:string, $map as map(*)){ >> xquery:invoke($template,$map) >> }; >> >> declare %restxq:path('/test') >> %restxq:GET >> function test:test(){ >> let $map := map{ "greeting" : "hello", >> "who": "world"} >> return test:render('view1.xml', $map ) >> }; >> ``` >> >> Arrêté à /Users/emmanuelchateau/Documents/basex/webapp/view1.xq, 1/15: >> [XPST0008] Undefined variable $map. >> >> >> I’m running BaseX version 7.9 >> Is there still anybody with this kind of templating running ? >> >> Thanks a lot for your help. >> >> Emmanuel > > >
EC
Thanks a lot ! it solved it.
I had tried to change the view file to xquery, but i dont’t know why, i didn’t test with a variable declaration at first.
It works with individual variable declaration (
view1.xq), but I can’t get back the map itself (in view2.xq). Is it a consistent behavior ? Am I missing some Xquery’subtleties here ?
Emmanuelxquery version "3.0" ;
module namespace test = 'test.test';
import module namespace xquery = "http://basex.org/modules/xquery";
declare function test:render($template as xs:string, $map as map(*)){
xquery:invoke($template, $map)
};
declare %restxq:path('/test')
%restxq:GET
function test:test(){
let $map := map{ "greeting" : "hello",
"who": "world"}
return test:render('view1.xq', $map )
};
declare %restxq:path('/test2')
%restxq:GET
function test:test2(){
let $map := map{ "greeting" : "hello",
"who": "world"}
return test:render('view2.xq', $map)
};view1.xq
xquery version "3.0" ;
declare variable $greeting external;
declare variable $who external;
<div>{$greeting, $who}</div>view2.xq
xquery version "3.0" ;
declare variable $map as map(*) external;
<div>{map:get($map, 'greeting')}</div>Quoted text · Emmanuel Chateau · 45 lines
Le 17 oct. 2014 à 19:03, Emmanuel Chateau <emchateau at laposte.net> a écrit : > Hi, > > I’ve been trying to use the templating proposed by Andy Bunce here > http://cubeb.blogspot.fr/2012/11/xquery-templating-engines-and-txq.html > https://mailman.uni-konstanz.de/pipermail/basex-talk/2012-November/004209.html > > I’ve tried with txq module in different ways. But, I can’t figure out why I couldn’t reproduce it. > Here is the simpler exemple I’ve tried : > > In the webapp folder an xml file, named `view1.xml` wich contains : > ```xml > <div>{map:get($map, 'greeting')}</div> > ``` > > In webapp a restxq file with : > > ```xquery > > module namespace test = 'test.test'; > import module namespace xquery = "http://basex.org/modules/xquery"; > > declare function test:render($template as xs:string, $map as map(*)){ > xquery:invoke($template,$map) > }; > > declare %restxq:path('/test') > %restxq:GET > function test:test(){ > let $map := map{ "greeting" : "hello", > "who": "world"} > return test:render('view1.xml', $map ) > }; > ``` > > Arrêté à /Users/emmanuelchateau/Documents/basex/webapp/view1.xq, 1/15: > [XPST0008] Undefined variable $map. > > > I’m running BaseX version 7.9 > Is there still anybody with this kind of templating running ? > > Thanks a lot for your help. > > Emmanuel
AB
Andy Bunce18 Oct 2014 12:25 #
The evaluated query has its own query context. The only variables available inside are those passed in the bindings.
If you want access to a map variable you must pass it in e.g.
declare
%restxq:path('/test2')
%restxq:GETfunction test:test2(){
let $map := map{
"map":map{ "greeting" : "hello","who": "world"}
}
return test:render('view2.xq', $map)
};Written for BaseX 7.9. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery