LivelySourceLivelyHelloWorld

class HelloWorld

Represents a "Hello World" Live view component.

This component displays a simple greeting message with a real-time clock that updates every second. It serves as a demonstration of Lively's live update capabilities and provides basic usage instructions.

Definitions

def initialize(...)

Initialize a new HelloWorld view.

Signature

parameter **options Hash

Additional options passed to the parent view.

Implementation

def initialize(...)
	super
	
	@clock = nil
end

attr :clock

Signature

attribute Async::Task | Nil

The background task that updates the view periodically.

def bind(page)

Bind this view to a page and start the update clock.

Signature

parameter page Live::Page

The page this view is bound to.

Implementation

def bind(page)
	super
	
	@clock ||= Async do
		while true
			self.update!
			
			sleep 1
		end
	end
end

def close

Close this view and stop the update clock.

Implementation

def close
	@clock&.stop
	
	super
end

def render(builder)

Render this view as HTML.

Signature

parameter builder Live::Builder

The HTML builder for constructing the view.

Implementation

def render(builder)
	builder.tag(:h1) do
		builder.text("Hello, I'm Lively!")
	end
	
	builder.tag(:p) do
		builder.text("The time is #{Time.now}.")
	end
	
	builder.tag(:p) do
		builder.text(<<~TEXT)
			Lively is a simple client-server SPA framework. It is designed to be easy to use and understand, while providing a solid foundation for building interactive web applications. Create an `application.rb` file and define your own `Application` class to get started.
		TEXT
	end
	
	builder.inline_tag(:pre) do
		builder.text(<<~TEXT)
			#!/usr/bin/env lively
			
			class Application < Lively::Application
				def body
					Lively::HelloWorld.new
				end
			end
		TEXT
	end
	
	builder.tag(:p) do
		builder.text("Check the `examples/` directory for... you guessed it... more examples.")
	end
end