x_component v0.1.0 X View Source
Component-based HTML templates for Elixir/Phoenix, inspired by Vue. Zero-dependency. Framework/library agnostic. Optimized for Phoenix and Gettext.
Features
- Declarative HTML template syntax close to Vue.
- Compile time errors and warnings.
- Type checks with dialyzer specs.
- Template code formatter.
- Inline, context-aware components.
- Smart attributes merge.
- Decorator components.
- Fast compilation and rendering.
- Optimized for Gettext/Phoenix/ElixirLS.
- Component scaffolds generator task.
Template Syntax
See more examples here.
~X"""
<body>
<!-- Body -->
<div class="container">
<Breadcrumbs
:crumbs=[
%{to: :root, params: [], title: "Home", active: false},
%{to: :form, params: [], title: "Form", active: true}
]
data-breadcrumbs
/>
<Form :action='"/book/" <> to_string(book.id)'>
{{ @message }}
<FormInput
:label='"Title"'
:name=":title"
:record="book"
/>
<FormInput
:name=":body"
:record="book"
:type=":textarea"
/>
<RadioGroup
:name=":type"
:options=["fiction", "bussines", "tech"]
:record="book"
/>
</Form>
</div>
</body>
"""
Link to this section Summary
Functions
Returns inline compilation option. By default all components are compiled
with inline
option for faster rendering. inline
option is disabled when
extracting gettext to provide context aware AST. To get faster code reload in
developemnt inline
option can be disabled via config
Compiles given template string to elixir AST.
Formats given component file string.
Formats given template string. Returns iodata.
Returns Elixir code snippet that will be added to the body of the component module created via generator task.
Returns a json library module that is used to serialize map
.
By default it uses Phoenix.json_library/1
when used with Phoenix.
Json library can be set via application config
Returns a root component module that is used by components generator and Phoenix.
Returns components directory path used by generator task.
Link to this section Functions
Returns inline compilation option. By default all components are compiled
with inline
option for faster rendering. inline
option is disabled when
extracting gettext to provide context aware AST. To get faster code reload in
developemnt inline
option can be disabled via config:
config :x_component,
compile_inline: false,
Examples
iex> X.compile_inline?()
true
compile_string!(source, env \\ __ENV__, options \\ [])
View Sourcecompile_string!(String.t(), Macro.Env.t(), X.Compiler.options()) :: Macro.t()
Compiles given template string to elixir AST.
Options:
:line
- the line to be used as the template start.:context
- compile all variables in given context. Variables are not context aware whennil
.:inline
- inserts nested component AST into parent component whentrue
. Whenfalse
nested components will be rendered via embedrender/2
functions. Templates compiled withinline
have better performance.
Example
iex> X.compile_string!("<span>Hello {{= example + 1 }} </span>")
[
"<span>Hello ",
{:+, [line: 1], [{:example, [line: 1], nil}, 1]},
" </span>"
]
iex> X.compile_string!("<span>Hello {{= example + 1 }} </span>", __ENV__, context: Example, line: 10)
[
"<span>Hello ",
{:+, [line: 11], [{:example, [line: 11], Example}, 1]},
" </span>"
]
Formats given component file string.
Example
iex> X.format_file!("""
...> defmodule Example do
...> use X.Component,
...> template: ~X"\""
...> <div> example<span/> <hr> </div>
...> "\""
...> end
...> """)
"""
defmodule Example do
use X.Component,
template: ~X"\""
<div> example
<span />
<hr>
</div>
"\""
end
"""
format_string!(source, options \\ [])
View Sourceformat_string!(String.t(), X.Formatter.options()) :: String.t()
Formats given template string. Returns iodata.
Example
iex> X.format_string!("<span><span/>Hello {{= example + 1 }} </span>")
"\n<span>\n <span />Hello {{= example + 1 }} \n</span>"
Returns Elixir code snippet that will be added to the body of the component module created via generator task.
config :x_component,
generator_template: "\""
use MyAppWeb, :component
import String
"\""
Examples
iex> X.generator_template()
" use X.Template\n"
Returns a json library module that is used to serialize map
.
By default it uses Phoenix.json_library/1
when used with Phoenix.
Json library can be set via application config:
config :x_component,
json_library: Jason,
Examples
iex> X.json_library()
Jason
Returns a root component module that is used by components generator and Phoenix.
config :x_component,
root_module: "MyApp.Components"
Examples
iex> X.root_module()
"X.Components"
Returns components directory path used by generator task.
config :x_component,
root_path: "lib/my_app_web/components",
Examples
iex> X.root_path()
"tmp"