class Page

Represents a complete HTML document.

A page combines a renderable body with the stylesheets, import map, JavaScript modules, and body attributes required to present it. Pages render complete HTTP responses after a route has selected their content.

Definitions

def initialize(title: "Lively", body: nil, icon: nil, stylesheets: [], imports: {}, modules: [], body_attributes: {})

Initialize a new page.

Signature

parameter title String

The document title.

parameter body Object | Nil

The renderable document body.

parameter icon String | Nil

The favicon URL.

parameter stylesheets Array(String | Hash)

Stylesheets in document order. Hash entries specify link attributes.

parameter imports Hash

JavaScript import map entries.

parameter modules Array(String)

JavaScript module URLs in document order.

parameter body_attributes Hash

Attributes applied to the body element.

Implementation

def initialize(title: "Lively", body: nil, icon: nil, stylesheets: [], imports: {}, modules: [], body_attributes: {})
	@title = title
	@body = body
	@icon = icon
	@stylesheets = stylesheets
	@imports = imports
	@modules = modules
	@body_attributes = body_attributes
	@template = TEMPLATE
end

attr :title

Signature

attribute String

The document title.

attr :body

Signature

attribute Object | Nil

The renderable document body.

attr :icon

Signature

attribute String | Nil

The favicon URL.

attr :stylesheets

Signature

attribute Array(String | Hash)

Stylesheets in document order.

attr :imports

Signature

attribute Hash

JavaScript import map entries.

attr :modules

Signature

attribute Array(String)

JavaScript module URLs in document order.

attr :body_attributes

Signature

attribute Hash

Attributes applied to the body element.

attr :template

Signature

attribute XRB::Template

The document template.

def body_tag

The opening body tag including configured attributes.

Signature

returns XRB::Tag

Implementation

def body_tag
	XRB::Tag.opened("body", @body_attributes)
end

def stylesheet_tag(stylesheet)

A stylesheet link tag for the given URL or attributes.

Signature

parameter stylesheet String | Hash

The stylesheet URL or link attributes.

returns XRB::Tag

Implementation

def stylesheet_tag(stylesheet)
	attributes = if stylesheet.respond_to?(:to_hash)
		stylesheet.to_hash
	else
		{href: stylesheet}
	end
	
	XRB::Tag.closed("link", {rel: "stylesheet", type: "text/css"}.merge(attributes))
end

def import_map

The serialized JavaScript import map.

Signature

returns XRB::MarkupString

Implementation

def import_map
	json = JSON.pretty_generate(imports: @imports)
	json = json.gsub("<", "\\u003c").gsub(">", "\\u003e").gsub("&", "\\u0026")
	
	XRB::MarkupString.raw(json)
end

def to_html

Render this page to an HTML string.

Signature

returns String

Implementation

def to_html
	@template.to_string(self)
end

def call

Render this page as an HTTP response.

Signature

returns Protocol::HTTP::Response

A successful HTML response.

Implementation

def call
	Protocol::HTTP::Response[200, {"content-type" => "text/html; charset=utf-8"}, [to_html]]
end