Skip to content

Commit

Permalink
Support variables from root context (#153)
Browse files Browse the repository at this point in the history
* Support variables from root context

* Update spec_sections.jl

---------

Co-authored-by: tz-lom <nuzhdin.urii@gmail.com>
  • Loading branch information
tz-lom and tz-lom authored Jun 13, 2023
1 parent 5ea01f8 commit a51d484
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Mustache"
uuid = "ffc61752-8dc7-55ee-8c37-f3e9cdd09e70"
version = "1.0.16"
version = "1.0.17"

[deps]
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Expand Down
35 changes: 19 additions & 16 deletions src/context.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,26 @@ function lookup(ctx::Context, key)
## do something with "."
## we use .[ind] to refer to value in parent of given index;
m = match(r"^\.\[(.*)\]$", key)
m === nothing && break

idx = m.captures[1]
vals = context.parent.view

# this has limited support for indices: "end", or a number, but no
# arithmetic, such as `end-1`.
## This is for an iterable; rather than
## limit the type, we let non-iterables error.
if true # isa(vals, AbstractVector) || isa(vals, Tuple) # supports getindex(v, i)?
if idx == "end"
value′ = AnIndex(-1, vals[end])


if m !== nothing
idx = m.captures[1]
vals = context.parent.view

# this has limited support for indices: "end", or a number, but no
# arithmetic, such as `end-1`.
## This is for an iterable; rather than
## limit the type, we let non-iterables error.
if true # isa(vals, AbstractVector) || isa(vals, Tuple) # supports getindex(v, i)?
if idx == "end"
value′ = AnIndex(-1, vals[end])
else
ind = Base.parse(Int, idx)
value′ = AnIndex(ind, string(vals[ind]))
end
end
else
ind = Base.parse(Int, idx)
value′ = AnIndex(ind, string(vals[ind]))
end
#break
!global_lookup && break
end
end
end
Expand Down
14 changes: 10 additions & 4 deletions test/spec_sections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ tpl = """| This Is
"""

## "\r\n" should be considered a newline for standalone tags.
tpl = """|
{{#boolean}}
{{/boolean}}
tpl = """|
{{#boolean}}
{{/boolean}}
|"""

@test Mustache.render(tpl, Dict{Any,Any}("boolean"=>true)) == """|
@test Mustache.render(tpl, Dict{Any,Any}("boolean"=>true)) == """|
|"""

## Standalone tags should not require a newline to precede them.
Expand All @@ -203,6 +203,12 @@ tpl = """#{{#boolean}}
tpl = """|{{# boolean }}={{/ boolean }}|"""

@test Mustache.render(tpl, Dict{Any,Any}("boolean"=>true)) == """|=|"""


## Nested object fields should be accessible via global lookup
tpl = """|{{# inner }}{{ field.subfield }}{{/ inner }} {{# inner }}{{ ~field.subfield }}{{/ inner }}|"""

@test Mustache.render(tpl, Dict{Any,Any}("inner"=>Dict("field"=>Dict("subfield"=>1)),"field"=>Dict("subfield"=>2))) == """|1 2|"""
end


2 comments on commit a51d484

@jverzani
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/85474

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.17 -m "<description of version>" a51d484c1774fd0d24851b3a1a729bc22da33618
git push origin v1.0.17

Please sign in to comment.