class Page

Represents a complete HTML document.

A page combines application content with the stylesheets, import map, JavaScript modules, and body attributes required to present it. Applications can use this class directly or subclass it to provide shared defaults.

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 document body. The result of to_html is interpolated into the template.

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 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 body_content

The rendered body content.

Signature

returns Object

Implementation

def body_content
	@body&.to_html || "No body specified!"
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 call

Render this page to a string.

Signature

returns String

Implementation

def call
	@template.to_string(self)
end