Ratatouille v0.5.1 Ratatouille.View View Source

In Ratatouille, a view is simply a tree of elements. Each element in the tree holds an attributes map and a list of zero or more child nodes. Visually, it looks like something this:

%Element{
  tag: :view,
  attributes: %{},
  children: [
    %Element{
      tag: :row,
      attributes: %{},
      children: [
        %Element{tag: :column, attributes: %{size: 4}, children: []},
        %Element{tag: :column, attributes: %{size: 4}, children: []},
        %Element{tag: :column, attributes: %{size: 4}, children: []}
      ]
    }
  ]
}

View DSL

Because it's a bit tedious to define views like that, Ratatouille provides a DSL to define them without all the boilerplate.

Now we can turn the above into this:

view do
  row do
    column(size: 4)
    column(size: 4)
    column(size: 4)
  end
end

While the syntax is more compact, the end result is exactly the same. This expression produces the exact same %Element{} struct as defined above.

To use the DSL like this, we need to import all the functions:

import Ratatouille.View

Alternatively, import just the ones you need:

import Ratatouille.View, only: [view: 0, row: 0, column: 1]

Forms

All of the possible forms are enumerated below.

Element with tag foo:

foo()

Element with tag foo and attributes:

foo(size: 42)

Element with tag foo and children as list:

foo([
  bar()
])

Element with tag foo and children as block:

foo do
  bar()
end

Element with tag foo, attributes, and children as list:

foo(
  [size: 42],
  [bar()]
)

Element with tag foo, attributes, and children as block:

foo size: 42 do
  bar()
end

Empty Elements

Similar to so-called "empty" HTML elements such as <br />, Ratatouille also has elements for which passing content doesn't make sense. For example, the leaf node text stores its content in its attributes and cannot have any child elements of its own.

In such cases, the block and list forms are unsupported.

Validation

While some errors---such as passing children to empty elements---are prevented by the DSL, it's still possible (for now, at least) to build semantically-invalid element trees using the DSL. This means that the elements are being used in a way that doesn't make sense to the renderer.

In order to prevent cryptic rendering errors, the renderer first validates the element tree it's given and rejects the whole thing if the structure is unsupported. It currently checks the following things:

  • The top-level element passed to the renderer must have the :view tag.
  • A parent element may only have child elements that have one of the supported child tags for the parent element.
  • An element must define all of its required attributes and may not define any unknown attributes.

The last two rules are based on the element's specification in Ratatouille.Renderer.Element.

Link to this section Summary

Functions

Defines an element with the :bar tag.

Defines an element with the :bar tag and either

Defines an element with the :bar tag and the given attributes and child elements.

Defines an element with the :canvas tag.

Defines an element with the :canvas tag and either

Defines an element with the :canvas tag and the given attributes and child elements.

Defines an element with the :canvas_cell tag.

Defines an element with the :canvas_cell tag and the given attributes.

Defines an element with the :chart tag.

Defines an element with the :chart tag and the given attributes.

Defines an element with the :column tag.

Defines an element with the :column tag and either

Defines an element with the :column tag and the given attributes and child elements.

Defines an element with the :label tag.

Defines an element with the :label tag and either

Defines an element with the :label tag and the given attributes and child elements.

Defines an element with the :overlay tag.

Defines an element with the :overlay tag and either

Defines an element with the :overlay tag and the given attributes and child elements.

Defines an element with the :panel tag.

Defines an element with the :panel tag and either

Defines an element with the :panel tag and the given attributes and child elements.

Defines an element with the :row tag.

Defines an element with the :row tag and either

Defines an element with the :row tag and the given attributes and child elements.

Defines an element with the :sparkline tag.

Defines an element with the :sparkline tag and the given attributes.

Defines an element with the :table tag.

Defines an element with the :table tag and either

Defines an element with the :table tag and the given attributes and child elements.

Defines an element with the :table_cell tag.

Defines an element with the :table_cell tag and the given attributes.

Defines an element with the :table_row tag.

Defines an element with the :table_row tag and either

