Anatomy of a Nitrogen Page

Jesse Gumm edited this page Jul 6, 2013 · 3 revisions

(Note, this help is outdated as of Nitrogen 2.x and is only applicable to Nitrogen 1.0 and below)

A Nitrogen page in just an Erlang module that follows a certain naming convention and exposes certain functions.

Naming Conventions

Nitrogen is usually configured so that calls under the /web/ url path are handled by Nitrogen. Therefore, Nitrogen pages usually begin with the prefix “web_”. If Nitrogen were exposed under a different path, then the prefix should correspond to that path.

If you instead configured Yaws or Mochiweb so that calls to /app/ were handled by Nitrogen, then your Nitrogen pages would begin with the prefix “app_”.

This naming convention assures that only your Nitrogen page modules can be called through the web.

Any slashes in the URL path are replaced by underscores in the module, so http://localhost/web/user/list would call the web_user_list module, in the file web_user_list.erl.

Functions to Expose

A Nitrogen page includes two standard functions, main/0 and event/1.

The Main Function

The main/0 function is called when the page is first requested. Generally, in this function you will build a tree of Nitrogen elements and then call wf:render/1 to turn the elements into HTML.

The main/0 function should return the output of wf:render/1, plain html, or whatever content you intend to send to the browser.

The Event Function

The event/1 function is called when a page posts back an event. Generally, in this function you examine the parameters of the event, run some server side logic, and then update an element on the page via Ajax by using wf:update, wf:insert_top, or wf:insert_bottom.