Examples
This page provides you with several examples of how CHL may and can be used.
See the tutorial for a guide on how to use CHL properly. Also see FastCGI, on how to use CHL with FastCGI.
Contents
Simple Hello World Application
FastCGI Example
Addition of two integers, POST method
Examples
Simple Hello World Application
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!
FastCGI Example
test.vw (view file)
... <body> <{print("This is the CHL FastCGI implementation.");}> </body> ...
index.c (controller file)
#include <chl/chl.h> #include <chl/fcgi.h> int main() { while(chl_fcgi_next()) chl_view("test.vw"); }
Output: This is the CHL FastCGI implementation.
Addition of two integers, POST method
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="num2" /> <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