Skip to content

Support Flat Data#31

Open
drewknab wants to merge 1 commit into
schultyy:masterfrom
drewknab:support-flat-data-nested-template
Open

Support Flat Data#31
drewknab wants to merge 1 commit into
schultyy:masterfrom
drewknab:support-flat-data-nested-template

Conversation

@drewknab

Copy link
Copy Markdown

Support nested Section from flat data.

Nested template:

Hello {{name}}
You have just won {{value}} dollars!
{{#in_ca}}
Well, {{taxed_value}} dollars, after taxes.
{{/in_ca}}

Flat Data Example:

{
  "name": "Chris",
  "value": 10000,
  "taxed_value": 10000 - (10000 * 0.4),
  "in_ca": true
}

Produces Example:

Hello Chris
You have just won 10000 dollars!
Well, 6000.0 dollars, after taxes.

  defp process_if(template, val) do
    case val do
      nil -> ""
      false -> ""
      [] -> ""
      [_ | _] -> val
                 |> Stream.map(&(render(template, &1)))
                 |> Enum.join()
      val -> render(template, val) # With the above example, this val is true. Function render tries to access a key on a boolean value and throws an exception.
    end
  end

Proposed code change adds a case for true and that runs data through render instead of val.

Tests added, previous tests still passing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant