Similar to LuckyHTML, but for Hyperview.
- Add the dependency to your
shard.yml:
dependencies:
lucky_hxml:
github: luckyframework/lucky_hxmlversion: ~> 0.1- Run
shards install
- Add the dependency to
src/shards.cr:
require"lucky"require"avram/lucky"# ...require"lucky_hxml"- Add a
src/components/base_hxml_component.crfile:
abstractclassBaseHXMLComponent < LuckyHXML::ComponentendSynonymous with Lucky Components
- Add a
src/screens/main_screen.crfile:
abstractclassMainScreen < LuckyHXML::ScreenendSynonymous with Lucky HTML
- Include in
BrowserAction(or anyLucky::Action) and modifyaccepted_formats:
abstractclassBrowserAction < Lucky::Action# ...includeLuckyHXML::Renderable
accepted_formats [:html, :json, :xml], default::html# ^^^^endclassHomeScreen < MainScreendefrender
doc do
screen do
styles do
style id:"body", flex:"1", backgroundColor:"white"end
body style:"body"do
view do
text "Welcome!"endendendendendendclassPhoneBehaviorComponent < BaseHXMLComponent
needs trigger : String="press"
needs phone_number : Stringdefrender
behavior(
"xmlns:comms": "https://hypermedia.systems/hyperview/communications",
trigger: trigger,
action:"open-phone",
"comms:phone-number": phone_number
)
endendYou can create custom elements and attributes with element & attribute methods respectively.
classSwipeRowComponent < BaseHXMLComponentdefrender(&)
element "swipe:row"do
swipe_namespace
yieldendendprivatedefswipe_namespace : Nil
attribute "xmlns:swipe", "https://hypermedia.systems/hyperview/swipeable"endendmount PhoneBehaviorComponent, phone_number:"123-456-7890"With a block:
mount SwipeRowComponentdo# ...endclassHome::Index < BrowserAction
get "/"do# Same as `html` macro
hxml HomeScreenendendclassHome::Index < BrowserAction
get "/"do# Same as `component` method
hxml_component PhoneBehaviorComponent, phone_number:"111-222-3333"endend