generating static pages in OPA
Asked Answered
M

3

5

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 ?

Marquez answered 13/12, 2011 at 13:31 Comment(0)
N
5

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.

Novia answered 13/12, 2011 at 20:53 Comment(4)
Sweet! What happens if you call some JavaScript functions in xhtml_page (as {Random.int num}) ? Is there a way to ensure that the generated page will not contain some JavaScript code which will try to communicate with the server ?Marquez
There will not be server communication with Random.int (in my example) because it will be executed when you generate your pages.Novia
You can have "server" communication if you attach event listener to some xhtml part (for example <span onclick={a_server_call}/>), but if you use Xhtml.serialize_as_standalone_html those actions will be removed from the xhtml, hence there will be no server call. It depends on what html you want to generate. Note that you can not generate something like <a onclick="javascript:my_js_call()"/> because Opa forbids it.Novia
OK thanks. I just want to use the bindings to twitter or github to display have a script displaying some latests tweets/commmits on the index page (which is generated statically generated). I will experiment with your method.Marquez
S
2

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.

Statutable answered 23/12, 2011 at 19:30 Comment(0)
S
0

Thomas, I suppose you want to use Opa for the code (instead of JS), correct?

Having just a bunch of static pages would not be a typical usage for the language, but sure you can do that easily:

Server.start(Server.http, { resources: @static_resource_directory("resources") })

The above is a complete Opa program (S4 style) that will just serve statically the content of the resources directory (HTML included).

More conventional scenario would be to use the Templating mechanism of Opa (a markup accepting a big subset of HTML).

Hope that helps.

Solis answered 13/12, 2011 at 17:9 Comment(5)
Not really. What I want is to write a single .opa file to generate a bunch of static pages. I'm happy to use only a subset of OPA features (for instance, I don't need any kind of communication). Then, the static pages can be served by any HTTP servers.Marquez
You mean you want to programatically generate those pages? (i.e. have a program that generates a bunch of static pages?)Solis
Yes, exactly! Of course, I can write my application, run it locally, and then use wget to get a bunch of static pages (and their ressources) ... but then I need to rely on the OPA compiler to not include any server-side communication constructs in the generated pages.Marquez
Ok, if that's really what you are after, then, frankly, this is not what Opa was designed for. That doesn't mean you will not be able to accomplish this task with Opa, but it will be an overkill for this task (and not such a great fit either). But I wonder, why do you insist on generating those static pages in advance instead of generating them on the fly? (i.e. if you can tell a bit about the context of your app I may be able to help better).Solis
Thanks for your help, but I guess that what I need looks like the Xhtml.serialize_* functions. Thanks for your time!Marquez

© 2022 - 2024 — McMap. All rights reserved.