RPGWEB and simple web pages
Javascript frameworks and web 2.0 are a big deal however, you don’t always need them for something simple. Or perhaps it is a simple internal tool for your company, and a quick HTML page fits your needs perfectly. Well, embedded RPG templates have been added to RPGWEB. Let’s take a quick look at an example.
ctl-opt bnddir('RPGWEB') option(*nodebugio:*srcstmt);
/include RPGWEB/QRPGLESRC,RPGWEB_H
dcl-ds app likeds(RPGWEBAPP) inz;
RPGWEB_setView(app : '/home/dlong/web_apps/public');
RPGWEB_get(app : '/test' : %paddr(TEST_index));
RPGWEB_start(app: 3031);
*inlr = *on;
return;
dcl-proc TEST_index;
dcl-pi *n likeds(RPGWEBRSP);
request likeds(RPGWEBRQST);
end-pi;
dcl-ds response likeds(RPGWEBRSP);
dcl-ds data qualified;
field1 char(50);
field2 packed(11:0);
end-ds;
data.field1 = 'Daniel';
data.field2 = 8675309;
RPGWEB_setStatus(response : HTTP_OK);
RPGWEB_setBody(response : RPGWEB_render(app : 'index' : data));
return response;
end-proc;
We can see a couple of new procedures. RPG_setView and RPGWEB_render. RPG_setView sets the location in the IFS where the RPG template is to be found. The RPGWEB_render method passes the data to the template, and then creates HTML page, then returns it. The template should always end in `.erpg`. So here we are looking for the file `index.erpg`. We are passing the application so we will know where to look for the template and then the data that is used in the template. The following is the `index.erpg` that for the example.
<%=
dcl-ds data qualified;
field1 char(50);
field2 packed(11:0);
end-ds;
data = RPGWEB_injected_data();
=%>
<html>
<%
if 1 = 0;
RPGWEB_write('Hello World');
else;
RPGWEB_write('Hello ' + %trim(data.field1));
endif;
%>
<br />
<%
RPGWEB_write('<br /> ' + %char(data.field2));
%>
</html>
At the top of the template, you see the data structure defined again here. I am trying to work on this so you don't have to redefine it. For now, this works. You see we grab the injected data and push it into this data data structure. then we can use it inside of the logic blocks. The output is super lame, but it serves the purpose of showing what can be done.
This uses standard RPG concepts. This isn't super fast right now, but we are working on that. I hope you find this feature helpful. Lemme know. If you build something with it, I would like to see it. Check the project out at the following on GitHub. Link below.