Defines an element with the :table_row tag and the given attributes and child elements.

Defines an element with the :text tag.

Defines an element with the :text tag and the given attributes.

Defines an element with the :tree tag.

Defines an element with the :tree tag and either

Defines an element with the :tree tag and the given attributes and child elements.

Defines an element with the :tree_node tag.

Defines an element with the :tree_node tag and either

Defines an element with the :tree_node tag and the given attributes and child elements.

Defines an element with the :view tag.

Defines an element with the :view tag and either

Defines an element with the :view tag and the given attributes and child elements.

Defines an element with the :viewport tag.

Defines an element with the :viewport tag and either

Defines an element with the :viewport tag and the given attributes and child elements.

Link to this section Functions

Defines an element with the :bar tag.

Allowed Child Elements

  • label

Examples

Empty element:

bar()

With a block:

bar do
  # ...child elements...
end
Link to this macro

bar(attributes_or_children)

View Source (macro)

Defines an element with the :bar tag and either:

  • given attributes and an optional block
  • a list of child elements

Attributes

None

Allowed Child Elements

  • label

Examples

Passing attributes:

bar(key: value)

Passing attributes and a block:

bar(key: value) do
  # ...child elements...
end

Passing list of children:

bar([elem1, elem2])
Link to this macro

bar(attributes, children)

View Source (macro)

Defines an element with the :bar tag and the given attributes and child elements.

Attributes

None

Allowed Child Elements

  • label

Examples

bar([key: value], [elem1, elem2])

Defines an element with the :canvas tag.

Allowed Child Elements

  • canvas_cell

Examples

Empty element:

canvas()

With a block:

canvas do
  # ...child elements...
end
Link to this macro

canvas(attributes_or_children)

View Source (macro)

Defines an element with the :canvas tag and either:

  • given attributes and an optional block
  • a list of child elements

Attributes

  • height (required) - Integer representing the canvas height
  • width (required) - Integer representing the canvas width

Allowed Child Elements

  • canvas_cell

Examples

Passing attributes:

canvas(key: value)

Passing attributes and a block:

canvas(key: value) do
  # ...child elements...
end

Passing list of children:

canvas([elem1, elem2])
Link to this macro

canvas(attributes, children)

View Source (macro)

Defines an element with the :canvas tag and the given attributes and child elements.

Attributes

  • height (required) - Integer representing the canvas height
  • width (required) - Integer representing the canvas width

Allowed Child Elements

  • canvas_cell

Examples

canvas([key: value], [elem1, elem2])

Defines an element with the :canvas_cell tag.

Examples

Empty element:

canvas_cell()
Link to this macro

canvas_cell(attributes)

View Source (macro)

Defines an element with the :canvas_cell tag and the given attributes.

Attributes

  • x (required) - Integer representing the cell's column (zero-indexed)
  • y (required) - Integer representing the cell's row (zero-indexed)
  • color (optional) - Constant representing color to use for foreground
  • char (optional) - Single character to render within this cell
  • background (optional) - Constant representing color to use for background
  • attributes (optional) - Constant representing style attributes to apply

Examples

canvas_cell(key: value)

Defines an element with the :chart tag.

Examples

Empty element:

chart()
Link to this macro

chart(attributes)

View Source (macro)

Defines an element with the :chart tag and the given attributes.

Attributes

  • series (required) - List of float or integer values representing the series
  • type (required) - Type of chart to plot. Currently only :line is supported
  • height (optional) - Height of the chart in rows

Examples

chart(key: value)

Defines an element with the :column tag.

Allowed Child Elements

  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

Empty element:

column()

With a block:

column do
  # ...child elements...
end
Link to this macro

column(attributes_or_children)

View Source (macro)

Defines an element with the :column tag and either:

  • given attributes and an optional block
  • a list of child elements

Attributes

  • size (required) - Number of units on the grid that the column should occupy

Allowed Child Elements

  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

Passing attributes:

column(key: value)

Passing attributes and a block:

column(key: value) do
  # ...child elements...
end

Passing list of children:

