From 61dfdcbcd4251806698ccc17c18bec96c055936c Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Fri, 16 Jun 2023 19:44:54 -0700 Subject: [PATCH] mu --- Gemfile | 1 + Gemfile.lock | 10 ++++++++++ lib/app.rb | 46 ++++++++++++++++++++++++---------------------- views/hello.phlex | 6 +++++- views/layout.phlex | 5 +++++ 5 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 views/layout.phlex diff --git a/Gemfile b/Gemfile index b272d58..1d42402 100644 --- a/Gemfile +++ b/Gemfile @@ -7,4 +7,5 @@ gem "phlex" gem "rackup" gem "rake" gem "roda" +gem "ruby-lsp" # TODO: Set up ruby-lsp gem "tilt" diff --git a/Gemfile.lock b/Gemfile.lock index a82f369..f6972e0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,11 +5,13 @@ GEM concurrent-ruby (1.2.2) erb (4.0.2) cgi (>= 0.3.3) + language_server-protocol (3.17.0.3) minitest (5.18.0) phlex (1.8.1) concurrent-ruby (~> 1.2) erb (>= 4) zeitwerk (~> 2.6) + prettier_print (1.2.1) rack (3.0.8) rackup (2.1.0) rack (>= 3) @@ -17,6 +19,13 @@ GEM rake (13.0.6) roda (3.69.0) rack + ruby-lsp (0.5.1) + language_server-protocol (~> 3.17.0) + sorbet-runtime + syntax_tree (>= 6.1.1, < 7) + sorbet-runtime (0.5.10880) + syntax_tree (6.1.1) + prettier_print (>= 1.2.0) tilt (2.2.0) webrick (1.8.1) zeitwerk (2.6.8) @@ -31,6 +40,7 @@ DEPENDENCIES rackup rake roda + ruby-lsp tilt BUNDLED WITH diff --git a/lib/app.rb b/lib/app.rb index 8afacd3..58e2c38 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -5,12 +5,12 @@ class App < Roda route do |r| r.root do - render "hello" + # render("hello", locals: {name: "Alice"}) + view("hello", locals: {name: "Alice"}) end end end - require "tilt/template" require "phlex" @@ -19,28 +19,30 @@ module Tilt def prepare end - # def evaluate(scope, locals) - # klass = Class.new(Phlex::HTML) - # klass.class_eval(data, __FILE__, __LINE__) - # klass.new.() - # end - - def precompiled_template(*) - data + # 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 - def precompiled_preamble(*) - <<~RUBY - klass = Class.new(Phlex::HTML) do - RUBY - end - - def precompiled_postamble(*) - <<~RUBY - end - klass.new.call - RUBY - 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 diff --git a/views/hello.phlex b/views/hello.phlex index 862cac7..bb3b3d2 100644 --- a/views/hello.phlex +++ b/views/hello.phlex @@ -1,3 +1,7 @@ +def initialize(name:) + @name = name +end + def template - h1 { "👋 Hello World!" } + h1 { "👋 Hello #@name!" } end diff --git a/views/layout.phlex b/views/layout.phlex new file mode 100644 index 0000000..1fc8541 --- /dev/null +++ b/views/layout.phlex @@ -0,0 +1,5 @@ +def template + article do + yield + end +end