Skip to content

Commit

Permalink
Rename templateview_template (#630)
Browse files Browse the repository at this point in the history
This is a non-breaking rename that issues a warning whenever you define
`template`.
  • Loading branch information
joeldrapper authored Jan 30, 2024
1 parent 72ac60e commit f0cdace
Show file tree
Hide file tree
Showing 31 changed files with 112 additions and 85 deletions.
2 changes: 1 addition & 1 deletion fixtures/layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize(title: "Example")
@title = title
end

def template(&block)
def view_template(&block)
html do
head do
title { @title }
Expand Down
2 changes: 1 addition & 1 deletion fixtures/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Example
class Page < Phlex::HTML
def template
def view_template
render LayoutComponent.new do
h1 { "Hi" }

Expand Down
2 changes: 1 addition & 1 deletion lib/phlex/deferred_render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# @tabs = []
# end
#
# def template
# def view_template
# @tabs.each { |t| a { t.name } }
# @tabs.each { |t| article(&t.content) }
# end
Expand Down
2 changes: 1 addition & 1 deletion lib/phlex/elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# class MyComponent < Phlex::HTML
# include MyCustomElements
#
# def template
# def view_template
# trix_editor
# end
# end
Expand Down
22 changes: 16 additions & 6 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -117,7 +127,7 @@ def __final_call__(buffer = +"", context: Phlex::Context.new, view_context: nil,
end
end
else
template
view_template
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/phlex/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def before_template
h1 { "Hello" }
end

def template
def view_template
h2 { "World" }
end

Expand Down
6 changes: 3 additions & 3 deletions test/phlex/render_enumerable.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class Card < Phlex::HTML
def template(&block)
def view_template(&block)
article(&block)
end
end
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/phlex/svg.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

class Example < Phlex::SVG
def template
def view_template
svg do
path(d: "123")
end
end
end

class ExampleFromHTML < Phlex::HTML
def template
def view_template
svg do |s|
s.path(d: "321")
end
Expand Down
4 changes: 2 additions & 2 deletions test/phlex/testing/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/phlex/unbuffered.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class Example < Phlex::HTML
def template
def view_template
yield
end

Expand Down
2 changes: 1 addition & 1 deletion test/phlex/view/around_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def around_template
h3 { "After" }
end

def template
def view_template
h2 { "Hello" }
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/phlex/view/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
extend ViewHelper

view do
def template
def view_template
plain "Hi"
end
end
Expand All @@ -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

Expand All @@ -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
Expand All @@ -47,7 +47,7 @@ def template

with "a view that yields nothing" do
view do
def template
def view_template
yield
end

Expand Down
12 changes: 6 additions & 6 deletions test/phlex/view/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
view do
attr_accessor :captured

def template
def view_template
h1 { "Before" }
@captured = capture { "Hello" }
h1 { "After" }
Expand All @@ -24,7 +24,7 @@ def template
view do
attr_accessor :captured

def template
def view_template
h1 { "Before" }
@captured = capture do
h1 { "Hello" }
Expand All @@ -43,7 +43,7 @@ def template
view do
attr_accessor :captured

def template
def view_template
h1 { "Before" }
@captured = capture do
h1 { "Hello" }
Expand All @@ -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" }
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/phlex/view/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

with "simple comment" do
view do
def template
def view_template
comment { "This is an HTML comment" }
end
end
Expand All @@ -17,7 +17,7 @@ def template

with "empty comment" do
view do
def template
def view_template
comment
end
end
Expand All @@ -29,7 +29,7 @@ def template

with "number comment" do
view do
def template
def view_template
comment { 1 }
end
end
Expand All @@ -41,7 +41,7 @@ def template

with "escaped comment" do
view do
def template
def view_template
comment { "<b>Important</b>" }
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/phlex/view/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

with "content" do
view do
def template
def view_template
h1 { "Before" }
yield
h2 { "After" }
Expand Down
2 changes: 1 addition & 1 deletion test/phlex/view/custom_elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
register_element :trix_editor
register_element :trix_toolbar

def template
def view_template
div do
trix_toolbar
trix_editor { "Hello" }
Expand Down
2 changes: 1 addition & 1 deletion test/phlex/view/doctype.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

with "a doctype" do
view do
def template
def view_template
html do
head { doctype }
end
Expand Down
2 changes: 1 addition & 1 deletion test/phlex/view/format_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "date"

class Example < Phlex::HTML
def template
def view_template
h1 { Date.new(2022, 11, 28) }
end

Expand Down
17 changes: 17 additions & 0 deletions test/phlex/view/legacy_template_method.rb
Original file line number Diff line number Diff line change
@@ -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 == "<h1>Hello, world!</h1>"
end
end
Loading

0 comments on commit f0cdace

Please sign in to comment.