Skip to content

Commit

Permalink
Add echo tag
Browse files Browse the repository at this point in the history
  • Loading branch information
edgurgel committed Jul 7, 2024
1 parent cda318d commit 443a3e0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/solid/tag/echo.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Solid.Tag.Echo do
import NimbleParsec
alias Solid.Parser.{BaseTag, Literal, Argument}

@behaviour Solid.Tag

@impl true
def spec(_parser) do
space = Literal.whitespace(min: 0)

ignore(BaseTag.opening_tag())
|> ignore(string("echo"))
|> ignore(space)
|> tag(Argument.argument(), :argument)
|> optional(tag(repeat(Argument.filter()), :filters))
|> ignore(BaseTag.closing_tag())
end

@impl true
def render([argument: argument, filters: filters], context, options) do
{:ok, value, context} =
Solid.Argument.get(argument, context, [{:filters, filters} | options])

{[text: value], context}
end
end
16 changes: 16 additions & 0 deletions test/solid/tag/echo_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Solid.Tag.EchoTest do
use ExUnit.Case, async: true
alias Solid.Tag.Echo
alias Solid.Context

defmodule Parser do
import NimbleParsec
defparsec(:parse, Echo.spec(__MODULE__) |> eos())
end

test "integration" do
{:ok, parsed, _, _, _, _} = "{% echo 'abc' | upcase %}" |> Parser.parse()

assert {[text: "ABC"], context} = Echo.render(parsed, %Context{}, [])
end
end

0 comments on commit 443a3e0

Please sign in to comment.