diff --git a/README.md b/README.md index d0633cf..5377fd5 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,20 @@ get '/foo' do end ``` +## Using Phlex in other templates + +It's also possible to call `phlex` from within other views, for instance an ERB template: + +```erb +<%= phlex MyView.new %> +``` + +A `layout` can also be passed: + +```erb +<%= phlex MyView.new, layout: :wrapper %> +``` + ## Streaming Streaming a Phlex view can be enabled by passing `stream: true` which will cause Phlex to automatically write to the response after the closing `` and buffer the remaining content: diff --git a/spec/general_spec.rb b/spec/general_spec.rb index b370a73..14f0cbc 100644 --- a/spec/general_spec.rb +++ b/spec/general_spec.rb @@ -11,7 +11,7 @@ def view_template end class LinkView < Phlex::HTML - def initialize(full) + def initialize(full = false) @full = full end @@ -52,6 +52,14 @@ class TestApp < Sinatra::Application FooView.call end + get '/inline' do + erb :inline + end + + get '/inline_with_layout' do + erb :inline_with_layout + end + get '/link' do phlex LinkView.new(params[:full]) end @@ -131,6 +139,20 @@ def app end end + context 'when #phlex is called from within another view' do + it 'works the same' do + get '/inline', {}, { 'SCRIPT_NAME' => '/foo' } + + expect(last_response.body).to start_with('
link
') + end + + it 'allows passing a layout' do + get '/inline_with_layout' + + expect(last_response.body).to start_with('
link
') + end + end + context 'when passing content_type' do it 'responds correctly' do get '/xml' diff --git a/spec/views/inline.erb b/spec/views/inline.erb new file mode 100644 index 0000000..d3f3c46 --- /dev/null +++ b/spec/views/inline.erb @@ -0,0 +1 @@ +<%= phlex LinkView.new %> diff --git a/spec/views/inline_with_layout.erb b/spec/views/inline_with_layout.erb new file mode 100644 index 0000000..6eab4de --- /dev/null +++ b/spec/views/inline_with_layout.erb @@ -0,0 +1 @@ +<%= phlex LinkView.new, layout: :layout_more %>