From 019a31f483d81e431cd97c87f087f688d8ccc186 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Fri, 16 Jun 2023 21:01:31 -0700 Subject: [PATCH] mu --- lib/app.rb | 15 ++++++++++++--- views/hello.phlex | 4 ---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/app.rb b/lib/app.rb index a652f8e..3c9efa2 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -122,11 +122,20 @@ module Tilt # # https://github.com/phlex-ruby/phlex-rails/blob/main/lib/phlex/rails/layout.rb#L18 def evaluate(scope, locals, &block) - p scope - p locals klass = Class.new(Phlex::HTML) klass.class_eval(data, __FILE__, __LINE__) - component = klass.new(**locals) + + component = klass.new + + scope.instance_variables + .reject { _2.start_with?("@_") } + .each do |k| + component.instance_variable_set(k, scope.instance_variable_get(k)) + end + locals.each do |k,v| + component.instance_variable_set("@#{k}", v) + end + component.call do |yielded| output = yield component.unsafe_raw(output) diff --git a/views/hello.phlex b/views/hello.phlex index bb3b3d2..5c7a363 100644 --- a/views/hello.phlex +++ b/views/hello.phlex @@ -1,7 +1,3 @@ -def initialize(name:) - @name = name -end - def template h1 { "👋 Hello #@name!" } end