Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phlex ActionView template handler #117

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/phlex/rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ module Phlex
module Rails
# @api private
class Engine < ::Rails::Engine
initializer "phlex-rails.template_engine" do |_app|
ActionView::Template.register_template_handler(
:phlex,
-> (_template, source) do
"Phlex::Rails::TemplateHandler.call(#{source.inspect})"
end
)
end
end
end
end
15 changes: 15 additions & 0 deletions lib/phlex/rails/template_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Phlex::Rails
class TemplateHandler
def self.call(source)
klass = Class.new(Phlex::HTML) do
class_eval <<~RUBY, __FILE__, __LINE__ + 1
def template
#{source}
end
RUBY
end

klass.call
end
end
end
13 changes: 13 additions & 0 deletions spec/template_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Phlex::Rails::TemplateHandler do
it "renders as expected" do
result = Phlex::Rails::TemplateHandler.call(
"h1 { 'hello world' }"
)

expect(result).to eq %(<h1>hello world</h1>)
end
end