<%doc> first, we define all the various AJAX methods we want to call, and register them with the myghtyjax handler. all of these functions, as well as the myghtyjax initializer functions, can be anywhere, at the bottom of the whole page or even in a totally different file, and also shared by any number of templates. <%once> # a plain python function which will be mapped to a javascript function def myresult (firstname, lastname, email): return "document.getElementById('result').innerHTML = 'well hi %s %s %s';" % (firstname, lastname, email) <%doc> a myghty "hello" method that will be mapped to a javascript function <%method myres> <%args> firstname lastname document.getElementById('result').innerHTML = "hello <% firstname %> <%lastname %>!"; <%doc> a myghty "hello" method whos HTML contents will be returned to a callback function. note that you can also call this component within the original template (or from any other template), not just via XMLHttpRequest. This illustrates multi-contextual templating components. <%method drawtable> <%attr> type='source' <%args> firstname lastname email
First Last Email
<% firstname %> <% lastname %> <% email %>
<%doc> a myghty method to add two numbers, and the resulting HTML contents will be drawn directly to a DOM element. Includes an optional argument "slow" which will cause it to pause 2 seconds. while pointless here, it is meant to simulate an operation that might be doing a long database operation or something similarly time-consuming. <%method addnumbers> <%args> leftop rightop slow = False <%attr> type='writedom' # specify a fixed target DOM id. if # omitted, the target id must be specified # as the first argument to the javascript # function. dom_id='addresult' <%init> import time if slow: time.sleep(2) try: a = str(int(leftop) + int(rightop)) except: a = '' % if slow: After much consideration, I can safely say that <% leftop %> + <% rightop %> = <% a %> ! % else: <% a %> ! that was easy. % <%init> # register the functions with myghtyjax. # if this function returns True, it means myghtyjax # executed the ajax-enabled component, so we return # from execution. # note that autohandlers are not executed within the # ajax call, as myghtyjax.myt, the entry point for # all XMLHttpRequest calls, inherits from None. if m.comp('myghtyjax.myt:init', # local python def myresult = myresult, # local myghty methods add = 'SELF:addnumbers', myres = 'SELF:myres', drawtable = 'SELF:drawtable', # an external template hourglass = 'hourglass.myt', ): return <%doc>Deliver the myghtyajax javascript stub functions. This method takes the optional arguments 'handleruri' and 'scripturi', which reference the web-addressable URI of the myghtyjax.myt file and the myghtyjax.js file. They can also be set as global interpreter attributes. If not supplied, myghtyjax determines them based on where it is being called from. <& myghtyjax.myt:js &>

Myghty Ajax Toolkit

In this example, Myghty is used to create a seamless bridge between client-side Javascript and server-side Python functions, or between Javascript and Myghty template components. You can write regular Python-embedded HTML, put a set of tags around it, and have that component's server-side code and textual output "magically" become available as a regular javascript function, which can return its text, execute server-generated Javascript, or write its output into any DOM element on the page.

index.myt - this page illustrates various functions that utilize myghtyjax
"hourglass" page - this page illustrates how functions can exist in a second page
myghtyjax library / javascript functions - this is the actual library

Try out the functions here. Each click of a button generates a request to the server, whose output is then populated into the current page:
First Name:<& text, name="firstname", value="wile e"&>
Last Name:<& text, name="lastname", value="coyote"&>
Email:<& text, name="email", value="wilee@suprgenius.net"&>


<& text, name="leftop", value="4", size=3 &> + <& text, name="rightop", value="5", size=3 &> =
# for the "slow" version, we call the "hourglass" function first.
<%method text> <%args> name size=20 value = None