GithubHelp home page GithubHelp logo

lommix / templus Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 60 KB

Go inspired html template engine / compiler for rust

License: MIT License

Rust 95.30% HTML 4.70%
html rust template templates web

templus's Introduction

Templus

Go inspired template engine/compiler for Rust.

With proper inheritance, composition and caching.


Recently, I had the opportunity to develop a small web app in Go utilizing solely the standard libraries. It is truly remarkable how comprehensive the Go standard library ecosystem is and how far you get with no dependencies at all.

The template behavior was the only truly frustrating aspect for me. The syntax is acceptable, but the inability to parse all my templates simultaneously, cache the parse tree, and then render specific blocks while maintaining inheritance and composition is disappointing. Importing items multiple times results in overwrites, and a template can only be inherited once. Go necessitates parsing all required templates for each page in a separate object or reprocessing them with each request.

To challenge myself I implemented my own Go inspired template engine in rust with serde being the only dependency. Syntax is basically the same, but with proper if statements, inheritance, and composition.

This is work in progress and will probably be used and iterated on in my next small web project.

cargo run --example render

Basic Example

Templates are defined by blocks. You can have as many as you want in one file. Html outside define-blocks is ignored. There is very little cloning, the template container is bound to the lifetime of the template source. Only in the final render step, the Html gets copied to new Memory. This should make things pretty fast.

let tmpl = std::fs::read_to_string("templus/examples/example.html").expect("cannot read file");

let mut envirement = templus::renderer::Envirement::new();
envirement.parse(&tmpl).unwrap();

let html = environment
    .render(
        "foo",
        &templus::context! {
            name => "lommix",
            number => 69,
            bool => true
        },
    )
    .unwrap();

print!("{}", html);

Sample template code:

{{ define 'base' }}
<html>
    <head>
        <meta charset="UTF-8" />
        {{block 'meta'}}
        {{ end }}
    </head>
    <body>
        {{ block 'content' }}
        {{ end }}
        {{ block 'js' }}
            <script src="/test.js"></script>
        {{ end }}
    </body>
</html>
{{ end }}

{{ define 'foo' extends 'base' }}

    {{ block 'meta' }}
        <title>jsx sucks</title>
    {{end}}

    {{ block 'content' }}
        <h1>hello</h1>

        <div class="import-this">
            {{ import 'foobar' }}
        </div>

        {{ if .bool }}
            <p>bool is true</p>
        {{end}}

        {{ if .number > 42 }}
            <p>num is bigger than 42</p>
        {{ end }}

        {{ if .number < 42 }}
            <p>num is smaller than 42</p>
        {{ else }}
            <p>num is not smaller than 42</p>
        {{ end }}

        {{ if .number == 69 }}
            <p>number is 69</p>
        {{ end }}

    {{ end }}
{{ end }}

{{ define 'foobar'}}
    <ul class="loop-example">
    {{ range 10 }}
        <li> you are {{.name}} </li>
    {{ end }}
    </ul>
{{ end }}

Todos

  • Variable assignments.
  • Binary operators in if statements.
  • User defined functions.
  • Bindings for other languages.
  • Cli Tools and Parse Tree serialization.

templus's People

Contributors

lommix avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.