3 ms·
Very interesting... Made me jot down a non-mini, non-existent web framework I think I might like to use.... It's only just a tad bit more compact than Tir and
by meric 16y ago
Very interesting...
Made me jot down a non-mini, non-existent web framework I think I might like to use.... It's only just a tad bit more compact than Tir and will take a lot more effort to implement... but (I hope) it is much easier to tell at a glance what it is doing. Must find time for building my own web framework some day.
Corresponding Arc Challenge: (cheated a bit, if you want to make this 'proper' you'd need to add a cookie, and maybe another function.)
require "filter"
require "form"
require "response"
require "route"
said_form = form.default { saying = form.charfield() }
function said_page(args)
return response.http(said_form.render())
end
function said_click(args)
return response.http(
([[<a href="javascript:document.write(%s)">click here</a>]])
:format(args.post.saying))
end
route.set(filter.get, "^/said/$", said_page)
route.set(filter.post, "^/said/$", said_click)
Login / Logout
require "filter"
require "template"
require "form"
require "widget"
require "response"
require "route"
require "generic"
require "css"
login_form = form.default({
username = form.charfield(),
password = form.charfield{widget = widget.password}
})
function login_form:clean(args, data)
if authenticate(data.username, data.password) then
return true
end
args:set_error(self, {'username or password is incorrect'})
args:set_data(self, args.post)
return false
end
function login_check(request)
if request.session['user'] then
return nil, response.redirect("/")
end
return filter.get(request)
end
function login_page(args)
return response.http(template.default.render_with {
head = css.link("my.css"),
body = login_form.render()
})
end
function login_auth(args)
if login_form:clean(args, args.post) then
return response.redirect("/")
end
return login_page(args)
end
route.set(login_check, "^/login/$", login_page)
route.set(filter.post, "^/login/$", login_auth)
route.set(filter.any, "", generic.not_found)
- zedshaw 16y agoNice, yeah it's really not hard to crank these little frameworks out. In fact I'm starting to think, if doing one is so simple, then maybe just little frameworks from now on. One thing though: Part of the point of Tir is that you can read the code to one function and know what it's doing. For example, if I showed this to someone and asked, "What happens after faile to login?" They'd have to trace through routing to figure it out. Not that I'm saying that's bad, since the advantage is you don't have to save any state. I'm just saying that's what's different about Tir (and Arc or Seaside).