In one of my project, I have to write HTML & JavaScript code. I would prefer to use a statically typed language instead, so I'm evaluating OPA. However, my goal is to generate a collection of static pages, so I don't care about the OPA HTTP server and persistent layer.
So here comes my question : is there an (easy) way to generate a collection of static pages in OPA ?
If i understand correctly, you want to build you xhtml with Opa, but instead of serving, print it into files?
We have 2 functions for that :
The differences between those 2 functions, is that the first one will not generate the associated opa js code.
Then you can write the resulting string into an HTML file on disk.
Note that we do not provide any method to write a file in disk in our stdlib. You have to use the bsl system for that :
write = %%BslFile.of_string%%
A small example :
static.opa
write = %%BslFile.of_string%%
xhtml_page(num:int) =
<p>Page {num}</p>
pages = [1, 2, 3, 4, 5]
do List.iter(i ->
xhtml_content = xhtml_page(i)
string_content = Xhtml.serialize_as_standalone_html(xhtml_content)
write("{i}.html", string_content)
, pages)
Compile and run : opa static.opa --
This will generate 5 html pages.
You can use Xhtml.precompiled. It take a xhtml value and gives you back a xhtml that is precompiled (internally already flattened to a string).
It is usefull when a part of the web page is static while the remaining is dynamic. You can avoid paying some cost (serialisation, memory ...) for the static part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With