1 | # -*- coding: utf-8 -*-
|
---|
2 | # GitHub http://gist.github.com/3247
|
---|
3 | from django import template
|
---|
4 | from django.template import resolve_variable
|
---|
5 |
|
---|
6 | register = template.Library()
|
---|
7 |
|
---|
8 | '''
|
---|
9 | ROR link_to_remote clone
|
---|
10 |
|
---|
11 | Example 1:
|
---|
12 | {% ajax %}
|
---|
13 | link_to_remote
|
---|
14 | url:/dashboard/serverstatus/
|
---|
15 | update:#server_status_body
|
---|
16 | data:maxcount=10
|
---|
17 | {% endajax %}
|
---|
18 |
|
---|
19 | Example 2:
|
---|
20 | {% ajax %}
|
---|
21 | link_to_remote
|
---|
22 | url:/dashboard/server/
|
---|
23 | update:#server
|
---|
24 | {% endajax %}
|
---|
25 | '''
|
---|
26 |
|
---|
27 | def parseAjaxCmd(output):
|
---|
28 | """parse ajax command"""
|
---|
29 | ajax_template={'link_to_remote':'''
|
---|
30 | <script>
|
---|
31 | $.ajax({
|
---|
32 | type:'GET',
|
---|
33 | url:'%(url)s',
|
---|
34 | dataType:'html',
|
---|
35 | data: "%(data)s",
|
---|
36 | success: function(msg){
|
---|
37 | $("%(update)s").html(msg)
|
---|
38 | }
|
---|
39 | })
|
---|
40 | </script>
|
---|
41 | '''}
|
---|
42 | output=filter(lambda x:x!='',
|
---|
43 | map(lambda x:x.strip(),
|
---|
44 | output.split('\n')
|
---|
45 | )
|
---|
46 | )
|
---|
47 | ajaxcmd,params=output[0],output[1:]
|
---|
48 | tmpl=ajax_template.get(ajaxcmd,'')
|
---|
49 | if tmpl=='':return ''
|
---|
50 | param_dict={'url':'/','data':'','update':'results'}
|
---|
51 | param_dict.update(
|
---|
52 | dict( [p.split(":") for p in params] )
|
---|
53 | )
|
---|
54 | print "param_dict",param_dict
|
---|
55 | return tmpl%param_dict
|
---|
56 |
|
---|
57 | class AjaxTagsNode(template.Node):
|
---|
58 | def __init__(self,nodelist):
|
---|
59 | self.nodelist = nodelist
|
---|
60 | def render(self, context):
|
---|
61 | output = self.nodelist.render(context)
|
---|
62 | return parseAjaxCmd(output)
|
---|
63 |
|
---|
64 | @register.tag('ajax')
|
---|
65 | def ajaxtag(parser, token):
|
---|
66 | """
|
---|
67 | {% ajax %}
|
---|
68 | {% endajax %}
|
---|
69 | """
|
---|
70 | nodelist = parser.parse(('endajax',))
|
---|
71 | parser.delete_first_token()
|
---|
72 | return AjaxTagsNode(nodelist)
|
---|
73 |
|
---|