column([elem1, elem2])
Link to this macro

column(attributes, children)

View Source (macro)

Defines an element with the :column tag and the given attributes and child elements.

Attributes

  • size (required) - Number of units on the grid that the column should occupy

Allowed Child Elements

  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

column([key: value], [elem1, elem2])
Link to this function

element(tag, attributes_or_children)

View Source
Link to this function

element(tag, attributes, children)

View Source

Defines an element with the :label tag.

Allowed Child Elements

  • text

Examples

Empty element:

label()

With a block:

label do
  # ...child elements...
end
Link to this macro

label(attributes_or_children)

View Source (macro)

Defines an element with the :label tag and either:

  • given attributes and an optional block
  • a list of child elements

Attributes

  • content (optional) - Binary containing the text content to be displayed
  • color (optional) - Constant representing color to use for foreground
  • background (optional) - Constant representing color to use for background
  • attributes (optional) - Constant representing style attributes to apply
  • wrap (optional) - Boolean indicating whether or not to wrap lines to fit available space

Allowed Child Elements

  • text

Examples

Passing attributes:

label(key: value)

Passing attributes and a block:

label(key: value) do
  # ...child elements...
end

Passing list of children:

label([elem1, elem2])
Link to this macro

label(attributes, children)

View Source (macro)

Defines an element with the :label tag and the given attributes and child elements.

Attributes

  • content (optional) - Binary containing the text content to be displayed
  • color (optional) - Constant representing color to use for foreground
  • background (optional) - Constant representing color to use for background
  • attributes (optional) - Constant representing style attributes to apply
  • wrap (optional) - Boolean indicating whether or not to wrap lines to fit available space

Allowed Child Elements

  • text

Examples

label([key: value], [elem1, elem2])

Defines an element with the :overlay tag.

Allowed Child Elements

  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

Empty element:

overlay()

With a block:

overlay do
  # ...child elements...
end
Link to this macro

overlay(attributes_or_children)

View Source (macro)

Defines an element with the :overlay tag and either:

  • given attributes and an optional block
  • a list of child elements

Attributes

  • padding (optional) - Integer number of units of padding

Allowed Child Elements

  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

Passing attributes:

overlay(key: value)

Passing attributes and a block:

overlay(key: value) do
  # ...child elements...
end

Passing list of children:

overlay([elem1, elem2])
Link to this macro

overlay(attributes, children)

View Source (macro)

Defines an element with the :overlay tag and the given attributes and child elements.

Attributes

  • padding (optional) - Integer number of units of padding

Allowed Child Elements

  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

overlay([key: value], [elem1, elem2])

Defines an element with the :panel tag.

Allowed Child Elements

  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

Empty element:

panel()

With a block:

panel do
  # ...child elements...
end
Link to this macro

panel(attributes_or_children)

View Source (macro)

Defines an element with the :panel tag and either:

  • given attributes and an optional block
  • a list of child elements

Attributes

  • color (optional) - Color of title
  • background (optional) - Background of title
  • attributes (optional) - Attributes for the title
  • padding (optional) - Integer providing inner padding to use when rendering child elements
  • height (optional) - Height of the table in rows or :fill to fill the parent container's box
  • title (optional) - Binary containing the title for the panel

Allowed Child Elements

  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

Passing attributes:

panel(key: value)

Passing attributes and a block:

panel(key: value) do
  # ...child elements...
end

Passing list of children:

panel([elem1, elem2])
Link to this macro

panel(attributes, children)

View Source (macro)

Defines an element with the :panel tag and the given attributes and child elements.

Attributes

  • color (optional) - Color of title
  • background (optional) - Background of title
  • attributes (optional) - Attributes for the title
  • padding (optional) - Integer providing inner padding to use when rendering child elements
  • height (optional) - Height of the table in rows or :fill to fill the parent container's box
  • title (optional) - Binary containing the title for the panel

Allowed Child Elements

  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

panel([key: value], [elem1, elem2])

Defines an element with the :row tag.

Allowed Child Elements

  • column

Examples

Empty element:

