Torch is a rapid admin generator for Phoenix apps. It uses generators rather than DSLs to ensure that the code remains maintainable.
To install Torch, perform the following steps:
- Add
torch
to your list of dependencies inmix.exs
. Then, runmix deps.get
:
def deps do
[{:torch, "~> 1.0.0-rc.6"}]
end
- Ensure
:torch
is started in your applications list inmix.exs
:
def application do
[applications: [:torch]]
end
- Add
torch
to yourpackage.json
dependencies. Then, runnpm install
.
"dependencies": {
"phoenix": "file:deps/phoenix",
"phoenix_html": "file:deps/phoenix_html",
+ "torch": "file:deps/torch"
},
- Import
torch.js
in yourapp.js
:
import torch from "torch/priv/static/torch"
- Import
torch.css
in yourapp.scss
:
@import "~torch/priv/static/torch";
-
Run
mix torch.install (eex|slim)
to install the relevant Torch files. You can choose betweeneex
templates andslim
templates. If you choose to useslim
templates, you will need to install Phoenix Slim. -
Set up CSS as described below.
Torch provides its CSS in two ways:
- A precompiled css file in
priv/static/css/torch.css
. - SASS styles in
assets/css/app.sass
If you want to customize the look and feel of your admin, you should use the SASS styles. Update your app.scss
file to look like this:
@import "admin_variables";
@import "~torch/assets/css/app";
The admin_variables
file was generated when you ran mix torch.install
.
Then, simply uncomment and customize the variables in admin_variables.scss
to change how Torch is styled.
If you're not using SASS, then you will need to configure your asset pipeline to compile the precompiled torch.css
.
Using Webpack? Check out the Webpack guide Using Brunch? Check out the Brunch guide
To test that you have torch styles and static assets installed and bundled properly, you can add a torch test componet to your markup. In HTML, the test components looks like this:
<div class="torch-test-component">
<div class="fa fa-heart fa-pull-left fa-3x"></div>
<div class="infinite-red-logo"></div>
</div>
When adding the test component to your markup, you should see a FontAwesome heart icon and the Infinte Red logo. This will ensure that Brunch, or your asset builder of choice, correctly built Torch's static assets.
Run mix torch.gen (eex|slim)
to generate admin controllers and views for a given Ecto schema module. Torch expects you to have already defined the schema in your project.
Also, Torch expects you to have phoenix_slime
installed and configured if you generate slim
templates.
The full format is as follows:
mix torch.gen (eex|slim) [Admin | term for admin] [Singular model term] [plural model term] (sort field) (sort_direction) (attribute:attribute type)
For example, if we wanted to generate an admin area for a Post
model we already have using eex
templates, we could run this command:
$ mix torch.gen eex Admin Post posts inserted_at desc title:string body:text inserted_at:date
And the output would be:
Success!
You should now add a route to the new controller to your `router.ex`, within the `:admin` scope:
scope "/admin", Example.Admin, as: :admin do
pipe_through :browser
resources "/posts", PostController
end
And update the `layout/admin.html.eex` navigation:
<header id="main-header">
<nav>
<h1>Torch Admin</h1>
<ul>
<li><%= Torch.NavigationView.nav_link @conn, "Posts", admin_post_path(@conn, :index) %></a>
</ul>
</nav>
</header>
The command created the following files for us:
web/templates/admin/post/index.html.eex
web/templates/admin/post/edit.html.eex
web/templates/admin/post/new.html.eex
web/templates/admin/post/_form.html.eex
web/templates/admin/post/_filters.html.eex
web/controllers/admin/post_controller.ex
web/views/admin/post_view.ex
If you hook up the routes as described above, you'll see a fully featured CRUD interface for posts, including sophisticated filtering, sorting and search at http://localhost:4000/admin/posts.
To learn more about the torch.gen
task, run:
mix help torch.gen
Torch, as open source projects, is free to use and always will be. Infinite Red offers premium Torch support and general web app design/development services. Email us at hello@infinite.red to get in touch with us for more details.