2 of 2 found this page helpful

Entering a Script on a Page

    Table of Contents

    Introduction

    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".

    Three different ways to enter a script

    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.

    Method #1: Double-braces

    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.

    Making sure the script is a script

    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.

    Mult-line input

    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

    Styling the script output

    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.

    When things don't work

    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:

    1. The script is in a formatted or styled block, and is not Format:"Normal", Style:<blank>.
    2. You have a carriage return in there somewhere (and therefore the script spans multiple paragraphs, which doesn't work).
    3. You do not have a proper matching set of {{ and }}.
    4. You have some other HTML elements hidden in the script that are breaking things.  Try selecting the entire script and clicking the "Remove format" tool (looks like an eraser, to the right of the "Source" button) in the toolbar.

    Method #2: DekiScript blocks

    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!

    Applying Styles to the block

    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:

    1. Apply the styles directly inside the script, using XML literals, like this:dekiscript_style1.png
    2. Wrap the entire script in a <div> with the desired styles:

    Both of these will produce this output:

    ENTERING A SCRIPT ON A PAGE

    What happened to my HTML source?

    Let'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);
    &lt;em&gt; title &lt;/em&gt;;<br /></pre>

    Note that the the < and > characters have been replaced by their respective HTML entities, &lt; and &gt;.  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.

    Method #3: HTML Statements

    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.

    Why not to do this

    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:

    1. The HTML source editor is not a fun place to code:
      • All your code is in attributes, enclosed in quotes, so you can't use double-quotes.  This results in a lot of escaping, which is annoying.
      • The editor validates your HTML whenever exiting the HTML source editor (such as when switching back to WYSIWYG or saving the page).  If your HTML is broken, the editor will do what it can to salvage the code, which usually means silently discarding it.  So you can write a bunch of code, save it, and then find that the editor discarded all of it because you inadvertantly used a " inside an attribute.  This only needs to happen to you once (and it will, trust me) to make you want to swear off this approach forever.
      • Lots of useful programming characters need to be escaped into HTML entities (<, >, & are the primary culprits).  This makes the code hard to write and read, and mistakes can sometimes have rather severe consequences.
      • Having giant chunks of code inside attributes tends to make the HTML itself very hard to read.
    2. You can't see or edit the code in the WYSIWYG editor.  You might not even know it's there.  This is generally Not Good, especially when doing copy and paste operations in the WYSIWYG editor.
    3. You generally end up needing to bounce back and forth between the two editors a lot, and it's just plain annoying.

    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.

    Why you might have no choice

    Despite the above warnings, there are precisely one and a half good reasons to code in the HTML:

    1. Old Scripts: Sometimes you have to deal with an old script that uses it, and it's too much work to convert it.  This only counts as half a reason because often (usually?) it is in fact worth the effort to convert the script to a more modern style, especially if you'll be spending any real time modifying or maintaining it.
    2. Global variables:  Variables in DekiScript are local to their block, so the only way (for now) to make variables visible across an entire page (or parts of a page) are to initialize them in HTML statements that enclose the desired scope.  So, you could go into the source editor, wrap a <div> around your entire page, and then put some variable initialization inside an "init" attribute in the <div>.  Those variables will be visible to any DekiScript (entered in any manner) inside the <div>, which means anywhere on the page.  In Mindtouch 2010, this reason has been eliminated, as variables are now intrinsically global across the page.

    Maintaining Sanity in HTML Statments

    If you do find yourself hacking DekiScript in HTML statements in the source editor, there are some things you can do to keep sane:

    • As mentioned, your code will be completely contained within double-quoted attribute values.  So be careful of your use of quotes!  Inside your code, use single-quotes only, or escaped (with a backslash) single or double-quotes.
    • To minimize the chance of disaster, try this trick:
      • Whenever you are ready to switch out of the HTML source editor, first do a "select all" and copy the whole source to the clipboard (ctrl-A ctrl-C).
      • Then go back to the WYSIWYG editor and then immediately back to the source editor.
      • Check if all your code survived.  If so, you're OK to exit as desired.  If not, try to figure out what the problem was, and then recover your code simply by pasting it back from the clipboard.

    Copying and pasting DekiScript

    <TODO>

    Topics to be covered:

    • Copying code from Forum posts
    • Copying code from other pages
    • Copying code from the "View source" mode of the Syntax highlighter

    Edit: and Save:

    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.

    Edit:

    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

    neilw

    This is used exclusively inside templates, usually in conjunction with wiki.create(), to pre-initializae data on the page.

    Save:

    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.

    Was this page helpful?
    Tag page

    Files 3

    FileVersionSizeModified 
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by