Serving HTML

Sharaf is using the hepek-components as its template engine.
Hepek is a bit different than other template engines, in the sense that it is plain scala code.
There is no separate language you need to learn.
It has useful utilities like Bootstrap 5 templates, form helpers etc. so you can focus on the important stuff.


Let's make a simple HTML page that greets the user.
Create a file html.sc and paste this code into it:

    //> using scala "3.4.0"
//> using dep ba.sake::sharaf:0.4.0

import io.undertow.Undertow
import scalatags.Text.all.*
import ba.sake.hepek.html.HtmlPage
import ba.sake.sharaf.*, routing.*

object IndexView extends HtmlPage:
  override def bodyContent = div(
    p("Welcome!"),
    a(href := "/hello/Bob")("Hello world")
  )

class HelloView(name: String) extends HtmlPage:
  override def bodyContent =
    div("Hello ", b(name), "!")

val routes = Routes:
  case GET() -> Path() =>
    Response.withBody(IndexView)
  case GET() -> Path("hello", name) =>
    Response.withBody(HelloView(name))

Undertow.builder
  .addHttpListener(8181, "localhost")
  .setHandler(SharafHandler(routes))
  .build
  .start()

println(s"Server started at http://localhost:8181")

and run it like this:

scala-cli html.sc 

Go to http://localhost:8181
to see how it works.