0

We have a custom C template engine for web template processing. Let us say that we have a file "index.html" with the following content:

{% #include <stdio.h> %}

<!doctype html>
<html>
    <head><title>{{ title }}</title></head>
    <body>
        {{ body }}
        {% printf("test"); %}
        {% printf("test2") %}
        {% printf("test3"); %}
        {%
            printf("test4");
            printf("test5");
        %}
        {%
            printf("test6");
            printf("test7")
        %}
        {% printf("%d", isalpha('a')); %}
    </body>
</html>

When the template engine is called (initialized), it scans all the files in a particular "views" folder. The "index.html" file happens to be in this folder. It is scanned and cached in memory of the main program which has called the template engine.

When a request is made, the template engine's operation function is called. The function takes the cached code and an array of variables with their types and values as parameters and returns a constant char buffer with the updated variables and logical operations. For the sake of simplicity, let us say that logical operations have the {% ... %} and variables the {{ ... }} syntax respectively.

One should be able to use C functions and general logic in the markup language. You should even be able to define functions in templates. If something goes wrong, the variable or logical operation should return void. The end result of our called "index.html" page with "body" and "title" as the variables of {{ body }} and {{ title }} respectively should look like this:

<!doctype html>
<html>
    <head><title>title</title></head>
    <body>
        bodytesttest3test4test5
    </body>
</html>

How does one emulate the logic and syntax of the C language? Is this possible? Do you need to go as far as to develop a custom library of the language's operations and function calls or is there a simpler solution? What is the best way of implementing such a solution?

1
  • @blueshifr - Madness? This is Sparta. Commented Mar 21, 2012 at 10:58

1 Answer 1

1

There are two approaches. Both are not that easy to implement. The first, slightly simplier approach is to generate C from the template files and then use a compiler such as GCC to generate a shared object. This shared object can be loaded with dlopen()/dlsym() to generate the HTML code then.

The second, more complicated but much more flexible approach is to implement an interpreter interpreting the „template language.” You can read about this in compiler building papers and books (the Dragon Book, e.g.).

Of course you could also try to embed for example the PHP interpreter, but that has disadvantages, too.

The „best way” does not exist. It highly depends on what you want. I for myself would implement an interpreter, I think. Generating .so files seems „dirty” to me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.