Tuesday, March 29, 2011

Nitrogen, an Erlang web application library/framework Part 7

Slide 71: Extending Nitrogen: Custom Elements - Part 1


Picking up again, ./dev element my_element won't run unless nitrogen is running. So in one window:

bash-4.1$ ./dev element my_element
Node is not running!
bash-4.1$ ./nitrogen console
Exec: /home/sps/erlang-libs/nitrogen/rel/nitrogen/erts-5.8.2/bin/erlexec -boot /home/sps/erlang-libs/nitrogen/rel/nitrogen/releases/2.0.4/nitrogen -embedded -config /home/sps/erlang-libs/nitrogen/rel/nitrogen/etc/app.config -config /home/sps/erlang-libs/nitrogen/rel/nitrogen/etc/mochiweb.config -args_file /home/sps/erlang-libs/nitrogen/rel/nitrogen/etc/vm.args -- console
Root: /home/sps/erlang-libs/nitrogen/rel/nitrogen
Erlang R14B01 (erts-5.8.2) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:5] [kernel-poll:true]

Eshell V5.8.2 (abort with ^G)
(nitrogen@127.0.0.1)1> Starting Mochiweb Server (nitrogen) on 0.0.0.0:8000, root: './site/static'

...and in the other window:

bash-4.1$ ./dev element my_element
Created element: ./site/src/elements/element_my_element.erl
Remember to recompile!
bash-4.1$

Slide 72: Extending Nitrogen: Custom Elements - Part 2



OK, record and two mandatory functions.

Slide 73: Extending Nitrogen: Custom Elements - Part 3



Hmmmm. The tutorial presents only one of the two mandatory functions, and no record. Let's see how creative my typos are this time.



Ah, the missing pieces are dummied in the ${Root}/src/elements/element_my_element.erl file, along with an instruction to move the record definition to the ${Root}/include/records.hrl. I'll leave them be, and alter only the render_element function.


Slide 74: Extending Nitrogen: Custom Elements - Part 4



After replacing the contents of my_page.erl following the title/0 definition with the recommended code, and recompiling (fixing variable name typos until getting a clean compile),
the page http://localhost:8000/my/page works as expected.



OK; so if you have a number of form elements that repeatedly occur together, then you can bundle them up with this kind of "custom" element. It's not yet clear to me if this would be useful for an entirely new kind of element, e.g., a "treebutton" which would toggle between expanded and collapsed representation of a tree.

No comments:

Post a Comment