Skip to content

Commit

Permalink
Demonstrate using Phlex from other templates
Browse files Browse the repository at this point in the history
  • Loading branch information
benpickles committed Sep 10, 2024
1 parent 5d4242e commit 0d63db8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `</head>` and buffer the remaining content:
Expand Down
24 changes: 23 additions & 1 deletion spec/general_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def view_template
end

class LinkView < Phlex::HTML
def initialize(full)
def initialize(full = false)
@full = full
end

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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('<main><a href="/foo/bar">link</a></main>')
end

it 'allows passing a layout' do
get '/inline_with_layout'

expect(last_response.body).to start_with('<main><div><a href="/bar">link</a></div>')
end
end

context 'when passing content_type' do
it 'responds correctly' do
get '/xml'
Expand Down
1 change: 1 addition & 0 deletions spec/views/inline.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= phlex LinkView.new %>
1 change: 1 addition & 0 deletions spec/views/inline_with_layout.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= phlex LinkView.new, layout: :layout_more %>

0 comments on commit 0d63db8

Please sign in to comment.