TangibleInc / template-system

A template system for WordPress with content type loops and conditions
https://docs.loopsandlogic.com/reference/template-system/
6 stars 2 forks source link

Compiler that converts templates to code/files #122

Closed eliot-akira closed 1 month ago

eliot-akira commented 2 months ago

Currently, the template engine works as an interpreter to render templates. It walks the abstract syntax tree of the parsed template, running it as a program (somewhat like Lisp). This template/program is what generates HTML, runs PHP functions for dynamic tags, and enqueues CSS and JavaScript files.

It may be possible to implement a new strategy for the engine, that works as a compiler.

Instead of interpreting each node at run time (on page load), the template can be converted to the target languages (HTML/CSS/JS/PHP) at compile time when it's saved in the admin edit screen.

An advantage of the compiler approach is that the generated files can be cached, both on the server (OpCache) and on the frontend (browser cache).

A disadvantage is the added complexity of the template life cycle, saving and loading files. There's also a question whether dynamically generating PHP files is seen as reasonable/acceptable behavior for a plugin; some hosting providers may forbid it as dark magic. It would be an opt-in and experimental feature, like "turbo mode".


What is the difference between an interpreter and compiler?

An interpreter converts high-level source code into intermediate code or directly into machine code, which can be executed by a computer. It does this line-by-line or in smaller chunks, meaning that it only translates and executes one section of code at a time.

A compiler, on the other hand, takes entire programs and converts them into executable machine code all at once before they are run. This means that every piece of your source code is translated into machine code during compilation, which can then be executed by the computer without needing further translation.

The key difference between an interpreter and a compiler lies in how they handle the process of translating high-level code to machine code: interpreters do this on-the-fly as the program runs, while compilers translate the entire program before execution.

GabrielGallagher commented 2 months ago

I'm not 100% sure if this would fly on wp.org so it might have to be kept as a premium exclusive feature.

eliot-akira commented 2 months ago

I think you're right. I wanted to write down the idea since I'd considered it before, but as I thought it through, I realized I wouldn't be comfortable with any plugin that did what I described. It's too powerful. If I do experiment with this, I'll make it a private repo and premium add-on.

A compiler that generates HTML/CSS/JS files would be fine, though it kind of defeats the purpose of dynamic HTML - which might be better cached in the database, as the Cache tag does.

That leaves CSS and JS, which would benefit from being compiled/saved/uploaded as minifed files cachable by the browser. For CSS it could use CSS variables for dynamic values, instead of Sass variables.