/cee_server_pages

A templating language with embedded C syntax.

Primary LanguageCMIT LicenseMIT

“C server pages”

What is it?

A templating engine with embedded C syntax.

Usage

  1. Write the template in any text format you wish. You can include any arbitrary C within ‘{{’ and ‘}}’ snippets. Curly brace literals are escaped with backslashes. Backslash literals are written as two backslashes
    <h1>{{ fprintf(fileptr, "%d", your_variable) }}% complete</h1>
        

    You can also do loops:

       <ul>
       {{ for ( int index = 0; index < 10; index++ ) { }}
    	 <li> {{ fprintf(fileptr, "%d", index); }} </li>
       {{ } }}
       </ul>
        

    When you wish to display a given variable, it is recommended you do it in the form:

    fprintf(fileptr, "%d", your_variable);
        

    where fileptr is of type (FILE *). The variable name might become configurable in the future but for now, it MUST be named fileptr.

  2. Compile the template into an .h file
    cee_templ < template.htm > template.h; # or
    cat template.htm | cee_templ > template.h;
        

    It is recommended you add this line to your build script or makefile. See this repository’s makefile for examples

  3. Write a function which includes the above .h file which MUST include one parameter of type (FILE *) named fileptr
    void example_template(FILE *fileptr, int your_variable)
    {
        #include "template.htm"
    }
        

Why, AKA: Time is a flat circle

Inspired by a recent Tsoding Daily stream VOD implementing the same idea, I went ahead and wrote one of my own, but with a {{ handlebars }} syntax to avoid the %-escape issue Alexey mentioned near the end of the VOD. I also added a file pointer variable so this could be used in-memory as well.

I just found it funny because this is basically Java Server Pages in C (hence the title) . Whether or not that is a good thing is up to the reader.