forked from ChicagoBoss/ChicagoBoss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME_ELIXIR
136 lines (85 loc) · 3.25 KB
/
README_ELIXIR
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
Using Elxir with Chicago Boss
==
Elixir is a nifty new language for the Erlang VM which you can use in parts of
your project instead of regular Erlang if you prefer.
Supported features:
* Elixir web controllers
* Embedded Elixir views
* Elixir library files in src/lib/ (but no inter-file macros!)
Requirements:
* Erlang R15B01 or later
* A love of danger
Learn more about Elixir at http://www.elixir-lang.org/
Setup
--
To enable Elixir, you need to do the following:
1. Uncomment {elixir, ...} in the "deps" section of rebar.config in the
ChicagoBoss directory
2. Copy priv/web_controller.ex to the src/ directory in ChicagoBoss
3. Download Elixir:
./rebar get-deps
4. Compile everything:
./rebar compile
5. Make a new project:
make app PROJECT=my_application
Controller API
--
Put your Elixir controllers into src/controller/ as before. The source files
should NOT be prefixed with the application name. Example:
src/controller/puppy_controller.ex
Open up the controller and write a basic module, like:
defmodule MyApplication.PuppyController do
use Boss.WebController
get :index, [] do
{:output, "Hello, world!"}
end
get about, [] do
{:output, "Hello, world!"}
end
end
To test it out, start the server and visit:
http://localhost:8001/puppy/index
The ChicagoBoss API is essentially the same as the Erlang one. Note that
functions corresponding to the same action should be grouped together:
get :index, [] do {:output, "Good"} end
get :index2, [] do {:output, "Still good"} end
post :index2, [] do {:output, "Fine"} end
post :index, [] do {:output, "BAD"} end
There are two variables implicitly available to your handlers: "req" (the
SimpleBridge request object) and "session_id" (the session identifier, if
present). If you don't like implicit variables, you can write your handlers
in an explicit style:
def index(req, session_id, :GET, []) do
{:output, "Hello, world!"}
end
Other macros available include "before_", "after_", and "lang_", which take the
same arguments as their Erlang counterparts.
Tokens are passed in as Elixir strings (binaries), not Erlang strings.
Feel free to mix and match Elixir and Erlang controllers in the same project,
but watch out for name collisions.
You can access the regular Chicago Boss Erlang API using atoms:
get :index, [] do
puppies = :boss_db.find(:puppy, [{:name, :equals, "Fido"}])
{:ok, [{:puppies, puppies}]}
end
View API
--
Chicago Boss also support EEx files (Embedded Elixir). These should go in
src/view/<controller name>/ and have an extension ".eex". Example:
src/view/puppy/index.eex
You can open it up and write a basic template like:
Puppy name: <%= @puppy.name %>
Note that top-level variables should be prefixed with @.
To populate this template, you'll want something in your controller like:
get :index, [] do
{:ok, [{:puppy, [{:name, "Fido"}]}]}
end
To write a loop, use Enum.map:
<%= Enum.map @puppies, fn(pup) -> %>
Pupply name: <%= pup.name %><br>
<% end %>
This is a comment:
<%# Comment %>
Of course, you're free to continue using Django templates with extension ".dtl"
if you prefer a dedicated template language.
Enjoy!