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

"where" filter #154

Open
cdsaenz opened this issue Sep 26, 2021 · 0 comments
Open

"where" filter #154

cdsaenz opened this issue Sep 26, 2021 · 0 comments

Comments

@cdsaenz
Copy link

cdsaenz commented Sep 26, 2021

I saw in Jekyll (I'm writing a small PHP SSG inspired in it, thanks to this project) and other .liquid implementations that there's a filter called where useful to filter out an array of data. Apparently in a recent version, Jekyll also added it as a filter in a for iteration (but I didn't see it in Shopify's version).

I didn't see it in php-liquid, so here's my initial take on this filter. Tried it on a single level array, need to check it out for multidimensional cases.

Note: if available under another name in the project, please disregard. Thanks!

{% assign developers = team | where: "job","Developer" %}

  /**
   * Where filter
   */
   public function where($array,$field,$value) {

        $r = array_filter($array, function($v, $k) use ($field,$value) {
                if (is_array($v)) {
                    return (isset($v[$field]) && $v[$field] == $value);
                } else {
                    return true;
                }
            }, ARRAY_FILTER_USE_BOTH);

        return $r;
   }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants