mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Bypass Template Rendering as a phoenix til
This commit is contained in:
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
|
|||||||
warrant a full blog post. These are mostly things I learn by pairing with
|
warrant a full blog post. These are mostly things I learn by pairing with
|
||||||
smart people at [Hashrocket](http://hashrocket.com/).
|
smart people at [Hashrocket](http://hashrocket.com/).
|
||||||
|
|
||||||
_469 TILs and counting..._
|
_470 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -192,6 +192,7 @@ _469 TILs and counting..._
|
|||||||
|
|
||||||
### Phoenix
|
### Phoenix
|
||||||
|
|
||||||
|
- [Bypass Template Rendering](phoenix/bypass-template-rendering.md)
|
||||||
- [Render A Template To A String](phoenix/render-a-template-to-a-string.md)
|
- [Render A Template To A String](phoenix/render-a-template-to-a-string.md)
|
||||||
|
|
||||||
### PostgreSQL
|
### PostgreSQL
|
||||||
|
|||||||
32
phoenix/bypass-template-rendering.md
Normal file
32
phoenix/bypass-template-rendering.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# Bypass Template Rendering
|
||||||
|
|
||||||
|
Generally when rendering a response to a request in a Phoenix app, the
|
||||||
|
controller will make a render call that targets a specific template. If it
|
||||||
|
suits our needs, we can skip writing a template and bypass the template
|
||||||
|
portion of the response pipeline by implementing our own `render` function
|
||||||
|
directly in the view module.
|
||||||
|
|
||||||
|
Consider the following route and controller action:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
# web/router.ex
|
||||||
|
get "/hello", UserController, :hello
|
||||||
|
|
||||||
|
# web/controllers/user_controller.ex
|
||||||
|
def hello(conn, _params) do
|
||||||
|
render(conn, "hello.html")
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
The render call would normally trigger a corresponding template function,
|
||||||
|
but we bypass it by adding the following function to our view module:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
# web/views/user_view.ex
|
||||||
|
def render("hello.html", _assigns) do
|
||||||
|
"Hello, World!"
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
Visiting `/hello` will render a page with the view's layout and the words
|
||||||
|
"Hello, World!".
|
||||||
Reference in New Issue
Block a user