Introduced
http://keplerproject.github.io/orbit/
Orbit is a LUA-language version of the MVC framework.
This framework completely discards the Cgilua script model, supports the application, each application can unload a separate file, and of course you can also split it into a file when you need it.
This framework runs on servers in the WSAPI protocol, so it can work on xavante and some CGI and fastcgi programs.
Orbit is a MVC web framework for Lua. The design is inspired by lightweight Ruby frameworks such as Camping. It completely abandons the Cgilua model of "scripts" in favor of applications, where each Orbit application can fit in a s Ingle file, but you can split it into the multiple files if you want. All Orbit applications follow the WSAPI protocol, so they currently work with Xavante, CGI and Fastcgi. It includes a launcher that makes it easy to launch a Xavante instance for development.
Installation Run
Http://keplerproject.github.io/orbit/example.html
This program relies on SQLite.
Depending on your configuration, you might need to install the and rocks before running the luasql-sqlite3 markdown application. Now just start Xavante, and point your browser to blog.ws, and you should see the index page of the blog. If you created a blog.db from scratch you is not going to see any posts, though. The blog application in ' Samples/blog ' includes a blog.db filled with random posts and comments.
Installation:
Luarocks Search SQL
Apt-get Install Sqlite3
Luarocks Install Luasql-sqlite3 sqlite_dir=/usr/local
Run
[Email protected]:/usr/local/lib/luarocks/rocks/orbit/2.2.4-1/samples/blog# orbit-p 8181 Blog.lua
Starting Orbit Server at Port 8181
Use the Firefox SQLite plugin to view the DB
App interface
Using orbit.new constructs an app environment that can reference global variables,
It also defines the interface that the blog application inherits from the orbit project, such as the get interface of the route Dispatch_get
Local blog = setmetatable (Orbit.new (), {__index = _g}) If _version = = "Lua 5.2" then _env = Blogelse setfenv (1, b LOG) End
orbit.newInjects quite a lot of stuff in the blog module ' s namespace. The most important of these is dispatch_get the, dispatch_post , and methods that let you model define the main functionality of the APPL Ication.
Creating Models
The model role is responsible for querying the data from the database, which creates and modifies the data.
Our blog application have three kinds of objects:posts, comments, and "static" pages (things like a "about" page for the Blog, for example). It's no coincidence that we also has three tables in the database, each table maps to a kind of object our application ha Ndles, and for each kind we'll create a model. We first create a model object for posts:
Posts = Blog:model "POST"
function posts:find_recent () return Self:find_all ("Published_at is not NULL", {order = "Published_at desc", count = Recent_count}) End
Defining Controllers
For the requested URL, the execution accesses what data, how to respond. Access data calls the model object, which can be either HTML or XML JSON.
Controllers is the interface between the Web and your application. With Orbit your can map the path part of your application's URLs (in Http://myserver.com/myapp.ws/foo/bar the path is/foo/ Bar, for example) to controllers. In Lua terms, the Orbit controller is a function that receives a Request/response object (usually called web ) plus Parame Ters extracted from the path, and returns text which is sent to the client (usually HTML, but can was XML, or even an image) .
Get request a URL, call model to get data
function index (web) local PS = posts:find_recent () Local ms = Posts:find_months () Local pgs = PGS or Pages:fin D_all () return Render_index (web, {posts = PS, months = MS,
Post modifies the data, invokes the model, and saves the data.
function add_comment (web, post_id) Local input = Web.input if String.find (input.comment, "^%s*$") t Hen return view_post (Web, post_id, true) Else local comment = comments:new () comment.post_id = Tonumber (P ost_id) Comment.body = markdown (input.comment) if not String.find (Input.author, "^%s*$") then Comment.author = Input.author End If not String.find (Input.email, "^%s*$") then Comment.email = Input.email End If not String.find (Input.url, "^%s*$") then Comment.url = Input.url End Comment:save () local post = pos Ts:find (Tonumber (post_id)) post.n_comments = (post.n_comments or 0) + 1 post:save () cache:invalidate ("/") Cache:invalidate ("/post/": post_id) cache:invalidate ("/archive/": Os.date ("%y/%m", Post.published_at)) r Eturn Web:redirect (Web:link ("/post/": post_id)) Endendblog:dispatch_post (Add_comment, "/post/(%d+)/addcomment")
Views:generating HTML
A simple function of a view definition that generates view content, such as HTML, in a function.
It is possible to return the rendered content directly from the controller, but it is also recommended to detach the controller and view, which is a good programming practice.
Views is the last component of the MVC triad. For Orbit views is just simple functions that generate content (usually HTML), and is strictly optional, meaning you can Return content directly from the controller. But it's still good programming practice to separate controllers and views.
You can use concat to stitch strings into view results, or use a third-party template library to render the data obtained from model using the template engine.
Orbit provides the HTML and XML generator used orbit.htmlify, but you also have the freedom to choose other methods that you want to use.
How do you generate content are up to you:concatenate Lua strings, use table.concat , use a third-party template Library ... Orbit provides programmatic html/xml generation through orbit.htmlify , but is free to use any method you want. In this tutorial we'll stick with programmatic generation, though, as the other methods (straight strings, Cosmo, etc.) is thoroughly documented elsewhere.
Orbit.htmlify
function layout (web, args, inner_html) return html{ head{ title (blog_title), meta{["http-equiv"] = " Content-type ", content =" text/html; Charset=utf-8 "}, link{rel = ' stylesheet ', type = ' text/css ', href = web:static_link ('/style.css '), media = ' scre En '} }, body{ div{id = "Container", div{id = "header", title = "SiteName"}, div{id = "Mainnav" , _menu (web, args) }, div{id = "Menu", _sidebar (web, args) }, div{id = "Contents", inner_html}, div{id = "Footer", Copyright_notice}}} } end
Cosmo Background Template engine
Data and view separation is always mentioned in the MVC architecture,
If you use Orbit.htmlify mode, you can only write LUA code that conforms to the DOM structure, for the front-end code if it can be written in HTML, similar to JSP format, but also better than the orbit provides a way to maintain, front-end more convenient customization.
Look at Lua's background template engine
http://cosmo.luaforge.net/
Overview
Cosmo is a "safe templates" engine. It allows nested templates, providing many of the advantages of the Turing-complete template engines, without with The downside of allowing arbitrary code in the templates.
Simple Form Filling
Let's start with a simple example of Filling a set of scalar values into a template: Here is a few examples of Cosmo in use:
> values = {rank= "Ace", suit= "Spades"} > Template = "$ra NK of $suit "> Require (" Cosmo ") > = Cosmo.fill (template, values) Ace of spades
Note that the template is a string that marks where values should go. The table provides the values. $rank would get replaced by Value.rank ("Ace") and $suit 'll get replaced by Value.suit ("Spades").
Cosmo.fill () takes-parameters at once. Cosmo also provides a "shortcut" Method F () which takes only one parameter–the Template–and returns a fun Ction that then takes the second parameter. This allows to a more compact notation:
> = cosmo.f (template) {rank= "Ace", suit= "Spades"} Ace of Spad Es
LUA MVC framework Orbit approach