View Source Pyro.Component.Helpers (Pyro v0.3.7)

A library of helpers for using/authoring Pyro components.

Summary

Functions

Provides a configurable fallback timezone. Defaults to "Etc/UTC".

Safely get nested values from maps or keyword lists that may be nil or an otherwise non-map value at any point. Great for accessing nested assigns in a template.

A JS function to toggle CSS classes, since Phoenix does not yet provide one out of the box.

Translates the errors for a field from a keyword list of errors.

Functions

Provides a configurable fallback timezone. Defaults to "Etc/UTC".

# runtime.exs
config :pyro, default_timezone: "America/Chicago"

Note:

Requires a timezone database to be properly installed and configured.

Link to this function

format_datetime(timestamp, tz, formatter \\ &simple_datetime_formatter/1)

View Source

Formats datetimes to localized format.

Examples

iex> format_datetime(DateTime.new!(~D[2016-05-24], ~T[13:26:08.003], "Etc/UTC"), default_timezone())
"2016-05-24 13:26 UTC"

iex> format_datetime(~N[2000-01-01 23:00:07], "Etc/UTC")
"2000-01-01 23:00 UTC"

iex> format_datetime(~N[2000-01-01 23:00:07], "Etc/UTC", :date_time_seconds_timezone)
"2000-01-01 23:00:07 UTC"

iex> format_datetime(nil, "Etc/UTC")
""

iex> format_datetime("", "Etc/UTC")
""
Link to this function

get_nested(value, keys, default \\ nil)

View Source

Safely get nested values from maps or keyword lists that may be nil or an otherwise non-map value at any point. Great for accessing nested assigns in a template.

Examples

iex> get_nested(nil, [:one, :two, :three])
nil

iex> get_nested(%{one: nil}, [:one, :two, :three])
nil

iex> get_nested(%{one: %{two: %{three: 3}}}, [:one, :two, :three])
3

iex> get_nested(%{one: %{two: [three: 3]}}, [:one, :two, :three])
3

iex> get_nested([one: :nope], [:one, :two, :three])
nil

iex> get_nested([one: :nope], [:one, :two, :three], :default)
:default
Link to this function

toggle_class(js \\ %JS{}, classes, opts)

View Source
@spec toggle_class(js :: map(), classes :: String.t(), opts :: keyword()) :: map()

A JS function to toggle CSS classes, since Phoenix does not yet provide one out of the box.

Examples

toggle_class("rotate-180 bg-green", to: "#icon")
Link to this function

translate_errors(errors, field)

View Source

Translates the errors for a field from a keyword list of errors.