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

Demonstrate using Phlex from other templates #6

Merged
merged 1 commit into from
Sep 10, 2024
Merged
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
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 %>
Loading