ticket

Your First App

Create an app with ticket new, run it, then add a second page. It takes a few minutes and touches a route, a controller action and a view.

You need Polar and the ticket command first. Getting Started has both installs.

Create the app

$ ticket new hello
$ cd hello
$ ticket server
hello is listening on http://127.0.0.1:3000

ticket new writes the skeleton, runs polar fetch and builds it. Open http://127.0.0.1:3000 and you'll see a welcome page. ticket server --watch rebuilds and restarts when you edit a file.

What it wrote

polar.toml                          the project, and its dependency on Ticket
ticket.toml                         Ticket's settings: database, sessions, server
src/main.px                         exports router and route_table
src/routes.px                       the routes
src/controllers/pages_controller.px actions
src/views/layout.px                 the page around every view
src/views/pages.px                  the home page
public/                             static files, and the 404 and 500 pages

The route

A routes zone generates the router and a typed path helper for each route. The skeleton has one:

routes
  root  -> PagesController.home

The action

An action takes a Conn and returns a Response. This one wraps the page in the session, so flash and CSRF work, and renders the layout around a view.

functions
  home(conn: Conn) -> Response / {..} {
    Session.handle(conn, page)
  }

  page(conn: Conn) -> Response {
    Response.render(Layout.app(conn, "Hello", Pages.home()))
  }

The view

Views live in a views zone. The markup is type-checked, and {...} holes are escaped.

views
  home()
    <h1>Welcome to Hello</h1>
    <p>Edit <code>src/views/pages.px</code> to change this page.</p>

Add an About page

Three edits. First, a route in src/routes.px:

routes
  root         -> PagesController.home
  get  /about  -> PagesController.about  as about

Then an action in src/controllers/pages_controller.px, exported next to home:

  about(conn: Conn) -> Response / {..} {
    Session.handle(conn, about_page)
  }

  about_page(conn: Conn) -> Response {
    Response.render(Layout.app(conn, "About", Pages.about()))
  }

And a view in src/views/pages.px, also exported:

  about()
    <h1>About</h1>
    <p>Built with Ticket.</p>

A typo in a controller or view name is a compile error on the line that has it. The route generated a helper too: Paths.about_path() links to the page.

$ ticket routes
root_path   GET  /       PagesController.home
about_path  GET  /about  PagesController.about

Next: ticket g scaffold Post title:String body:String writes a whole resource: migration, model, controller, views and tests. See Generators.

Without the CLI

Ticket is an ordinary Polar dependency, so polar add github.com/stepanvanzuriak/ticket works in any project, and a hand-written router(request) function is all the launcher needs. ticket new just writes the same files for you.

[dependencies]
ticket = { git = "github.com/stepanvanzuriak/ticket", version = "v0.2.0" }

[run]
launcher = "ticket"
options = { port = 3000 }