Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Can't use variables as index in array #31

Open
schmoove opened this issue Apr 12, 2016 · 0 comments
Open

Can't use variables as index in array #31

schmoove opened this issue Apr 12, 2016 · 0 comments

Comments

@schmoove
Copy link
Contributor

With the original Liquid implementation, you are able to set an integer as a variable, and then use it inside bracket notation of an array/object to output the value of that key. This doesn't work in the PHP version:

Example:

{% assign letters = 'A,B,C,D,E' | split: ',' %}                                                                
{{ letters[1] }}

B

{% assign index = 1 %}
{{ letters[index] }}

blank

I've fixed this by modifying the "variable" method in the LiquidContext class:

    /**
     * Resolved the namespaced queries gracefully.
     *
     * @param string $key
     * @return mixed
     */
    public function variable($key)
    {
        /* Support [0] style array indicies */
        if (preg_match("|\[[0-9]+\]|", $key))
        {
            $key = preg_replace("|\[([0-9]+)\]|", ".$1", $key);
        }
        /* Support [var] style array indicies */ 
        elseif (preg_match("|\[(.*)\]|", $key, $matches))
        {
            $var = $this->fetch($matches[1]);
            $key = preg_replace("|\[(.*)\]|", "." . $var, $key);
        }
       .....
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

No branches or pull requests

1 participant