Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a flag to toggle class-templating-syntax #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions content/templates.xqm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
xquery version "3.1";


(:~
: HTML templating module
:
Expand All @@ -9,7 +10,6 @@ xquery version "3.1";
: @contributor Joe Wicentowski
:)
module namespace templates="http://exist-db.org/xquery/html-templating";

import module namespace inspect="http://exist-db.org/xquery/inspection";
import module namespace map="http://www.w3.org/2005/xpath-functions/map";
import module namespace request="http://exist-db.org/xquery/request";
Expand All @@ -24,6 +24,7 @@ declare variable $templates:CONFIG_ROOT := "root";
declare variable $templates:CONFIG_FN_RESOLVER := "fn-resolver";
declare variable $templates:CONFIG_PARAM_RESOLVER := "param-resolver";
declare variable $templates:CONFIG_FILTER_ATTRIBUTES := "filter-atributes";
declare variable $templates:CONFIG_USE_CLASS_SYNTAX := "class-lookup";

declare variable $templates:CONFIGURATION := "configuration";
declare variable $templates:CONFIGURATION_ERROR := QName("http://exist-db.org/xquery/html-templating", "ConfigurationError");
Expand All @@ -34,6 +35,7 @@ declare variable $templates:TYPE_ERROR := QName("http://exist-db.org/xquery/html
declare variable $templates:MAX_ARITY := 20;

declare variable $templates:ATTR_DATA_TEMPLATE := "data-template";
declare variable $templates:SEARCH_IN_CLASS := true();

(:~
: Start processing the provided content. Template functions are looked up by calling the
Expand Down Expand Up @@ -92,6 +94,7 @@ declare function templates:apply($content as node()+, $resolver as function(xs:s

declare %private function templates:get-default-config($resolver as function(xs:string, xs:integer) as item()?) as map(*) {
map {
$templates:CONFIG_USE_CLASS_SYNTAX: $templates:SEARCH_IN_CLASS,
$templates:CONFIG_FN_RESOLVER : $resolver,
$templates:CONFIG_PARAM_RESOLVER : templates:lookup-param-from-restserver#1
}
Expand Down Expand Up @@ -136,21 +139,29 @@ declare function templates:process($nodes as node()*, $model as map(*)) {
return
if ($dataAttr) then
templates:call($dataAttr, $node, $model)
else
else if ($model?($templates:CONFIGURATION)?($templates:CONFIG_USE_CLASS_SYNTAX)) then
let $instructions := templates:get-instructions($node/@class)
return
if ($instructions) then
for $instruction in $instructions
return
templates:call($instruction, $node, $model)
else
element { node-name($node) } {
$node/@*, for $child in $node/node() return templates:process($child, $model)
}
templates:process-children($node, $model)
else templates:process-children($node, $model)
default return
$node
};

declare %private function templates:process-children($node as node(), $model as map(*)) {
element { node-name($node) } {
$node/@*,
for $child in $node/node()
return
templates:process($child, $model)
}
};

declare %private function templates:get-instructions($class as xs:string?) as xs:string* {
for $name in tokenize($class, "\s+")
where templates:is-qname($name)
Expand Down Expand Up @@ -576,7 +587,7 @@ declare function templates:form-control($node as node(), $model as map(*)) as no
return
if (exists($value)) then
switch ($type)
case "checkbox"
case "checkbox"
case "radio" return
element { node-name($node) } {
$node/@* except $node/@checked,
Expand Down
11 changes: 11 additions & 0 deletions test/app/call-from-class.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>

<head>
<title>Test calling Templates from class</title>
</head>

<body>
<p class="test:print-from-class"></p>
</body>

</html>
29 changes: 18 additions & 11 deletions test/app/modules/view.xql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ declare variable $test:app-root :=
substring-before($modulePath, "/modules")
;

declare
declare
%templates:wrap
function test:init-data($node as node(), $model as map(*)) {
let $addresses := (
Expand All @@ -50,23 +50,23 @@ function test:init-data($node as node(), $model as map(*)) {
}
};

declare
declare
%templates:wrap
function test:print-name($node as node(), $model as map(*)) {
$model("address")?name
};
declare
declare
%templates:wrap
function test:print-city($node as node(), $model as map(*)) {
$model("address")?city
};
declare
declare
%templates:wrap
function test:print-street($node as node(), $model as map(*)) {
$model("address")?street
};

declare
declare
%templates:wrap
%templates:default("language", "en")
function test:hello($node as node(), $model as map(*), $language as xs:string) {
Expand All @@ -79,26 +79,26 @@ function test:hello($node as node(), $model as map(*), $language as xs:string) {
"Welcome"
};

declare
declare
%templates:wrap
%templates:default("defaultParam", "fallback")
function test:default($node as node(), $model as map(*), $defaultParam as xs:string) {
$defaultParam
};

declare
declare
%templates:wrap
function test:numbers($node as node(), $model as map(*), $n1 as xs:integer, $n2 as xs:double) {
($n1 treat as xs:integer) + ($n2 treat as xs:double)
};

declare
declare
%templates:wrap
function test:date($node as node(), $model as map(*), $date as xs:date) {
day-from-date($date)
};

declare
declare
%templates:wrap
function test:boolean($node as node(), $model as map(*), $boolean as xs:boolean) {
if ($boolean instance of xs:boolean) then
Expand All @@ -111,10 +111,17 @@ declare function test:custom-model($node as node(), $model as map(*)) {
$model?('my-model-item')
};

declare
%templates:wrap
function test:print-from-class($node as node(), $model as map(*)) {
'print-from-class'
};
let $config := map {
$templates:CONFIG_APP_ROOT : $test:app-root,
$templates:CONFIG_STOP_ON_ERROR: true()
$templates:CONFIG_STOP_ON_ERROR: true(),
$templates:CONFIG_USE_CLASS_SYNTAX: xs:boolean(request:get-parameter('classLookup', $templates:SEARCH_IN_CLASS))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to have different configurations to be checked as part of the unit tests. (see

declare variable $tt:config-no-filter := map {
and following)

}

let $lookup := function($name as xs:string, $arity as xs:integer) {
try {
function-lookup(xs:QName($name), $arity)
Expand All @@ -128,4 +135,4 @@ let $lookup := function($name as xs:string, $arity as xs:integer) {
:)
let $content := request:get-data()
return
templates:apply($content, $lookup, map { "my-model-item": 'xxx' }, $config)
templates:apply($content, $lookup, map { "my-model-item": 'xxx' }, $config)
Loading
Loading