row()

With a block:

row do
  # ...child elements...
end
Link to this macro

row(attributes_or_children)

View Source (macro)

Defines an element with the :row tag and either:

  • given attributes and an optional block
  • a list of child elements

Attributes

None

Allowed Child Elements

  • column

Examples

Passing attributes:

row(key: value)

Passing attributes and a block:

row(key: value) do
  # ...child elements...
end

Passing list of children:

row([elem1, elem2])
Link to this macro

row(attributes, children)

View Source (macro)

Defines an element with the :row tag and the given attributes and child elements.

Attributes

None

Allowed Child Elements

  • column

Examples

row([key: value], [elem1, elem2])

Defines an element with the :sparkline tag.

Examples

Empty element:

sparkline()
Link to this macro

sparkline(attributes)

View Source (macro)

Defines an element with the :sparkline tag and the given attributes.

Attributes

  • series (required) - List of float or integer values representing the series

Examples

sparkline(key: value)

Defines an element with the :table tag.

Allowed Child Elements

  • table_row

Examples

Empty element:

table()

With a block:

table do
  # ...child elements...
end
Link to this macro

table(attributes_or_children)

View Source (macro)

Defines an element with the :table tag and either:

  • given attributes and an optional block
  • a list of child elements

Attributes

None

Allowed Child Elements

  • table_row

Examples

Passing attributes:

table(key: value)

Passing attributes and a block:

table(key: value) do
  # ...child elements...
end

Passing list of children:

table([elem1, elem2])
Link to this macro

table(attributes, children)

View Source (macro)

Defines an element with the :table tag and the given attributes and child elements.

Attributes

None

Allowed Child Elements

  • table_row

Examples

table([key: value], [elem1, elem2])

Defines an element with the :table_cell tag.

Examples

Empty element:

table_cell()
Link to this macro

table_cell(attributes)

View Source (macro)

Defines an element with the :table_cell tag and the given attributes.

Attributes

  • content (required) - Binary containing the text content to be displayed
  • color (optional) - Constant representing color to use for foreground
  • background (optional) - Constant representing color to use for background
  • attributes (optional) - Constant representing style attributes to apply

Examples

table_cell(key: value)

Defines an element with the :table_row tag.

Allowed Child Elements

  • table_cell

Examples

Empty element:

table_row()

With a block:

table_row do
  # ...child elements...
end
Link to this macro

table_row(attributes_or_children)

View Source (macro)

Defines an element with the :table_row tag and either:

  • given attributes and an optional block
  • a list of child elements

Attributes

  • color (optional) - Constant representing color to use for foreground
  • background (optional) - Constant representing color to use for background
  • attributes (optional) - Constant representing style attributes to apply

Allowed Child Elements

  • table_cell

Examples

Passing attributes:

table_row(key: value)

Passing attributes and a block:

table_row(key: value) do
  # ...child elements...
end

Passing list of children:

table_row([elem1, elem2])
Link to this macro

table_row(attributes, children)

View Source (macro)

Defines an element with the :table_row tag and the given attributes and child elements.

Attributes

  • color (optional) - Constant representing color to use for foreground
  • background (optional) - Constant representing color to use for background
  • attributes (optional) - Constant representing style attributes to apply

Allowed Child Elements

  • table_cell

Examples

table_row([key: value], [elem1, elem2])

Defines an element with the :text tag.

Examples

Empty element:

text()
Link to this macro

text(attributes)

View Source (macro)

Defines an element with the :text tag and the given attributes.

Attributes

  • content (required) - Binary containing the text content to be displayed
  • color (optional) - Constant representing color to use for foreground
  • background (optional) - Constant representing color to use for background
  • attributes (optional) - Constant representing style attributes to apply

Examples

text(key: value)

Defines an element with the :tree tag.

Allowed Child Elements

  • tree_node

Examples

Empty element:

tree()

With a block:

tree do
  # ...child elements...
end
Link to this macro

tree(attributes_or_children)

View Source (macro)

Defines an element with the :tree tag and either:

  • given attributes and an optional block
  • a list of child elements

