-
Notifications
You must be signed in to change notification settings - Fork 10
Examples
it4e edited this page Apr 17, 2016
·
15 revisions
This page provides you with several examples of how CHL may and can be used.
See tutorial for a guide on how to use CHL properly.
index.vw (view file)
...
<body>
<{print("Hello world!");}>
</body>
...
index.c (controller file)
#include <chl/chl.h>
int main() {
chl_view("index.vw");
}
Output: Hello world!
Takes two numbers from POST and outputs their sum.
index.vw (view file)
...
<body>
<{sum();}>
<form method="post" action="">
<input type="text" name="num1" />
<input type="text" name="sum2" />
<input type="submit" name="submit" value="Calculate" />
</form>
</body>
...
A simple POST form, with two text input fields and a submit button. CHL function sum() is called every page request.
index.c (controller file)
#include <chl/chl.h>
#include <stdlib.h>
#include <stdio.h>
// Output sum of two numbers
void sum(char * args) {
int num1, num2;
// Do nothing if form-data has not been sent
if(! chl_post("submit")
return;
// Get POST data in integer form
num1 = chl_posti("num1");
num2 = chl_posti("num2");
printf("%d", num1 + num2);
}
int main() {
// Append function sum so it can be used inline
chl_func_append("sum", sum);
chl_view("index.vw");
}
Appends and defines the function sum so it can be used inline, then it calls the view file index.vw.
Input: num1=10, num2=40
Output: 50