the server will now serve up templates for a plugin automatically in

a synthesized module
This commit is contained in:
Kevin Dangoor 2010-05-03 15:38:01 -04:00
Родитель 41e55aef46
Коммит d922ecc1c3
4 изменённых файлов: 58 добавлений и 1 удалений

Просмотреть файл

@ -1360,6 +1360,47 @@ def plugin_gallery(request, response):
result.append(data)
return _respond_json(response, result)
@expose(r'^/plugin/templates/(?P<plugin_name>.*)/$', 'GET', auth=False)
def plugin_templates(request, response):
plugin_name = request.kwargs['plugin_name']
if ".." in plugin_name:
raise BadRequest("'..' not allowed in plugin names")
if request.user:
path = get_user_plugin_path(request.user)
else:
path = []
path.extend(c.plugin_path)
plugin = plugins.lookup_plugin(plugin_name, path)
if plugin is None:
return _plugin_does_not_exist(response, plugin_name)
templates = plugin.templates
if not templates:
raise FileNotFound("Plugin %s has no templates" % plugin_name)
response.content_type = "text/javascript"
response.body = _wrap_script(plugin_name, "templates", """
var jsmt = require('jsmt');
var templates = jsmt.compileAll(%s);
exports.render = function(name, data) {
if (!templates[name]) {
throw new Error("Unknown template: " + name);
}
return templates[name](data);
};
for (var key in templates) {
exports[key] = templates[key];
}
""" % (simplejson.dumps(templates)))
return response()
class FileIterable(object):
def __init__(self, filename):
self.filename = filename

Просмотреть файл

@ -114,9 +114,14 @@ class Plugin(BasePlugin):
if not server_base_url.startswith("/"):
server_base_url = "/" + server_base_url
name = self.name
resources = list()
md['tiki:resources'] = resources
if (self.location / "templates").isdir():
resources.append(dict(type="script", url="%splugin/templates/%s/"
% (server_base_url, name), name="templates",
id="%s:templates" % name))
if self.location_name == "user":
resources.extend([
dict(type='script',

Просмотреть файл

@ -0,0 +1,3 @@
This is a js micro template. Watch:
1+1 <%= 1+1 %>

Просмотреть файл

@ -157,6 +157,7 @@ def test_default_plugin_registration():
assert "plugin1" in response.body
assert "plugin/script/testplugins/plugin1/thecode.js" in response.body
assert "plugin/file/testplugins/plugin1/resources/foo/foo.css" in response.body
assert "plugin/templates/plugin1/" in response.body
assert "NOT THERE" not in response.body
data = loads(response.body)
md = data["plugin1"]
@ -171,6 +172,13 @@ def test_get_script_from_plugin():
content_type = response.content_type
assert content_type == "text/javascript"
assert "this is the code" in response.body
def test_get_templates_from_plugin():
response = app.get("/plugin/templates/plugin1/")
content_type = response.content_type
assert content_type == "text/javascript"
print response.body
assert "js micro template" in response.body
def test_get_script_bad_plugin_location():
response = app.get("/plugin/script/BOGUSLOCATION/plugin1/thecode.js",