Attributes

None

Allowed Child Elements

  • tree_node

Examples

Passing attributes:

tree(key: value)

Passing attributes and a block:

tree(key: value) do
  # ...child elements...
end

Passing list of children:

tree([elem1, elem2])
Link to this macro

tree(attributes, children)

View Source (macro)

Defines an element with the :tree tag and the given attributes and child elements.

Attributes

None

Allowed Child Elements

  • tree_node

Examples

tree([key: value], [elem1, elem2])

Defines an element with the :tree_node tag.

Allowed Child Elements

  • tree_node

Examples

Empty element:

tree_node()

With a block:

tree_node do
  # ...child elements...
end
Link to this macro

tree_node(attributes_or_children)

View Source (macro)

Defines an element with the :tree_node tag and either:

  • given attributes and an optional block
  • a list of child elements

Attributes

  • content (required) - Binary label for the node
  • color (optional) - Constant representing color to use for foreground
  • background (optional) - Constant representing color to use for background
  • attributes (optional) - Constant representing style attributes to apply

Allowed Child Elements

  • tree_node

Examples

Passing attributes:

tree_node(key: value)

Passing attributes and a block:

tree_node(key: value) do
  # ...child elements...
end

Passing list of children:

tree_node([elem1, elem2])
Link to this macro

tree_node(attributes, children)

View Source (macro)

Defines an element with the :tree_node tag and the given attributes and child elements.

Attributes

  • content (required) - Binary label for the node
  • color (optional) - Constant representing color to use for foreground
  • background (optional) - Constant representing color to use for background
  • attributes (optional) - Constant representing style attributes to apply

Allowed Child Elements

  • tree_node

Examples

tree_node([key: value], [elem1, elem2])

Defines an element with the :view tag.

Allowed Child Elements

  • overlay
  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

Empty element:

view()

With a block:

view do
  # ...child elements...
end
Link to this macro

view(attributes_or_children)

View Source (macro)

Defines an element with the :view tag and either:

  • given attributes and an optional block
  • a list of child elements

Attributes

  • top_bar (optional) - A :bar element to occupy the view's first row
  • bottom_bar (optional) - A :bar element to occupy the view's last row

Allowed Child Elements

  • overlay
  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

Passing attributes:

view(key: value)

Passing attributes and a block:

view(key: value) do
  # ...child elements...
end

Passing list of children:

view([elem1, elem2])
Link to this macro

view(attributes, children)

View Source (macro)

Defines an element with the :view tag and the given attributes and child elements.

Attributes

  • top_bar (optional) - A :bar element to occupy the view's first row
  • bottom_bar (optional) - A :bar element to occupy the view's last row

Allowed Child Elements

  • overlay
  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

view([key: value], [elem1, elem2])

Defines an element with the :viewport tag.

Allowed Child Elements

  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

Empty element:

viewport()

With a block:

viewport do
  # ...child elements...
end
Link to this macro

viewport(attributes_or_children)

View Source (macro)

Defines an element with the :viewport tag and either:

  • given attributes and an optional block
  • a list of child elements

Attributes

  • offset_x (optional) - Integer representing the number of columns to offset the child content by. Defaults to 0.
  • offset_y (optional) - Integer representing the number of rows to offset the child content by. Defaults to 0.

Allowed Child Elements

  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

Passing attributes:

viewport(key: value)

Passing attributes and a block:

viewport(key: value) do
  # ...child elements...
end

Passing list of children:

viewport([elem1, elem2])
Link to this macro

viewport(attributes, children)

View Source (macro)

Defines an element with the :viewport tag and the given attributes and child elements.

Attributes

  • offset_x (optional) - Integer representing the number of columns to offset the child content by. Defaults to 0.
  • offset_y (optional) - Integer representing the number of rows to offset the child content by. Defaults to 0.

Allowed Child Elements

  • canvas
  • chart
  • label
  • panel
  • row
  • sparkline
  • table
  • tree
  • viewport

Examples

viewport([key: value], [elem1, elem2])