Angel \”Java\” Lopez on Blog

January 19, 2013

CobolScript (4) Web Pages with Templates

Filed under: COBOL, CobolScript, JavaScript, NodeJs, Open Source Projects — ajlopez @ 3:47 pm

Previous Post

In the previous post I presented CobolScript generating output using templates. It can be used to generate web page output, too. The sample is at:

https://github.com/ajlopez/CobolScript/tree/master/samples/templateweb

The launch program in JavaScript is simple:

var cobs = require('../..'),
    http = require('http'),
    fs = require('fs');

var program = cobs.compileTemplateFile('./factorial.cobp');

http.createServer(function(req, res) {
    program.run(cobs.getRuntime({ request: req, response: res }));
}).listen(8000);

console.log('Server started, listening at port 8000');

The key part is the call to compile the file template. It produce a compiled JavaScript function to be invoked. The call of program.run executes the template, with a giving runtime context. The runtime context is build giving the request and response objects of the current incoming request. That runtime derives all the output of the CobolScript program to the response output. So, the template doesn’t know about web request and response. You can see the runtime object as a service provider to the compiled CobolScript program. Its properties can be accessed if you define a LINKAGE SECTION as in classic COBOL. But this feature was not used in this simple sample.

The template file

<h1>Factorial</h1>
<p>Page generated by CobolScript, using templates</p>
<table>
<tr><th align='right'>n</th><th align='right'>n!
</th>
</tr>
<#
local n.
perform show-factorial using n varying n from 1 to 10.
#>
</table>
<#
.
stop run.

show-factorial section using n.
local result.
perform factorial using n giving result.
#>
<tr>
<td align='right'>${n}</td><td align='right'>${result}
</td>
</tr>
<#
.

factorial section using n.
local m.
if n = 1 then return n.
subtract 1 from n giving m.
perform factorial using m giving m.
multiply n by m.
return m.
#>

Launch the server with

node server

Navigate to localhost:8000, the result:

Next post: a dynamic web site accessing MySQL database, using CobolScript.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

2 Comments »

  1. [...] Previous Post Next Post [...]

    Pingback by CobolScript (3) Templates « Angel ”Java” Lopez on Blog — January 19, 2013 @ 3:49 pm

  2. This paragraph will assist the internet people for setting up new web site or
    even a weblog from start to end.

    Comment by call conversation — February 5, 2013 @ 12:14 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 65 other followers

%d bloggers like this: