Structuring Lapis Projects

2015, Feb 04, 11:01 pm

One of the hidden coding challenges is organization. Code can quickly become unwieldy when not arranged and rearranged for clarity and modularity.

This short article is an introduction to how code organization seems to work for Lapis projects, and the necessary statements for accomplishing this task.

Lapis projects consist of a great many disparate components. Understanding these components in the first step towards an organized code base, which is in turn good for extending, maintaining, and debugging your project.

As a template we can use the open source website from leaf corcoran, the creator of Lapis.

There are three keywords we use for pulling together code from the various folders: require, import, and @include.

import * from require "..."

While this statement is used in Lapis, it comes directly from moonscript, as documented right here.

One of the interesting things that appears to be missing from the official Lapis documentation, is the use of import and require outside of accessing the built in Lapis tables. The example found throughout and elsewhere are examples like the following:

From within an app, something like:

import Users from require "models"

And within models/users.moon:

class Users extends Model

There are many more examples within the source code. It's well worth reading.


Our central application is app.moon, which holds all the routes. Of course holding all routes in a single file is very bad for proper code organization. Luckily Lapis provides a solution.

We can make use of the @include function which allows for the inclusion of sub-applications, as described in the official documentation. Thus we can hold various separable 'applications' in an applications folder. Example applications might be users.moon, admin.moon, etc.

More Organizational Options

The other organizational technique found in is the use of additional folder like helper, for holding reusable code snippets, like those for CSRF, or image handling, etc.

Aside from that, we also have the static as created during Lapis application creation.


Add a comment