Skip to content

Instantly share code, notes, and snippets.

@bradgessler
Created February 9, 2024 20:52
Show Gist options
  • Save bradgessler/f46a0cede64eca7ca440964dbcbf4639 to your computer and use it in GitHub Desktop.
Save bradgessler/f46a0cede64eca7ca440964dbcbf4639 to your computer and use it in GitHub Desktop.
Super duper forms
require "bundler/inline"
gemfile do
source 'https://rubygems.org'
gem 'phlex'
end
class Field < Phlex::HTML
def template
p { "Field" }
end
end
class Form < Phlex::HTML
def field
render Field.new
end
def template
p { "Form" }
yield if block_given?
end
end
class Page < Phlex::HTML
def parent
if block_given?
render Form.new do
yield
end
else
Form.new
end
end
def template
# This works
render Form.new do |parent|
parent.field
end
# This fails with `undefined method `target' for nil`
begin
parent = Form.new
render parent.field
rescue => e
plain e.inspect
end
end
end
Page.new.call $stdout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment