Next: Introduction, Previous: (dir), Up: (dir) [Contents][Index]
This is the closure-template Reference Manual, generated automatically by Declt version 4.0 beta 2 "William Riker" on Mon Aug 15 03:32:36 2022 GMT+0.
Next: Systems, Previous: The closure-template Reference Manual, Up: The closure-template Reference Manual [Contents][Index]
cl-closure-template - Common Lisp implementation of Closure Template from Google
CL-USER> (asdf:operate 'asdf:load-op '#:closure-template)
CL-USER> (defparameter *template* "
/*
* Greets a person using 'Hello' by default.
*/
{namespace closureTemplate.Example}
{template helloName}
{if not $greetingWorld}
Hello {$name}!
{else}
{$greetingWorld} {$name}!
{/if}
{/template}")
*TEMPLATE*
CL-USER> (closure-template:compile-template :common-lisp-backend *template*)
#<PACKAGE "CLOSURETEMPLATE.EXAMPLE">
CL-USER> (closuretemplate.example:hello-name '(:name "Andrey"))
"Hello Andrey!"
CL-USER> (closure-template.example:hello-name '(:name "Andrey" :greeting-world "Hi"))
"Hi Andrey!"
CL-USER> (closure-template:compile-template :javascript-backend *template*)
"if (typeof closureTemplate === 'undefined') { closureTemplate = {}; }
if (typeof closureTemplate.Example === 'undefined') { closureTemplate.Example = {}; }
closureTemplate.Example.$isEmpty$ = function (obj) {
for (var prop in obj) if (obj.hasOwnProperty(prop)) return false;
return true;
};
closureTemplate.Example.$escapeHTML$ = function (obj) {
if (typeof obj == 'string') return String(obj).split('&').join('&').split( '<').join('<').split('>').join('>').split('\\\"').join('"').split('\\'').join(''');
else return obj;
};
closureTemplate.Example.$round$ = function (number, ndigits) {
if (ndigits) {
var factor = Math.pow(10.0, ndigits);
return Math.round(number * factor) / factor;
}
else return Math.round(number)
};
closureTemplate.Example.$objectFromPrototype$ = function (obj) {
function C () {}
C.prototype = obj;
return new C;
};
closureTemplate.Example.helloName = function($env$, $target$) {
if (!$env$) { $env$ = {}; }
var $result$ = $target$ || [];
if (!$env$.greetingWorld) {
$result$.push(\"Hello \");
$result$.push(closureTemplate.Example.$escapeHTML$($env$.name));
$result$.push(\"!\");
}
else {
$result$.push(closureTemplate.Example.$escapeHTML$($env$.greetingWorld));
$result$.push(\" \");
$result$.push(closureTemplate.Example.$escapeHTML$($env$.name));
$result$.push(\"!\");
}
if (!$target$) return $result$.join('');
else return null;
};
"
CL-USER> (closure-template:compile-template :requirejs-backend *template*)
"define(function () {
var module = { };
module.$isEmpty$ = function (obj) {
for (var prop in obj) if (obj.hasOwnProperty(prop)) return false;
return true;
};
module.$escapeHTML$ = function (obj) {
if (typeof obj == 'string') return String(obj).split('&').join('&').split( '<').join('<').split('>').join('>').split('\\\"').join('"').split('\\'').join(''');
else return obj;
};
module.$round$ = function (number, ndigits) {
if (ndigits) {
var factor = Math.pow(10.0, ndigits);
return Math.round(number * factor) / factor;
}
else return Math.round(number)
};
module.$objectFromPrototype$ = function (obj) {
function C () {}
C.prototype = obj;
return new C;
};
module.helloName = function($env$, $target$) {
if (!$env$) { $env$ = {}; }
var $result$ = $target$ || [];
if (!$env$.greetingWorld) {
$result$.push(\"Hello \");
$result$.push(module.$escapeHTML$($env$.name));
$result$.push(\"!\");
}
else {
$result$.push(module.$escapeHTML$($env$.greetingWorld));
$result$.push(\" \");
$result$.push(module.$escapeHTML$($env$.name));
$result$.push(\"!\");
}
if (!$target$) return $result$.join('');
else return null;
};
return module; });"
You can add custom print directives. For example, printing integers as hexadecimal values:
CL-USER> (closure-template:define-print-syntax printHex "hex" (:constant t))
CLOSURE-TEMPLATE.PARSER::PRINT-DIRECTIVE
CL-USER> (closure-template:register-print-handler :common-lisp-backend 'printHex :function #'(lambda (params end value) (format nil "~X" value)))
#<Anonymous Function #x302001B7085F>
CL-USER> (defparameter *template* "
/*
* Greets a person using 'Hello' by default.
*/
{namespace closureTemplate.Example}
{template helloName}
Hello {$name} {$param|hex}!
{/template}")
*TEMPLATE*
CL-USER> (closure-template:compile-template :common-lisp-backend *template*)
#<Package "CLOSURE-TEMPLATE.EXAMPLE">
CL-USER> (closure-template.example:hello-Name '(:name "Name" :param 128))
"Hello Name 80!"
To use Javascript backend, you need to register a handler for Javascript or RequireJS backends. This is a more complex example which shows how to handle parameters of the directive:
CL-USER> (closure-template:define-print-syntax printHex (and "hex" (esrap:? (and ":" (or "upper" "lower"))))
(:destructure (tag value)
(declare (ignore tag))
(list (if (and value (string= (second value) "lower")) 'case-lower 'case-upper))))
PRINT-HEX
CL-USER> (closure-template:register-print-handler :common-lisp-backend 'printHex
:function #'(lambda (params env value)
(declare (ignore env))
(format nil (if (member 'case-lower params) "~(~X~)" "~X") value)))
PRINT-HEX
CL-USER> (closure-template:register-print-handler :javascript-backend 'printHex
:handler "function (params, value) { var result = value.toString(16); if (params.case == \"upper\") { result = result.toUpperCase(); } return result; }"
:parameter-converter #'(lambda (params)
(format nil "{ case: \"~A\"; }" (if (member 'case-lower params) "lower" "upper"))))
PRINTHEX
CL-USER> (defparameter *template* "
/*
* Greets a person using 'Hello' by default.
*/
{namespace closureTemplate.Example}
{template helloName}
Hello {$name} {$param|hex:lower}!
{/template}")
*TEMPLATE*
CL-USER> (closure-template.example:hello-name '(name "Name" :param 143))
"Hello Name 8F!"
You can also use a predefined function by name or RequireJS module. For detals, please see docstrings for REGISTER-PRINT-HANDLER implementations.
Next: Modules, Previous: Introduction, Up: The closure-template Reference Manual [Contents][Index]
The main system appears first, followed by any subsystem dependency.
Next: Files, Previous: Systems, Up: The closure-template Reference Manual [Contents][Index]
Modules are listed depth-first from the system components tree.
Next: closure-template/common-lisp-backend, Previous: Modules, Up: Modules [Contents][Index]
closure-template (system).
Next: closure-template/javascript-backend, Previous: closure-template/parser, Up: Modules [Contents][Index]
asdf.lisp (file).
closure-template (system).
Previous: closure-template/common-lisp-backend, Up: Modules [Contents][Index]
standard-templates.lisp (file).
closure-template (system).
javascript-backend.lisp (file).
Next: Packages, Previous: Modules, Up: The closure-template Reference Manual [Contents][Index]
Files are sorted by type and then listed depth-first from the systems components trees.
Next: closure-template/parser/defpackage.lisp, Previous: Lisp, Up: Lisp [Contents][Index]
closure-template (system).
Next: closure-template/parser/command.lisp, Previous: closure-template/closure-template.asd, Up: Lisp [Contents][Index]
parser (module).
Next: closure-template/parser/expression.lisp, Previous: closure-template/parser/defpackage.lisp, Up: Lisp [Contents][Index]
defpackage.lisp (file).
parser (module).
Next: closure-template/defpackage.lisp, Previous: closure-template/parser/command.lisp, Up: Lisp [Contents][Index]
command.lisp (file).
parser (module).
Next: closure-template/interface.lisp, Previous: closure-template/parser/expression.lisp, Up: Lisp [Contents][Index]
parser (module).
closure-template (system).
Next: closure-template/asdf.lisp, Previous: closure-template/defpackage.lisp, Up: Lisp [Contents][Index]
defpackage.lisp (file).
closure-template (system).
Next: closure-template/common-lisp-backend/escape.lisp, Previous: closure-template/interface.lisp, Up: Lisp [Contents][Index]
closure-template (system).
Next: closure-template/common-lisp-backend/common-lisp-backend.lisp, Previous: closure-template/asdf.lisp, Up: Lisp [Contents][Index]
common-lisp-backend (module).
encode-string (function).
Next: closure-template/standard-templates.lisp, Previous: closure-template/common-lisp-backend/escape.lisp, Up: Lisp [Contents][Index]
escape.lisp (file).
common-lisp-backend (module).
Next: closure-template/javascript-backend/javascript-backend.lisp, Previous: closure-template/common-lisp-backend/common-lisp-backend.lisp, Up: Lisp [Contents][Index]
common-lisp-backend (module).
closure-template (system).
Previous: closure-template/standard-templates.lisp, Up: Lisp [Contents][Index]
javascript-backend (module).
Next: Definitions, Previous: Files, Up: The closure-template Reference Manual [Contents][Index]
Packages are listed by definition order.
Next: closure-template.parser, Previous: Packages, Up: Packages [Contents][Index]
Previous: closure-template, Up: Packages [Contents][Index]
Next: Indexes, Previous: Packages, Up: The closure-template Reference Manual [Contents][Index]
Definitions are sorted by export status, category, package, and then by lexicographic order.
Next: Internals, Previous: Definitions, Up: Definitions [Contents][Index]
Next: Macros, Previous: Public Interface, Up: Public Interface [Contents][Index]
Next: Ordinary functions, Previous: Special variables, Up: Public Interface [Contents][Index]
Define a syntax rule for a new user print directive which is identified by SYMBOL. EXPR defines a grammar of the new directive without whitespace and the "|" delimiter. The body of definition should contain semantic rules used to convert possible parameters of the directive
Executes body with possibility to call specified functions from templates.
Usage: (with-user-functions (("incr" #’1+))
(closure-template:compile-template :common-lisp-backend "{template test} x + 1 = {incr($x)}{/template}"))
Next: Generic functions, Previous: Macros, Up: Public Interface [Contents][Index]
Replaces each escape sequence in the encoded URI with the character that it represents.
Next: Standalone methods, Previous: Ordinary functions, Up: Public Interface [Contents][Index]
Compile templates from TEMPLATE using BACKEND as target language
automatically generated reader method
automatically generated reader method
automatically generated reader method
automatically generated reader method
automatically generated reader method
automatically generated reader method
expr.
Register handler for custom print directive in BACKEND.
Directive syntax is defined by DIRECTIVE. Actual handler parameters are depend on
backend type.
Register DIRECTIVE handler for Javascript backend.
The following parameters are accepted:
:HANDLER - string which defines actual Javascript handler like "function (params, value) { }";
:FUNCTION - string with name of handler function (it should be defined elsewhere with
prototype similar to above);
:MODULE - RequireJS module which implements :FUNCTION.
:PARAMETER-CONVERTER - lambda which accepts data from parser and returns string parameters for handler.
:HANDLER or :FUNCTION are required, :PARAMETER-CONVERTER and :MODULE are optional. If :PARAMETER-CONVERTER is NIL or it returns NIL or empty string then "params" argument will be omitted in call to handler. if :MODULE not is missing it should contain URI of the RequireJS module which implements function :FUNCTION. :MODULE it not used when :HANDLER is specified
Register DIRECTIVE handler for Javascript backend.
The following parameters are accepted:
:HANDLER - string which defines actual Javascript handler like "function (params, value) { }";
:FUNCTION - string with name of handler function (it should be defined elsewhere with
prototype similar to above);
:PARAMETER-CONVERTER - lambda which accepts data from parser and returns string parameters for handler.
:HANDLER or :FUNCTION are required, :PARAMETER-CONVERTER is optional. If :PARAMETER-CONVERTER is NIL or it returns NIL or empty string then "params" argument will be omitted in call to handler.
Register handler for print DIRECTIVE in the BACKEND. ARGS must contain symbol :FUNCTION and lambda function with prototype (lambda (parameters environment value))
automatically generated reader method
automatically generated reader method
automatically generated reader method
expr.
Next: Classes, Previous: Generic functions, Up: Public Interface [Contents][Index]
asdf/action.
asdf/action.
asdf/action.
Previous: Standalone methods, Up: Public Interface [Contents][Index]
source-file.
common-lisp.
"tmpl"
:varname
This slot is read-only.
:expr
This slot is read-only.
:code-block
This slot is read-only.
:if-empty-code
This slot is read-only.
:options
This slot is read-only.
common-lisp.
:values
This slot is read-only.
:content
This slot is read-only.
:pairs
This slot is read-only.
:jsname
This slot is read-only.
Previous: Public Interface, Up: Definitions [Contents][Index]
Current JavaScript namespace
Next: Ordinary functions, Previous: Special variables, Up: Internals [Contents][Index]
Next: Generic functions, Previous: Macros, Up: Internals [Contents][Index]
Convert an infix expression to prefix.
impl.
name.
Find the highest-precedence operator in INFIX and reduce accordingly.
Next: Structures, Previous: Ordinary functions, Up: Internals [Contents][Index]
Make expression handler
Write expression as JavaScript
Next: Classes, Previous: Generic functions, Up: Internals [Contents][Index]
Previous: Structures, Up: Internals [Contents][Index]
Previous: Definitions, Up: The closure-template Reference Manual [Contents][Index]
Jump to: | %
(
-
A C D E F G H I L M N O P R S T U V W |
---|
Jump to: | %
(
-
A C D E F G H I L M N O P R S T U V W |
---|
Next: Data types, Previous: Functions, Up: Indexes [Contents][Index]
Jump to: | *
+
A B C D E H I J M N O P R S T V |
---|
Jump to: | *
+
A B C D E H I J M N O P R S T V |
---|
Jump to: | A C D E F I J L M N O P R S T V W |
---|
Jump to: | A C D E F I J L M N O P R S T V W |
---|