Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating static pages in OPA

Tags:

opa

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 ?

like image 364
Thomas Avatar asked Dec 13 '11 13:12

Thomas


2 Answers

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 :

  • Xhtml.serialize_as_standalone_html
  • Xhtml.serialize_to_string

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.

like image 177
Fred Avatar answered Sep 18 '22 13:09

Fred


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.

like image 25
Rudy Avatar answered Sep 20 '22 13:09

Rudy