Display-as compile-time template crate

I'd like to announce the display-as crate. display-as is a compile-time templating system (like askama or ructe) that supports multiple output Formats, and can be extended to handle any utf8 text format you wish. Each Format has its own escaping function which is applied to string data. display-as is designed to each object implementing a trait DisplayAs may be easily and correctly used within another template, and each template can embed types of another format (e.g. how URLs and JavaScript show up in HTML).

Currently I myself am only using HTML and URL on my website, but anticipate using the Math format with MathJax in the nearish future. You can mix Formats within a given template, and a given type can have different representations in multiple Formats.

The template language ([see example] (progressions-website/activity.html at master · droundy/progressions-website · GitHub)) I use allows inclusion of arbitrary rust code within a template, including loops and the definition of local variables to be used later in the template. An include! macro combined with local variables gives the same power as "extends" in jinja2 to allow the reuse of the overall structure of a document.

Anyhow, it's still somewhat sparsely documented and I'm open to breaking changes as I refine the API. But I'm also already using display-as for a real website under development. (For the curious, it's a learning progression for partial derivatives in upper division physics which may be down for development as you read this) There is built in support for use with the warp web framework, which is tested and used. I'd be happy to add support for other frameworks.

5 Likes

Wow. I've been looking for something like this, but I'd about given up hope that someone had done it already. I was thinking I would have to do it myself. Thanks for all your work on this.

The key thing I've been looking for, and which you've implemented, is the ability to intermingle standard Rust in a page that is otherwise just text that is to be output as is. There's no DSL to learn, and there's also no multistep build process.

I will try to play around with it in the next few weeks and let you know about any feedback I might have.

Am I right in understanding that this does not require nightly?

That is correct. You should be aware that I recently discovered a bug that prevents match statements from working. I'll give it, but haven't yet done so. There is also a problem (also to be fixed) with pattern matches that include braces. I intend to fix both bugs, but haven't had much time for fun programming recently.

1 Like

I've just now fixed this bug.

1 Like

Great to hear that you got that bug with the match statements fixed, @droundy.

I've been getting familiar with the crate, and I'm very excited about what it can do. I'll open issues if I come across any problems that I think may be bugs.

Have you benchmarked the crate to see how well it performs in comparison to other template engines that do compile-time templating? Perhaps you want to have it included in these frequently referenced benchmarks? It probably would increase visibility of the crate.

No, I haven't benchmarked it yet. I'd love a contribution of some benchmarks if you're interested. Right now my focus is more strongly on using DisplayAs than on developing or promoting it. Too many things demanding time... And what time I devote to it does to bugs and docs.

I've just fixed this bug as well, with the just-released display-as 0.4.4.

Great! Thanks for your continued work on this. I've started using DisplayAs in an internal website I'm building with Warp. I'll let you know about any issues or pain points that come.

I don't use warp, so any suggestions for better integration will be greatly appreciated!

I've just released display-as 0.6.0, a major version bump that changes the return type of the format_as! macro to a new FormattedString type that will "remember" what it is formatted as (e.g. HTML or URL etc).