PhoenixHtmlSimplifiedHelpers
Installation
If available in Hex, the package can be installed as:
-
Add phoenix_html_simplified_helpers to your list of dependencies in
mix.exs:
def deps do
[{:phoenix_html_simplified_helpers, "~> x.x.x"}]
end- Ensure phoenix_html_simplified_helpers is started before your application:
def application do
[applications: [:phoenix_html_simplified_helpers]]
end- phoenix_html_simplified_helpers need to import(use) your Phoenix project. The following description is adding 'use syntax' into web.ex.
def view do
quote do
use Phoenix.View, root: "web/templates"
# Import convenience functions from controllers
import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
# Use all HTML functionality (forms, tags, etc)
use Phoenix.HTML
use Phoenix.HTML.SimplifiedHelpers # this line.
import MyApp.Router.Helpers
import MyApp.ErrorHelpers
import MyApp.Gettext
end
endAlso it is able to import(use) in each view helper files.
defmodule MyApp.LayoutView do
use MyApp.Web, :view
import Phoenix.HTML.SimplifiedHelpers.Truncate # this line.
import Phoenix.HTML.SimplifiedHelpers.TimeAgoInWords # this line.
end- time_ago_in_words has Gettext module that is changed translation file from project's locale.
Gettext.put_locale(Phoenix.HTML.SimplifiedHelpers.Gettext, "en")Usage
<%= truncate entity.content %>truncate "Once upon a time in a world far far away"
# Once upon a time in a world...
truncate "Once upon a time in a world far far away", length: 27
# Once upon a time in a wo...
truncate("And they found that many people were sleeping better.", length: 25, omission: "... (continued)"
# And they f... (continued)<%= time_ago_in_words entity.published_at %> agotime_ago_in_words Timex.Date.now
# less than 5 seconds
time_ago_in_words Timex.Date.now(:secs)
# less than 5 seconds
time_ago_in_words Ecto.DateTime.utc
# less than 5 seconds