From Documentation
Revision as of 13:26, 19 January 2022 by Hawk (talk | contribs) (correct highlight (via JWB))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The default interpreter for the zscript elements is Java (based on BeanShell). Depending on your preference, you could choose one of built-in interpreters, or implement your own interpreter.

The built-in interpreters includes: Java, Groovy, Ruby, Python, and JavaScript.

Choose Interpreter for Whole Page

To change the default interpreter for the whole page, you could use the page directive by specifying the zscriptLanguage attribute, such as

<?page zscriptLanguage="Groovy"?>
<window border="normal">
	<vbox id="vb">
		<label id="l" value="Hi"/>
		<button label="change label" onClick="l.value='Hi, Groovy';"/>
		<button label="add label" onClick="new Label('New').setParent(vb);"/>
	<button label="alert" onClick="alert('Hi, Groovy')"/>

Choose Interpreter for zscript

You could choose an interpreter for a particular zscript element by specifying the language attribute as follows.

<zscript language="Ruby">
(Java::Label.new 'New').parent = $vb

Choose Interpreter for Event Handler

You could choose an interpreter for a particular event handler by prefixing it with the language name as follows.

<button label="alert" onClick="python:alert('Hi, Python')"/>

Support More Scripting Languages

Currently ZK supports Java, JavaScript, Ruby, Groovy, and Python. However, it is easy to extend:

  1. Provide a class that implements Interpreter. However, it is suggested to derive from GenericInterpreter for simplicity.
  2. Declare the scripting language in either WEB-INF/zk.xml, or zk/config.xml.
    <language-name>SuperJava</language-name><!-- case insensitive -->

Multi-Scope versus Single-Scope

Depending on the implementation, an interpreter might have exactly one logical scope, or one logic scope per ID space to store these variables and methods declared in zscript. For the sake of description, we will call them the single-scope and multi-scope interpreters respectively.

For example, ZK's Java interpreter(BeanShell) is a multi-Scope Interpreter. On the other hand, Ruby, Groovy and JavaScript interpreters don't support multi-scope. It means all variables defined in, say, Ruby are stored in one logical scope (per interpreter). To avoid confusion, you could prefix the variable names with special prefix denoting the window.

Notice that each page has its own interpreter to evaluate the zscript code.

Version History

Last Update : 2022/01/19

Version Date Content

Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.