Comments (5)
Thanks again for this @isaacvando - I've assigned the issue to you. Can you please create a subdirectory called template
under the root directory and put your code there as a standalone project for now?
from book-of-examples.
For context, a template engine is a tool that enables dynamically expanding a static document with data. Some examples are Jinja and Thymeleaf.
I use Freemarker frequently at work, and my biggest annoyance with it is that you run into a huge number of run time errors that you have to find the hard way. I would like to write a Roc template engine that surfaces these errors at compile time. My idea for doing this is to create a templating language where the expressions are normal Roc expressions and we compile the template into a human readable Roc function which is then called directly by the consuming program. This way we get compile time errors and all the other standard tooling benefits for free.
At a minimum, I think a template engine should support loops, conditionals, and interpolation. A template could look like this:
{| for number : numbers |}
<div>{{Num.toStr number}}</div>
{|endfor|}
And generate a Roc function like this:
page = \{ numbers } ->
List.map numbers \number ->
"<div>$(Num.toStr number)</div>\n"
|> Str.joinWith ""
To implement this, we need to parse the template to extract the content and template language expressions, determine what parameters are needed for the generated function based on the expressions used in the template, and generate the final function.
The most complicated part about this is determine which parameters are necessary. Fortunately Roc's strictness with identifier names, and the fact that the generated module would not have any user defined imports make the job easier, but still not trivial. For example, given {{foo.result |> Result.map \r -> r * 2}}
we need to determine that foo
is a parameter and nothing else. To do this, we will need to know about lambdas, string interpolation, sub definitions (if we allow expressions to be multiple lines), and probably more things. Nevertheless, I think it is doable. If getting all of the edge cases right is too much to ask, we can always limit the allowed expressions.
I have not landed on a syntax yet so I am open to suggestions. It might be interesting to use a syntax similar to another engine so that existing templates in that language could be ported to Roc easily.
This design could be emulated in dynamic languages like JavaScript and Python, but not so easily in many other statically typed languages like Java, because the generated function would need to have types specified for the arguments which means the engine would need to do type inference. I think this highlights how awesome Roc's full type inference is for allow us to generate a function like this with a simple approach and still get all the compile time validation. I am also very excited about the fact that the you can use familiar Roc functions and syntax in the template.
Note that the generated function always accepts a record. This is probably what the user would want to pass in anyway, and it means that the order of the fields in the generated function does not matter.
from book-of-examples.
@gvwilson I will probably want to expand on this idea further outside of the book and turn it into a fully featured tool. How do you think that would work with licensing for the book?
Edit: I see now that you mentioned on another issue that the code will be MIT Licensed so this shouldn't be an issue. ✔️
from book-of-examples.
👍 There is also now an FAQ in #16 that unpacks this a bit more.
from book-of-examples.
Yes, will do.
from book-of-examples.
Related Issues (20)
- infrastructure proposal: create a blog for this book HOT 4
- topic proposal: discrete event simulator HOT 5
- topic: continuous integration HOT 2
- topic proposal: machine learning from first principles HOT 2
- topic proposal: property-based testing framework HOT 3
- topic proposal: a parser with useful error messages
- topic proposal: pseudorandom number generators HOT 1
- topic: regex pattern matching HOT 1
- topic proposal: Redis-like key value store with write-ahead log HOT 7
- topic: JSON ADT and codecs HOT 5
- topic proposal: Parser combinator library HOT 5
- proposal: Prohibit external packages HOT 9
- topic: compression
- proposal: use Jekyll/GitHub Pages _temporarily_ to build a website for this material HOT 2
- Topic proposal: A transpiler to JavaScript HOT 3
- meeting 2024-04-10 HOT 1
- discuss: how to manage inter-chapter dependencies? HOT 1
- topic: package manager
- topic: pretty-printing library HOT 2
- add link to rendered site
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from book-of-examples.