Table of Contents
|
Perhaps surprisingly, the simple act of entering a script on a page turns out to be a significant problem area for many new users. This article attempts to collect absolutely everything you need to know about this, so you can get past it quickly and move on to the more interesting stuff. Every single DekiScript programmer should know everything on this page.
A brief word about editor terminology
Editor terminology can get a little confusing, so let's clear things up before we get started. When we first open the editor, we are using what we shall call the "WYSIWYG editor". This is where you'll spend somewhere between 90% and 100% of your time. Clicking on the "Source" button at the top of the editor toolbar takes you to the "Source editor", where you can directly edit the HTML source of the page.
Where this gets messy is when the word "source" is used to refer to the content you edit in the WYSIWYG editor. This happens in a few high-profile places: some skins have a "View source" option on the "More" menu. This option takes you to the WYSIWYG editor. The next is the Syntax highlighter, which is commonly used in DekiScript FAQ pages to display the Template Dekiscript. The highlighter has a "view source" button, which again displays code that is intended for the WYSIWYG editor.
There's no good solution for this right now except to pay attention and be careful. Whenever possible, we will use the term "HTML source" to unambiguously refer to what you see in the "source editor".
Once you're ready to enter a script and you've opened the WYSIWYG editor for the page, you have at least three separate ways to do it. You're likely to encounter all of them in your travels.
In the WYSIWYG editor, you can enclose a script in double braces {{ }}. Essentially, the wiki will interpret everything between the double braces as a DekiScript, and replace the entire script (including the braces) with its output. So, if I enter this extremely simple script into the editor:
{{ page.path }}
When I save the page, it'll look like this:
en/docs/DekiScript/Beginner's_Guide/Entering_a_Script_on_a_Page
So far so good. This type of script is commonly used for small "quickies": one-liners or little bits intermingled with other content, but it technically can be used for large scripts as well.
How did I get the samples to show (like this: {{ page.path }}) without being interpreted as a script? I simply selected the text and assigned it a style of "Plaintext" using the style menu in the editor toolbar. There are a lot of wrappers that can be applied around the script that will inhibit interpretation by the wiki. Generally, when you select your script in the editor, the toolbar should show Format: "Normal" and Style: <blank>. In other words, it is like any ordinary text you enter into the editor.
A DekiScript must not span multiple paragraphs. Therefore, you cannot hit enter in the middle of a script, because that tells the WYSIWYG editor to create a new paragraph. Instead, if you want a multi-line script, you should use shift-enter, which will simply insert a line-break (<br />), which is fine.
| Code | Result | Reason | |
| Wrong | {{ "Two lines "; "of code" }} | {{ "Two lines "; "of code" }} | Don't use "enter" inside a script |
| Right | {{ "Two lines "; "of code" }} | Two lines of code | "shift-enter" works fine |
What if we want to style the output of the script? The script interpreter ignores any styles applied inside the double-braces; it just looks at the text. But if the entire script is enclosed in the style, then it'll work fine. If you want to apply styles to only parts of your script, you have some different options. This table summarizes this operation.
| Code | Result | Reason | |
| Wrong | {{ "Italicize the last word" }} | Italicize the last word | Styles inside a script are ignored |
| Right | {{ "Italicize the whole thing" }} | Italicize the whole thing | Styles applied to a whole script (including double-braces) work fine |
| Right | {{ "Italicize the last" }} {{ "word" }} | Italicize the last word | Each script is handled separately |
| Right | {{ "Italicize the last "; <em> "word" </em> }} | Italicize the last word | Use XML literal |
Don't worry about this too much right now, but store it away in the back of your mind because one day you will need it.
The most common error when working in this mode is that after you save the page, you'll see your script (or parts of it) staring at you, double-braces and all, rather than the script output. This usually suggests one of the following problems:
For any script longer than one or two lines, you're much better off using a block of Style=Dekiscript in the WYSIWYG editor (commonly referred to as a "DekiScript block"). You can create such a block by placing the cursor on a blank line, and selecting "Dekiscript" in either the "Insert" menu to the right of the Style menu (Mindtouch 2010) or the Style menu at the lower left corner of the editor toolbar (Mindtouch 2009 or earlier).
The new block will look something like this (note that this is skin dependent!):

Inside this block, you can enter your script. And, unlike with double-braces, here you can enter multi-line scripts normally, using the "enter" key. Shift-enter is now used to move the cursor out of the block.
Remember, inside this block you don't use double-braces. This is an alternative.
In HTML source terms, this approach is equivalent to placing your code inside a block of type <pre class="script">. If you peek into the HTML source editor for a moment, you'll see it. Look, but don't touch!
When we used double braces, we could (for instance) italicize the script's output by simply grabbing the whole script (including the braces) and applying the desired style using the WYSIWYG editor toolbar. You can't do that here, though. If you want to style the output of your script, your best options are:


Both of these will produce this output:
ENTERING A SCRIPT ON A PAGELet's say you go peek at the HTML source of the first example above. You will see this:
<pre class="script"> var title = string.toUpper(page.title); <em> title </em>;<br /></pre>
Note that the the < and > characters have been replaced by their respective HTML entities, < and >. Is this correct, or has the system messed up my code? In fact it is exactly correct: when you enter XML literals into a Dekiscript block, it's merely text on the wiki page. It is the DekiScript interpreter's job, after extracting the text from the <pre class="script"> block, to correctly interpret the XML and output the appropriate HTML to the web page.
So, unless you have a good reason and/or have become very comfortable with the way this all works, don't touch your Dekiscript blocks in the HTML source editor. It will only make you crazy.
A last note about using the <pre> script block: if you are creating templates that use a save: command, you must use the non-pre-formatted editor (WYSIWYG or source). There currently is no recognition by MindTouch of the save: parameter inside the formatted block, and the parser will choke if you try to use it. This paragraph probably makes no sense to you right now: go read the section on Save: and Edit: to further clarification.
It is possible to embed DekiScript in special attributes in the HTML source of your page, as described here. For the most part, support for XML literals in Lyons has made this approach unnecessary, but you may still run into some older code that uses this technique.
This coding approach is now so strongly deprecated (by me at least) that, rather than explain how to use it, we will instead focus on understanding why you don't want to use it:
Having done what we can to steer you away from this approach, we will say that you certainly can accomplish useful work this way, and there are even some advantages to it. But for new code, avoid it if you can.
Despite the above warnings, there are precisely one and a half good reasons to code in the HTML:
If you do find yourself hacking DekiScript in HTML statements in the source editor, there are some things you can do to keep sane:
<TODO>
Topics to be covered:
There are a couple of seldom-used but interesting and occasionally useful ways to change the behavior of scripts enclosed in double-braces: "save:" and "edit:", either of which you would place at the beginning of your script.
A script with "Edit:" at the beginning will behave as follows. When the editor is opened on the page, the script is executed, and its output is placed into the editor instead of the script. Let's say I have this script on a page:
{{ edit: web.link(user.uri, user.name) }}
When I edit the page, I won't see the script in the editor; rather, I'll see
This is used exclusively inside templates, usually in conjunction with wiki.create(), to pre-initializae data on the page.
A script with "Save:" at the beginning will evaluate the script when the document is saved, and store the results (rather than the script itself) into the page. So, if I entered this script in the editor:
{{ save: web.link(user.uri, user.name) }}
When I save the document, it will replace that code with neilw.
| File | Version | Size | Modified | |
|---|---|---|---|---|
| ||||
| ||||
| ||||
Copyright © 2011 MindTouch, Inc. Powered by