You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.4 KiB
50 lines
1.4 KiB
require "roda"
|
|
|
|
class App < Roda
|
|
plugin :render, engine: :phlex
|
|
|
|
route do |r|
|
|
r.root do
|
|
# render("hello", locals: {name: "Alice"})
|
|
view("hello", locals: {name: "Alice"})
|
|
end
|
|
end
|
|
end
|
|
|
|
require "tilt/template"
|
|
require "phlex"
|
|
|
|
module Tilt
|
|
class PhlexTemplate < Template
|
|
def prepare
|
|
end
|
|
|
|
# I have no idea how this works - it's just copied pretty much blindly from here:
|
|
#
|
|
# https://github.com/phlex-ruby/phlex-rails/blob/main/lib/phlex/rails/layout.rb#L18
|
|
def evaluate(scope, locals, &block)
|
|
klass = Class.new(Phlex::HTML)
|
|
klass.class_eval(data, __FILE__, __LINE__)
|
|
component = klass.new(**locals)
|
|
component.call do |yielded|
|
|
output = yield
|
|
component.unsafe_raw(output)
|
|
end
|
|
end
|
|
|
|
# Not sure if this is better...?
|
|
# def precompiled_preamble(*) = "klass = Class.new(Phlex::HTML) do"
|
|
# def precompiled_template(*) = data
|
|
# def precompiled_postamble(locals) = <<~RUBY
|
|
# end
|
|
# component = klass.new(**locals)
|
|
# component.call do |yielded|
|
|
# output = yield
|
|
# component.unsafe_raw(output)
|
|
# end
|
|
# RUBY
|
|
end
|
|
end
|
|
|
|
Tilt.register(Tilt::PhlexTemplate, "phlex")
|