Changes from Version 1 of documentation/LuaManual/EmbeddingScript

Show
Ignore:
Author:
gambini (IP: 96.38.104.126)
Timestamp:
05/19/12 07:39:11 (5 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • documentation/LuaManual/EmbeddingScript

    v0 v1  
     1[[PageOutline(1-5, Contents)]] 
     2= Embedding Lua script = 
     3 
     4When using the Lua plugin, Lua code can be embedded into RML files. Inline responses to events are executed as Lua code. Functions, structures and variables can be declared or included with the <script> tag, then referenced from the inline code. 
     5 
     6== Inline event responses == 
     7 
     8The Python plugin installs an event listener instancer to execute inline event responses as Python code. For example, in the following sample the element will run the print command when it is clicked: 
     9 
     10{{{ 
     11<button onclick="print 'Hello world!'" /> 
     12}}} 
     13 
     14Separate lines can be separated by semi-colons like Javascript, or just by a space like Lua. 
     15 
     16{{{ 
     17<button onclick="print 'Hello'; print 'world!'" /> 
     18}}} 
     19The only limitation is that string literals must be in single quotes (' ') rather than double quotes (" ") because of the way that double quotes signal an end of RML attributes. 
     20 
     21Three local variables are accessible to inline event handlers. These are: 
     22 * ''event'': The event currently being processed (ie, the event that triggered the handler). 
     23 * ''element'': The element currently responding to the event. 
     24 * ''document'': The owner document of the current element. 
     25 
     26{{{ 
     27<button onclick="print element.tag_name" /> 
     28<button onclick="print event.mouse_x .. ', ' .. event.mouse_y" /> 
     29}}} 
     30 
     31See the [wiki:documentation/LuaManual/Elements element], [wiki:documentation/LuaManual/Documents document] and [wiki:documentation/LuaManual/Events event] documentation for their full Lua interfaces. 
     32 
     33== Embedding Python into RML == 
     34 
     35Python code can be embedded into an RML document with the <script> tag. Similarly to Javascript, the script can be included from a separate file with the ''src'' attribute, or otherwise declared inline as loose content within the <script> tag. Any code embedded in this manner will be compiled with the document and will be available to inline event handlers in the RML. For example, the following document declares a Python function in the <script> tag and calls it from an element's ''onclick'' hander. 
     36 
     37{{{ 
     38<rml> 
     39        <head> 
     40                <script> 
     41function Test() 
     42        print 'Hello world!' 
     43end 
     44                </script> 
     45        </head> 
     46        <body> 
     47                <button onclick="Test()">Continue</button> 
     48        </body> 
     49</rml> 
     50}}} 
     51 
     52The following sample uses the ''test.lua'' file instead of declaring the script inline (it is assumed the Lua file declares a ''Test()'' function). 
     53 
     54{{{ 
     55<rml> 
     56        <head> 
     57                <script src="test.lua"> 
     58        </head> 
     59        <body> 
     60                <button onclick="Test()">Continue</button> 
     61        </body> 
     62</rml> 
     63}}} 
     64 
     65A document can include multiple <script> tags.