Talk:Programmer's Guide to awx

From X-Wrt

Jump to: navigation, search

Contents

Original awx author's documentation

This page is a copy of the awx author's documentation. It is neither complete or comprehensive. :-)

awx is a web development framework designed for small embedded systems.
It tries to cut down on the amount of redundant code that is typically written
for embedded web interfaces.
To make web pages more readable, it encourages separating the html code from
the web logic, config interaction, etc.

Example:

views/layout.ahtml:
------------------------
<html>
	<head><title><% TITLE %></title></head>
	<body>
		<h1><% TITLE %></h1>
		<p>
			<% render(RENDER) %>
		</p>
	</body>
</html>

views/helloworld.ahtml:
------------------------
<h2>Hello world!</h2>
<a href="<% ENVIRON["SCRIPT_NAME"] %>?action=time">Show Date/Time</a> |
<a href="<% ENVIRON["SCRIPT_NAME"] %>?action=version">Show Kernel version</a><br />
<br />
<% message %>

helloworld.awx:
------------------------
BEGIN {
	TITLE="A simple test page"
	RENDER="views/helloworld.ahtml"
}

function handle_default() {
	# do not display any message in the default handler
}

function handle_time() {
	"date" | getline
	message="Current Date/Time: " $0
}

function handle_version() {
	getline < "/proc/version"
	message = "Kernel version: " $0
}


As you can see in this example, awx makes a few assumptions of how your web pages are structured.
This allows you to skip some code that you would otherwise have to write (e.g. include a header, 
include a footer, if/else chain to check a cgi variable for which action should be run).
Typically you will want to use a common layout for almost all pages, so awx uses 'views/layout.ahtml'
as the default for that. You can override this by changing the LAYOUT variable either from the 
BEGIN{} section or the handler itself.
Pages are processed in the following order:

1. BEGIN{}
   - will be run for all 'actions' of the script. can set up common defaults like page title, layout, etc.
2. The action handler
   - awx will check the contents of the ACTION variable, which can be set in the BEGIN{} section. If this
     variable is unset, it will use the contents of the cgi parameter 'action' or (if that one is empty as
     well) simply fall back to 'default'
   - when calling the action, it gets a prefix of 'handle_', so that you can't call arbitrary awk functions
     by overriding the CGI parameter
3. The page view
   - The page view typically contains the HTML formatting of the page. It can be selected using the RENDER
     variable.

CGI parameters will be processed at the first attempt of reading a variable, either explicitly from BEGIN{}
or implicitly when looking for the page action to call


awx extends awk by a few common functions that make writing web pages easier:

- include(file): includes an awk file and executes its BEGIN{} section
   - must not contain any line handlers for input files
- render(file): show a file and substitute all <% %> parts
   - typically done implicitly for LAYOUT and RENDER, but can be used explicitly as well
- getvar(name): get the contents of a CGI variable


---------------
i have @if with @else and @for fully working
so you can do 
<% @for i in items %> Item <% i %>: <% items[i] %>
<% @end %>

it even handles translations like @TR<<<% foo %>>>
rewritten template parser for awx - uses @for, @if, @else, @end for basic flow control

What is missing to make the awx framework perfect

awx is still in a "flow" state, and may be changed/improved for the following

Template ahtml

Do something smart so that there is only bad excuses for having html code in .awx files

Forms

Do something

Validation

Need a routine for validation?

Commit

Need a routine for commit?

Personal tools