Attribute Commands
Return to Developer Support
EditMe Layouts and Scripted Pages use one-word "attribute commands" to create dynamic behavior, such as inserting the current page title, listing the comments for the current page, or doing something special if the user has already logged in. An attribute script consists of one or more attribute commands in an "editme" attribute of an HTML tag, as in <p editme="..."></p>.
The following attribute commands are available. Click a command name for full documentation:
- call - Calls a JavaScript function, passing the current tag (element) as an argument
- cdata - Replaces the content of the tag with a non-escaped (HTML) value
- eval - Executes a JavaScript argument
- foreach - Repeats the tag and children tags once for each member of a list
- if - Removes the tag if the JavaScript argument returns false
- ifattr - Removes a named attribute in the tag if the JavaScript argument returns false
- include - Includes a page by name
- parse - Evaluates tag content for {scriptlets}
- process - Process attribute commands in a specified XML document or page
- text - Replaces the content of the tag with escaped text
- times - Repeats the tag and children a specified number of times
call
Calls a JavaScript function, passing the current tag (DOM4J element) as the first argument.
Syntax: call <func_name> [<arg2>] [<arg3>] [..]
Arguments:
- func_name: The name of a JavaScript function. This function must have already been defined, usually in a pre-process script.
- arg2, arg3 ..: Optional additional arguments to pass, separated by a space. A reference to the tag itself is always passed as the first argument.
Example:
<script type="text/vnd.editme.js.pre">
function myfunc (tag, arg1) {
tag.setText('arg1 = ' + arg1);
}
</script>
<p editme="eval s='Hello!' ;; call myfunc s">...</p>
Result:
<p>Hello!</p>
[return to command list]
cdata
Replaces the content of the tag with a non-escaped (HTML) value.
Syntax: cdata <javascript>
Arguments:
- javascript: a JavaScript statement that evaluates to a string
Example:
<p editme="cdata '<b>Bold!</b>'"></p>
Result:
<p><b>Bold!</b></p>
Compare this result with the text command.
[return to command list]
eval
Executes a JavaScript argument. The result of the evaluation is discarded.
Syntax: eval <javascript>
Arguments:
- javascript: Any JavaScript statement or statements separated by single semi-colons.
Example:
<p editme="eval totalLength=page.content.length; for (i in page.comments)
{ totalLength += page.comments[i].comment.length } ;; parse">{totalLength}</p>
Result (makes assumption about page and comment content length):
<p>251</p>
Tip: It's often more readable to simply use an inline script tag to accomplish something like this. For example:
<script type="text/vnd.editme.js">
totalLength = page.content.length;
for (i in page.comments) {
totalLength += page.comments[i].comment.length
}
</script>
<p editme="parse">{totalLength}</p>
[return to command list]
foreach
Repeats and processes the tag and children tags once for each member of a list.
Syntax: foreach <variable> <list>
Arguments:
- variable: The name of a JavaScript variable to store the list member for each iteration. This variable can be referenced by child tags within the repeated tag. In the case of a numerically indexed Array, this variable will be set to the actual value in the array. In the case of a key/value pair array, this variable will be set to the key.
- list: A list of items; typically a JavaScript array.
- The foreach command must be the first command in an attribute script. The following will result in an error:
<p editme="eval array=['a','b'] ;; foreach letter array ;; parse">{letter}</p> - Any attribute commands following foreach will be run for each instance in the list.
- The foreach command places repeated tags at the end of the parent block. This can cause confusion. See Example 2 below. As a work-around, simply insert a <span> tag as the parent of the tag that is repeated.
Example 1:
<div editme="foreach letter ['a','b','c'] ;; parse">{letter}</div>
Result 1:
<div>a</div><div>b</div><div>c</div>
Example 2 (see note above):
<table>
<tr>
<td>First</td>
<td editme="foreach letter ['a','b'] ;; parse">{letter}</td>
<td>Last</td>
</tr>
</table>
Result 2:
<table> <tr> <td>First</td> <td>Last</td> <td>a</td> <td>b</td> </tr> </table>
Example 3:
<script type="text/vnd.editme.js">
var pairs = {'a':'A', 'b':'B', 'c':'C'};
</script>
<div editme="foreach letter pairs ;; text letter + ':' + pairs[letter]">
</div>
Result 3:
<div>a:A</div><div>b:B</div><div>c:C</div>
[return to command list]
if
Removes the tag if the JavaScript argument returns false. Any attribute
commands following the if command are not executed if the statement
evaluates to false.
Syntax: if <javascript>
Arguments:
- javascript: Any JavaScript statement or statements that evaluates to true or false.
Example:
<script type="text/vnd.editme.js">
var x = 4;
</script>
<p editme="if x<5 ;; parse">{x} is less than 5<p>
<p editme="if x>=5 ;; parse">{x} is greater than or equal to 5<p>
Result:
<p>4 is less than 5<p>
[return to command list]
ifattr
Removes a named attribute in the tag if the JavaScript argument returns false.
Syntax: ifattr <attribute_name> <javascript>
Arguments:
- attribute_name: The name of the attribute on the tag that should be removed if the JavaScript statement(s) evaluates to false.
- javascript: Any JavaScript statement or statements that evaluates to true or false.
Example:
<a editme="ifattr onclick !user.isLoggedIn" href="/Private"
onclick="alert('You must login first!'); return false">Click Me</a>
Result (if user is logged in):
<a href="/Private">Click Me</a>
Result (if user is not logged in):
<a href="/Private" onclick="alert('You must login first!'); return false">Click Me</a>
[return to command list]
include
Includes a the content of a named page.
Syntax: include <javascript>
Arguments:
- javascript: A JavaScript statement(s) that evaluates to a string specifying a page name.
Example:
<div editme="include 'menu'"></div>
Result:
<div>contents of the menu page</div>
Tip: This command is simply short-hand for "cdata site.getPage('menu').content"
[return to command list]
parse
Evaluates and outputs result from JavaScript between {squiggly brackets} within the tag content and/or within named attribute values.
Syntax:
- parse
- parse <attribute_name> [<attribute_name> ...]
- parse . <attribute_name> [<attribute_name> ...]
Arguments:
- attribute_name (zero or more): The name of an attribute on the current tag that contains {scriptlets}.
- . (period): A . can be listed as an argument if one or more attribute names are listed as arguments to indicate that the tag content should also be parsed.
Notes:
- Called with no arguments, the contents of the tag is parsed.
- If one or more attribute names are listed as arguments, the tag content is not parse unless one of the arguments is a period.
- Squiggly brackets can be ignored by following the an opening set with a tilde, as in: {~ this will not be processed}
- Scriptlets cannot contain line breaks.
- Scriptlets are all processed when the tag is processed. Any attribute commands in child tags are executed after the parent tag content is parsed. See Example 4 for an illustration of this behavior.
- The XML parser used by EditMe to extract text nodes to be parsed can be finicky: if a scriptlet containing somewhat complex code is not getting parsed, try moving the code to an inline script tag, setting the result to a variable, and placing just the variable name storing the result into a scriptlet.
Example 1:
<a editme="parse" href="Home">{user.name}</a>
Result 1:
<a href="Home">Bob</a>
Example 2:
<a editme="parse href" href="{user.name}">Your Page</a>
Result 2:
<a href="Bob">Your Page</a>
Example 3:
<a editme="parse . href" href="{user.name}">{user.name}</a>
Result 3:
<a href="Bob">Bob</a>
Example 4:
<p editme="eval x=1 ;; parse">
<span editme="eval x = 2">
{x}
<span>
</p>
Result 4:
<p><span>1</span><p>
[return to command list]
process
Process attribute commands in a specified XML document JavaScript page or Scripted Page. The argument for this command can be a page name (stored in a variable or a String literal), a page object, or a DOM4J Document object. If a page object or page name is given, that page's content should be valid XML with attribute commands to be executed. Pages must have either an EditMe Layout or EditMe Scripted Page mime type set in Page Properties to be accepted as arguments to this command.
Security Warning: Any page content processed this way should be editable by Administrators only. Allowing public edit to a page that is processed could be exploited to expose otherwise secured content on your site.
Syntax:
- process <page_name>
- process <page_obj>
- process <xml_doc>
Arguments:
- page_name: A string literal or variable containing a valid page name.
- page_obj: A variable containing a page object.
- xml_doc: A variable containing a DOM4J Document object.
Example:
<div editme="process 'testpage'"></div>
Result:
Assuming that the page named "testpage" contained the following content:
<p editme="text 'Hello!'"></p>
The result would be:
<div><p>Hello!</p></div>
[return to command list]
text
Replaces the content of the tag with escaped text.
Syntax: text <string>
Arguments:
- string: A string literal or variable containing a string value.
Example:
<p editme="text 'To make bold text, use the <b> tag.'"></p>
Result:
<p>To make bold text, use the <b> tag.</p>
[return to command list]
times
Repeats and processes the tag and children a specified number of times.
Syntax: times <enum> <count>
Arguments:
- enum: A variable name to store the current iteration, starting with 1 and ending with <count>
- count: A numeric literal or variable defining the number of times the template should be repeated
Example:
<p>As easy as<span editme="times n 3 ;; parse"> {n}</span>.</p>
Result:
<p>As easy as 1 2 3.</p>
[return to command list]