Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Custom tag accessing an index of an array for 4.0.0-tau.1 #77

Open
apps4everyone opened this issue Nov 8, 2020 · 1 comment
Open

Custom tag accessing an index of an array for 4.0.0-tau.1 #77

apps4everyone opened this issue Nov 8, 2020 · 1 comment

Comments

@apps4everyone
Copy link

Hey, just looking for a possibility to use two variables together to access an index of an array(Strings) like this:

Example:

#for(index in intValue5):
    #(colors)[#(index)]
#endfor

Swift example:

let index: Int = 2
let colours: [String] = ["#000000", "#0c02ff", "#008000"]
let colour: String = colours[index]

I tried a custom function #ValueFromArray(#(colors), #(index)) but got:
No exact match for function ValueFromArray(_ : variable($:index)); 1 possible matches: ValueFromArray(_: Array, _: Int)"}

public struct ValueFromArray: LeafFunction, StringReturn, Invariant {
    public static var callSignature: [LeafCallParameter] { [.array, .int] }
    public func evaluate(_ params: LeafCallValues) -> LeafData {
        guard let array = params[0].array else {
            return .error("`ValueFromArray` must be called with an array parameter.")
        }
        guard let index = params[1].int else {
            return .error("`ValueFromArray` must be called with an int parameter.")
        }
        return array[index]
    }
}

Thx for your Help, great work for the 4.0.0-tau.1 release, only documentation is hard (to find and also to create i know).

@apps4everyone
Copy link
Author

was looking over the leaf-kit source code and found that count would be something to find a hint how to solve this but there is a deactivated test case with a todo:
// TODO: Reimplement #count

func _testCount() throws {

if i activate the test it fails ;-(

func testCount() throws {

@0xTim 0xTim transferred this issue from vapor/leaf Nov 9, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant