@@ -10,7 +10,7 @@ Add plug_assign to your list of dependencies in `mix.exs`:
1010
1111``` elixir 
1212    def  deps  do 
13-       [{:plug_assign , " ~> 1.0.0 "  }]
13+       [{:plug_assign , " ~> 1.0.2 "  }]
1414    end 
1515``` 
1616
@@ -20,32 +20,104 @@ Fetch and install the dependencies
2020    $ mix deps.get
2121``` 
2222
23- Define assigns as part of your plug stack
23+ ## Basic Usage  
24+ 
25+ Define assigns as part of any plug stack.
2426
2527``` elixir 
2628    plug Plug .Assign , foo:  " bar"  , bar:  true , baz:  42 
2729``` 
2830
29- Or Set variables for templates to use in a Phoenix Pipeline
31+ This will set the given key/value pairs in the ` assigns `  map of the Plug.Conn, for you to use
32+ in controllers, views, and templates.
33+ 
34+ One way to use this is to set variables so you can quickly determine where you are in your app.
3035
3136``` elixir 
37+     pipeline :browser  do 
38+       .. .
39+       plug Plug .Assign , admin:  false 
40+     end 
41+ 
3242    pipeline :admin  do 
3343      plug Plug .Assign , admin:  true 
3444    end 
3545``` 
3646
37- Or directly in a Phoenix Controller
47+ And then use that in a template, such as your layout.
48+ 
49+ ``` html 
50+   <%= if @admin do %>
51+     <p >Hello, Administrator</p >
52+   <% end %>
53+ ``` 
54+ 
55+ Since you can use this anywhere you can use a plug, you can also use it in a Phoenix Controller.
56+ 
57+ ``` elixir 
58+     defmodule  HelloWeb .Admin .PostController  do 
59+       use  HelloWeb , :controller 
60+ 
61+       plug Plug .Assign , section:  :posts 
62+       .. .
63+     end 
64+ ``` 
65+ 
66+ ``` html 
67+   <h1 ><%= @section %></h1 >
68+ ``` 
69+ 
70+ If you want to only assign a variable for certain actions, you can use the ` plug ... when `  format,
71+ but be careful that bare keyword lists are only accepted as the last arguments of a macro call,
72+ which is no longer the case with a ` when `  clause, so add the square brackets ` [] `  around your
73+ assigns, or use a map instead.
3874
3975``` elixir 
40-     defmodule  Blog .Admin .PostController  do 
41-       use  Blog . Web , :Controller  
76+     defmodule  HelloWeb .Admin .PostController  do 
77+       use  HelloWeb , :controller  
4278
43-       plug Plug .Assign , subsection:   :posts 
44-       plug Plug .Assign , %{read_request :  true } when  action in  [:index  , :show  ]
79+       plug Plug .Assign , [ read_request:   true ]  when  action  in  [ :index ,  :show ] 
80+       plug Plug .Assign , %{write_request :  true } when  action in  [:edit  , :new  ,  :create ,  :update ,  :delete ]
4581      .. .
4682    end 
4783``` 
4884
85+ ## Accessing in templates  
86+ 
87+ If you're using an assign in a template that may not have been set, you can't use the
88+ ` @variable `  format without throwing an error, so use one of the following techniques instead:
89+ 
90+ -  Access shortcut: ` assigns[:admin] `  - Returns ` nil `  if not set.
91+ -  Map.get/2: ` Map.get(assigns, :admin) `  - Returns ` nil `  if not set.
92+ -  Map.get/3: ` Map.get(assigns, :admin, false) -  ` Returns ` false `  if not set.
93+ 
94+ ``` html 
95+   <%= if assigns[:admin] do %>
96+     <p >Hello, Administrator</p >
97+   <% end %>
98+ ``` 
99+ 
100+ ## Assignment formats  
101+ 
102+ Assigns can be either a keyword list
103+ 
104+ ``` elixir 
105+     plug Plug .Assign , foo:  " bar"  , bar:  true , baz:  42 
106+ ``` 
107+ 
108+ Or a map, as long as the keys are atoms.
109+ 
110+ ``` elixir 
111+     plug Plug .Assign , %{foo:  " foo"  , bar:  true , baz:  42 }
112+     plug Plug .Assign , %{:foo  =>  " foo"  , :bar  =>  true , :baz  =>  42 }
113+ ``` 
114+ 
115+ Any attempt to pass it anything else will throw an exception.
116+ 
117+ ``` bash 
118+ **  (ArgumentError) Invalid assignment, must be a keyword list or map with all keys as atoms
119+ ``` 
120+ 
49121## Generate documentation  
50122
51123``` bash 
0 commit comments