Skip to content

Commit

Permalink
Add a flag to toggle class-templating-syntax
Browse files Browse the repository at this point in the history
This commit adds a simple setting, which can be toggled by the configuration as part of the templating-model, to control if the old class based templating syntax (class='foo:bar') is interpreted as a call to a xquery function. This will allow to usage of colons inside classes (e.g. to use TailwindCSS)

Bumped version to v1.2.0

Fixed boolean casting and added test
  • Loading branch information
Bpolitycki committed Dec 6, 2023
1 parent c0f6e4b commit 3fae544
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 229 deletions.
11 changes: 7 additions & 4 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,8 +10,7 @@ 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 inspect="http://exist-db.org/xquery/inspection" at "java:org.exist.xquery.functions.inspect.InspectionModule";
import module namespace map="http://www.w3.org/2005/xpath-functions/map";
import module namespace request="http://exist-db.org/xquery/request";
import module namespace session="http://exist-db.org/xquery/session";
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 @@ -136,7 +138,7 @@ 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), $templates:SEARCH_IN_CLASS)[1]) then
let $instructions := templates:get-instructions($node/@class)
return
if ($instructions) then
Expand All @@ -147,6 +149,7 @@ declare function templates:process($nodes as node()*, $model as map(*)) {
element { node-name($node) } {
$node/@*, for $child in $node/node() return templates:process($child, $model)
}
else $node
default return
$node
};
Expand Down Expand Up @@ -576,7 +579,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
108 changes: 54 additions & 54 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
{
"name": "templating",
"version": "1.1.0",
"description": "eXist-db HTML Templating Library",
"homepage": "https://github.com/eXist-db/templating#readme",
"bugs": "https://github.com/eXist-db/templating/issues",
"keywords": [
"exist",
"exist-db",
"xml",
"xql",
"xquery"
],
"devDependencies": {
"@existdb/gulp-exist": "^4.3.2",
"@existdb/gulp-replace-tmpl": "^1.0.4",
"axios": "^0.21.1",
"chai": "^4.2.0",
"chai-xml": "^0.4.0",
"chokidar": "^3.5.3",
"delete": "^1.1.0",
"fs-extra": "^9.1.0",
"glob-stream": "^7.0.0",
"gulp": "^4.0.2",
"gulp-rename": "^2.0.0",
"gulp-zip": "^5.0.2",
"jsdom": "^16.4.0",
"mocha": "^10.1.0",
"xmldoc": "^1.1.2",
"yeoman-assert": "^3.1.1"
},
"author": {
"name": "The eXist-db Authors"
},
"license": "LGPL-2.1",
"scripts": {
"start": "npm install && npm run build",
"test": "gulp install:all && mocha test/mocha --recursive --exit && node test/xqs/xqSuite.js",
"test:watch": "mocha test/mocha --recursive --watch",
"build": "gulp build",
"build:all": "gulp build:all"
},
"repository": {
"type": "git",
"url": "https://github.com/eXist-db/templating",
"license": "LGPL-2.1"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/existdb"
},
"overrides": {
"chokidar": "$chokidar",
"glob-stream": "$glob-stream"
}
"name": "templating",
"version": "1.2.0",
"description": "eXist-db HTML Templating Library",
"homepage": "https://github.com/eXist-db/templating#readme",
"bugs": "https://github.com/eXist-db/templating/issues",
"keywords": [
"exist",
"exist-db",
"xml",
"xql",
"xquery"
],
"devDependencies": {
"@existdb/gulp-exist": "^4.3.2",
"@existdb/gulp-replace-tmpl": "^1.0.4",
"axios": "^0.21.1",
"chai": "^4.2.0",
"chai-xml": "^0.4.0",
"chokidar": "^3.5.3",
"delete": "^1.1.0",
"fs-extra": "^9.1.0",
"glob-stream": "^7.0.0",
"gulp": "^4.0.2",
"gulp-rename": "^2.0.0",
"gulp-zip": "^5.0.2",
"jsdom": "^16.4.0",
"mocha": "^10.1.0",
"xmldoc": "^1.1.2",
"yeoman-assert": "^3.1.1"
},
"author": {
"name": "The eXist-db Authors"
},
"license": "LGPL-2.1",
"scripts": {
"start": "npm install && npm run build",
"test": "gulp install:all && mocha test/mocha --recursive --exit && node test/xqs/xqSuite.js",
"test:watch": "mocha test/mocha --recursive --watch",
"build": "gulp build",
"build:all": "gulp build:all"
},
"repository": {
"type": "git",
"url": "https://github.com/eXist-db/templating",
"license": "LGPL-2.1"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/existdb"
},
"overrides": {
"chokidar": "$chokidar",
"glob-stream": "$glob-stream"
}
}
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))
}

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

0 comments on commit 3fae544

Please sign in to comment.