diff --git a/fixtures/layout.rb b/fixtures/layout.rb
index 1365461e..a7b81184 100644
--- a/fixtures/layout.rb
+++ b/fixtures/layout.rb
@@ -6,7 +6,7 @@ def initialize(title: "Example")
@title = title
end
- def template(&block)
+ def view_template(&block)
html do
head do
title { @title }
diff --git a/fixtures/page.rb b/fixtures/page.rb
index 117de544..f6c7665f 100644
--- a/fixtures/page.rb
+++ b/fixtures/page.rb
@@ -2,7 +2,7 @@
module Example
class Page < Phlex::HTML
- def template
+ def view_template
render LayoutComponent.new do
h1 { "Hi" }
diff --git a/lib/phlex/deferred_render.rb b/lib/phlex/deferred_render.rb
index 9ca05d68..76d44cef 100644
--- a/lib/phlex/deferred_render.rb
+++ b/lib/phlex/deferred_render.rb
@@ -11,7 +11,7 @@
# @tabs = []
# end
#
-# def template
+# def view_template
# @tabs.each { |t| a { t.name } }
# @tabs.each { |t| article(&t.content) }
# end
diff --git a/lib/phlex/elements.rb b/lib/phlex/elements.rb
index bdde822b..50e3c3c4 100644
--- a/lib/phlex/elements.rb
+++ b/lib/phlex/elements.rb
@@ -11,7 +11,7 @@
# class MyComponent < Phlex::HTML
# include MyCustomElements
#
-# def template
+# def view_template
# trix_editor
# end
# end
diff --git a/lib/phlex/sgml.rb b/lib/phlex/sgml.rb
index cfd77818..3e017a79 100644
--- a/lib/phlex/sgml.rb
+++ b/lib/phlex/sgml.rb
@@ -51,24 +51,34 @@ def element_method?(method_name)
# @abstract Override to define a template for your component.
# @example
- # def template
+ # def view_template
# h1 { "👋 Hello World!" }
# end
# @example Your template may yield a content block.
- # def template
+ # def view_template
# main {
# h1 { "Hello World" }
# yield
# }
# end
# @example Alternatively, you can delegate the content block to an element.
- # def template(&block)
+ # def view_template(&block)
# article(class: "card", &block)
# end
def template
yield
end
+ def self.method_added(method_name)
+ if method_name == :template
+ Kernel.warn "Defining the `template` method on a Phlex component is deprecated and will be unsupported in Phlex 2.0. Please define `view_template` instead."
+ end
+ end
+
+ def view_template(&block)
+ template(&block)
+ end
+
# @api private
def await(task)
if defined?(Concurrent::IVar) && task.is_a?(Concurrent::IVar)
@@ -106,9 +116,9 @@ def __final_call__(buffer = +"", context: Phlex::Context.new, view_context: nil,
if block
if is_a?(DeferredRender)
__vanish__(self, &block)
- template
+ view_template
else
- template do |*args|
+ view_template do |*args|
if args.length > 0
yield_content_with_args(*args, &block)
else
@@ -117,7 +127,7 @@ def __final_call__(buffer = +"", context: Phlex::Context.new, view_context: nil,
end
end
else
- template
+ view_template
end
end
diff --git a/test/phlex/callbacks.rb b/test/phlex/callbacks.rb
index c822e19f..656ab62b 100644
--- a/test/phlex/callbacks.rb
+++ b/test/phlex/callbacks.rb
@@ -9,7 +9,7 @@ def before_template
h1 { "Hello" }
end
- def template
+ def view_template
h2 { "World" }
end
diff --git a/test/phlex/render_enumerable.rb b/test/phlex/render_enumerable.rb
index ca9b3a6d..53812f19 100644
--- a/test/phlex/render_enumerable.rb
+++ b/test/phlex/render_enumerable.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class Card < Phlex::HTML
- def template(&block)
+ def view_template(&block)
article(&block)
end
end
@@ -14,13 +14,13 @@ def initialize
]
end
- def template
+ def view_template
render @cards
end
end
class WithBlock < WithoutBlock
- def template
+ def view_template
render @cards do
h1 { "Hi" }
end
diff --git a/test/phlex/svg.rb b/test/phlex/svg.rb
index 2c462cb0..93476fac 100644
--- a/test/phlex/svg.rb
+++ b/test/phlex/svg.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class Example < Phlex::SVG
- def template
+ def view_template
svg do
path(d: "123")
end
@@ -9,7 +9,7 @@ def template
end
class ExampleFromHTML < Phlex::HTML
- def template
+ def view_template
svg do |s|
s.path(d: "321")
end
diff --git a/test/phlex/testing/view_helper.rb b/test/phlex/testing/view_helper.rb
index 9e94218a..87d5594e 100644
--- a/test/phlex/testing/view_helper.rb
+++ b/test/phlex/testing/view_helper.rb
@@ -3,13 +3,13 @@
require "phlex/testing/view_helper"
class ExampleHTML < Phlex::HTML
- def template
+ def view_template
h1 { "👋" }
end
end
class ExampleSVG < Phlex::SVG
- def template
+ def view_template
svg do
path(d: "123")
end
diff --git a/test/phlex/unbuffered.rb b/test/phlex/unbuffered.rb
index 9cd5bcfb..5a552cc2 100644
--- a/test/phlex/unbuffered.rb
+++ b/test/phlex/unbuffered.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class Example < Phlex::HTML
- def template
+ def view_template
yield
end
diff --git a/test/phlex/view/around_template.rb b/test/phlex/view/around_template.rb
index 3bec3ca9..6ab87756 100644
--- a/test/phlex/view/around_template.rb
+++ b/test/phlex/view/around_template.rb
@@ -11,7 +11,7 @@ def around_template
h3 { "After" }
end
- def template
+ def view_template
h2 { "Hello" }
end
end
diff --git a/test/phlex/view/call.rb b/test/phlex/view/call.rb
index d18a7f42..8eed1d65 100644
--- a/test/phlex/view/call.rb
+++ b/test/phlex/view/call.rb
@@ -4,7 +4,7 @@
extend ViewHelper
view do
- def template
+ def view_template
plain "Hi"
end
end
@@ -17,7 +17,7 @@ def template
with "`render?` method returning false when the view context is true" do
view do
- def template
+ def view_template
plain "Hi"
end
@@ -29,7 +29,7 @@ def render?
with "a view that yields an object" do
view do
- def template
+ def view_template
yield(1, 2)
end
end
@@ -47,7 +47,7 @@ def template
with "a view that yields nothing" do
view do
- def template
+ def view_template
yield
end
diff --git a/test/phlex/view/capture.rb b/test/phlex/view/capture.rb
index 60770147..3e9a6b24 100644
--- a/test/phlex/view/capture.rb
+++ b/test/phlex/view/capture.rb
@@ -7,7 +7,7 @@
view do
attr_accessor :captured
- def template
+ def view_template
h1 { "Before" }
@captured = capture { "Hello" }
h1 { "After" }
@@ -24,7 +24,7 @@ def template
view do
attr_accessor :captured
- def template
+ def view_template
h1 { "Before" }
@captured = capture do
h1 { "Hello" }
@@ -43,7 +43,7 @@ def template
view do
attr_accessor :captured
- def template
+ def view_template
h1 { "Before" }
@captured = capture do
h1 { "Hello" }
@@ -63,7 +63,7 @@ def template
with "a call to flush inside a component rendered in the block" do
let(:component) do
Class.new(Phlex::HTML) do
- def template
+ def view_template
h1 { "Hello" }
flush
h1 { "World" }
@@ -73,7 +73,7 @@ def template
let(:previewer) do
Class.new(Phlex::HTML) do
- def template
+ def view_template
srcdoc = capture { yield } if block_given?
iframe srcdoc: srcdoc
@@ -82,7 +82,7 @@ def template
end
view do
- def template
+ def view_template
h1 { "Before" }
render @_view_context.previewer do
render @_view_context.component
diff --git a/test/phlex/view/comment.rb b/test/phlex/view/comment.rb
index 37c144ba..d175cd77 100644
--- a/test/phlex/view/comment.rb
+++ b/test/phlex/view/comment.rb
@@ -5,7 +5,7 @@
with "simple comment" do
view do
- def template
+ def view_template
comment { "This is an HTML comment" }
end
end
@@ -17,7 +17,7 @@ def template
with "empty comment" do
view do
- def template
+ def view_template
comment
end
end
@@ -29,7 +29,7 @@ def template
with "number comment" do
view do
- def template
+ def view_template
comment { 1 }
end
end
@@ -41,7 +41,7 @@ def template
with "escaped comment" do
view do
- def template
+ def view_template
comment { "Important" }
end
end
diff --git a/test/phlex/view/content.rb b/test/phlex/view/content.rb
index 3ba18ce8..1f9f31d1 100644
--- a/test/phlex/view/content.rb
+++ b/test/phlex/view/content.rb
@@ -5,7 +5,7 @@
with "content" do
view do
- def template
+ def view_template
h1 { "Before" }
yield
h2 { "After" }
diff --git a/test/phlex/view/custom_elements.rb b/test/phlex/view/custom_elements.rb
index 2351fb40..ccbce1d2 100644
--- a/test/phlex/view/custom_elements.rb
+++ b/test/phlex/view/custom_elements.rb
@@ -8,7 +8,7 @@
register_element :trix_editor
register_element :trix_toolbar
- def template
+ def view_template
div do
trix_toolbar
trix_editor { "Hello" }
diff --git a/test/phlex/view/doctype.rb b/test/phlex/view/doctype.rb
index c10574a4..3f601b33 100644
--- a/test/phlex/view/doctype.rb
+++ b/test/phlex/view/doctype.rb
@@ -5,7 +5,7 @@
with "a doctype" do
view do
- def template
+ def view_template
html do
head { doctype }
end
diff --git a/test/phlex/view/format_object.rb b/test/phlex/view/format_object.rb
index 7b804d09..943d0b32 100644
--- a/test/phlex/view/format_object.rb
+++ b/test/phlex/view/format_object.rb
@@ -3,7 +3,7 @@
require "date"
class Example < Phlex::HTML
- def template
+ def view_template
h1 { Date.new(2022, 11, 28) }
end
diff --git a/test/phlex/view/legacy_template_method.rb b/test/phlex/view/legacy_template_method.rb
new file mode 100644
index 00000000..751e6b92
--- /dev/null
+++ b/test/phlex/view/legacy_template_method.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+describe Phlex::SGML do
+ it "warns when you define a template" do
+ expect(Kernel).to receive(:warn)
+
+ example = Class.new(Phlex::HTML) do
+ def template
+ h1 { "Hello, world!" }
+ end
+ end
+
+ expect(
+ example.new.call
+ ).to be == "
Hello, world!
"
+ end
+end
diff --git a/test/phlex/view/naughty_business.rb b/test/phlex/view/naughty_business.rb
index 07b356ec..5e4cad47 100644
--- a/test/phlex/view/naughty_business.rb
+++ b/test/phlex/view/naughty_business.rb
@@ -5,7 +5,7 @@
with "naughty text" do
view do
- def template
+ def view_template
plain %(">)
end
end
@@ -17,7 +17,7 @@ def template
with "naughty tag attribute values" do
view do
- def template
+ def view_template
article id: %(">)
end
end
@@ -29,7 +29,7 @@ def template
with "naughty javascript link protocol in href" do
view do
- def template
+ def view_template
a href: "javascript:javascript:alert(1)" do
"naughty link"
end
@@ -43,7 +43,7 @@ def template
with "naughty javascript link protocol in href" do
view do
- def template
+ def view_template
a "href" => "javascript:javascript:alert(1)" do
"naughty link"
end
@@ -60,7 +60,7 @@ def template
naughty_attributes = { event_attribute => "alert(1);" }
view do
- define_method :template do
+ define_method :view_template do
send(:div, **naughty_attributes)
end
end
@@ -78,7 +78,7 @@ def template
naughty_attributes = { naughty_attribute => "alert(1);" }
view do
- define_method :template do
+ define_method :view_template do
send(:div, **naughty_attributes)
end
end
diff --git a/test/phlex/view/new.rb b/test/phlex/view/new.rb
index 163c2529..e2661ad1 100644
--- a/test/phlex/view/new.rb
+++ b/test/phlex/view/new.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class Example < Phlex::HTML
- def template(&block)
+ def view_template(&block)
h1(&block)
end
end
@@ -16,7 +16,7 @@ def render?
@should_render
end
- def template
+ def view_template
h1 {
yield
plain(", #{@name}")
@@ -29,7 +29,7 @@ def template
with "a block passed to new" do
view do
- def template
+ def view_template
render Example.new { "Hello" }
end
end
@@ -41,7 +41,7 @@ def template
with "a block and arguments passed to new" do
view do
- def template
+ def view_template
render ExampleWithArgs.new("World", should_render: true) { "Hello" }
end
end
diff --git a/test/phlex/view/numbers.rb b/test/phlex/view/numbers.rb
index 2a465284..4baa2d7d 100644
--- a/test/phlex/view/numbers.rb
+++ b/test/phlex/view/numbers.rb
@@ -5,7 +5,7 @@
with "numbers" do
view do
- def template
+ def view_template
span { 1 }
span { 2.0 }
diff --git a/test/phlex/view/plain.rb b/test/phlex/view/plain.rb
index 2a873f28..3f7d4216 100644
--- a/test/phlex/view/plain.rb
+++ b/test/phlex/view/plain.rb
@@ -5,7 +5,7 @@
with "text" do
view do
- def template
+ def view_template
plain "Hi"
end
end
@@ -17,7 +17,7 @@ def template
with "int as text" do
view do
- def template
+ def view_template
plain 1
end
end
@@ -29,7 +29,7 @@ def template
with "float as text" do
view do
- def template
+ def view_template
plain 2.0
end
end
@@ -41,7 +41,7 @@ def template
with "an object that has no special format_object handling" do
view do
- def template
+ def view_template
plain Object.new
end
end
diff --git a/test/phlex/view/process_attributes.rb b/test/phlex/view/process_attributes.rb
index 8ca6df50..aec6b761 100644
--- a/test/phlex/view/process_attributes.rb
+++ b/test/phlex/view/process_attributes.rb
@@ -4,7 +4,7 @@
extend ViewHelper
view do
- def template
+ def view_template
div(class: "foo")
end
diff --git a/test/phlex/view/renderable/render.rb b/test/phlex/view/renderable/render.rb
index bb3eaf6f..a2c18574 100644
--- a/test/phlex/view/renderable/render.rb
+++ b/test/phlex/view/renderable/render.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class Example < Phlex::HTML
- def template
+ def view_template
h1 { "Hello" }
end
end
@@ -12,7 +12,7 @@ def template
describe "#render" do
with "a view class" do
view do
- def template
+ def view_template
render Example
end
end
@@ -24,14 +24,14 @@ def template
with "another view" do
other_view = Class.new Phlex::HTML do
- def template(&block)
+ def view_template(&block)
div(&block)
end
end
with "markup" do
view do
- define_method :template do
+ define_method :view_template do
render(other_view.new) do
h1 { "Hi!" }
end
@@ -45,7 +45,7 @@ def template(&block)
with "text" do
view do
- define_method :template do
+ define_method :view_template do
render(other_view.new) { "Hello world!" }
end
end
@@ -57,7 +57,7 @@ def template(&block)
with "0-argument lambda" do
view do
- define_method :template do
+ define_method :view_template do
render -> { h1 { "Hi" } }
end
end
@@ -69,7 +69,7 @@ def template(&block)
with "1-argument lambda" do
view do
- define_method :template do
+ define_method :view_template do
render -> (_view) { h1 { "Hi" } }
end
end
@@ -81,7 +81,7 @@ def template(&block)
with "multi-argument proc" do
view do
- define_method :template do
+ define_method :view_template do
render proc { |_a, _b, _c| h1 { "Hi" } }
end
end
diff --git a/test/phlex/view/static_call.rb b/test/phlex/view/static_call.rb
index 0d5c9ce4..602f1efe 100644
--- a/test/phlex/view/static_call.rb
+++ b/test/phlex/view/static_call.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class Example < Phlex::HTML
- def template
+ def view_template
h1 { "Hi" }
end
end
diff --git a/test/phlex/view/svg_tags.rb b/test/phlex/view/svg_tags.rb
index 2214c2e5..e2ab94bf 100644
--- a/test/phlex/view/svg_tags.rb
+++ b/test/phlex/view/svg_tags.rb
@@ -6,7 +6,7 @@
Phlex::SVG::StandardElements.registered_elements.each do |method_name, tag|
with "<#{tag}> called with an underscore prefix while overridden" do
svg_view do
- define_method :template do
+ define_method :view_template do
send("_#{method_name}")
end
@@ -22,7 +22,7 @@
with "<#{tag}> with block content and attributes" do
svg_view do
- define_method :template do
+ define_method :view_template do
send(method_name, class: "class", id: "id", disabled: true, selected: false) { text { "Hello" } }
end
end
@@ -34,7 +34,7 @@
with "<#{tag}> with block text content and attributes" do
svg_view do
- define_method :template do
+ define_method :view_template do
send(method_name, class: "class", id: "id", disabled: true, selected: false) { "content" }
end
end
diff --git a/test/phlex/view/tags.rb b/test/phlex/view/tags.rb
index 7c1efe78..36364e7d 100644
--- a/test/phlex/view/tags.rb
+++ b/test/phlex/view/tags.rb
@@ -6,7 +6,7 @@
Phlex::HTML::StandardElements.registered_elements.each do |method_name, tag|
with "<#{tag}> called with an underscore prefix while overridden" do
view do
- define_method :template do
+ define_method :view_template do
send("_#{method_name}")
end
@@ -22,7 +22,7 @@
with "<#{tag}> with block content and attributes" do
view do
- define_method :template do
+ define_method :view_template do
send(method_name, class: "class", id: "id", disabled: true, selected: false) { h1 { "Hello" } }
end
end
@@ -34,7 +34,7 @@
with "<#{tag}> with block text content and attributes" do
view do
- define_method :template do
+ define_method :view_template do
send(method_name, class: "class", id: "id", disabled: true, selected: false) { "content" }
end
end
@@ -46,7 +46,7 @@
with "<#{tag}> with string attribute keys" do
view do
- define_method :template do
+ define_method :view_template do
send(method_name, "attribute_with_underscore" => true) { "content" }
end
end
@@ -58,7 +58,7 @@
with "<#{tag}> with hash attribute values" do
view do
- define_method :template do
+ define_method :view_template do
send(method_name, aria: { hidden: true }, data_turbo: { frame: "_top" }) { "content" }
end
end
@@ -72,7 +72,7 @@
Phlex::HTML::VoidElements.registered_elements.each do |method_name, tag|
with "<#{tag}> called with an underscore prefix while overridden" do
view do
- define_method :template do
+ define_method :view_template do
send("_#{method_name}")
end
@@ -88,7 +88,7 @@
with "<#{tag}> with attributes" do
view do
- define_method :template do
+ define_method :view_template do
send(method_name, class: "class", id: "id", disabled: true, selected: false)
end
end
@@ -100,7 +100,7 @@
with "<#{tag}> with string attribute keys" do
view do
- define_method :template do
+ define_method :view_template do
send(method_name, "attribute_with_underscore" => true)
end
end
@@ -112,7 +112,7 @@
with "<#{tag}> with hash attribute values" do
view do
- define_method :template do
+ define_method :view_template do
send(method_name, aria: { hidden: true }, data_turbo: { frame: "_top" })
end
end
diff --git a/test/phlex/view/tokens.rb b/test/phlex/view/tokens.rb
index 36868ddd..38081734 100644
--- a/test/phlex/view/tokens.rb
+++ b/test/phlex/view/tokens.rb
@@ -6,7 +6,7 @@
with "conditional classes" do
with "symbol conditionals" do
view do
- def template
+ def view_template
a href: "/", **classes("a", "b", "c", active?: "active", primary?: ["primary", "d"]) do
"Home"
end
@@ -29,7 +29,7 @@ def primary?
with "proc conditionals" do
view do
- def template
+ def view_template
a href: "/", **classes("a", "b", "c",
-> { true } => "true",
-> { false } => "false") do
@@ -46,7 +46,7 @@ def template
with "negative conditionals" do
view do
- def template
+ def view_template
a href: "/", **classes("a", "b", "c",
active?: {
then: "active",
@@ -69,7 +69,7 @@ def active?
with "no truthy conditionals" do
view do
- def template
+ def view_template
a href: "/", **classes(
active?: {
then: "active"
@@ -92,7 +92,7 @@ def active?
with "nils and empty strings" do
view do
- def template
+ def view_template
div(class: tokens("", nil, false, "foo", "", nil, false, "bar", "", nil, false))
end
end
diff --git a/test/phlex/view/unsafe_raw.rb b/test/phlex/view/unsafe_raw.rb
index e63e12ba..c71d17e2 100644
--- a/test/phlex/view/unsafe_raw.rb
+++ b/test/phlex/view/unsafe_raw.rb
@@ -5,7 +5,7 @@
with "raw content" do
view do
- def template
+ def view_template
unsafe_raw %(Hello
)
end
end
@@ -17,7 +17,7 @@ def template
with "nil content" do
view do
- def template
+ def view_template
unsafe_raw nil
end
end
diff --git a/test/phlex/view/whitespace.rb b/test/phlex/view/whitespace.rb
index c5e0f865..2b715b59 100644
--- a/test/phlex/view/whitespace.rb
+++ b/test/phlex/view/whitespace.rb
@@ -5,7 +5,7 @@
with "whitespace" do
view do
- def template
+ def view_template
a { "Home" }
whitespace
a { "About" }
@@ -19,7 +19,7 @@ def template
with "whitespace around" do
view do
- def template
+ def view_template
whitespace do
a { "Home" }
end
@@ -33,7 +33,7 @@ def template
with "whitespace around a string" do
view do
- def template
+ def view_template
span { "9" }
whitespace { "out of" }
span { "10" }