reggi / shopify

Just a place to track issues and feature requests that I have for shopify
3 stars 1 forks source link

Ability to create custom liquid filters? #2

Open reggi opened 9 years ago

reggi commented 9 years ago

The liquid language allows you to create custom filters. However you can't do this in @Shopify. :cry:

reggi commented 9 years ago

This could even be a filter that creates another filter.

{{ "01" | snippet: "filter-month-number" }}

Uses snippets/filter-month-number.liquid and 01 would be passed into the file as the liquid variable filter-month-number just like with works. It could either capture all the output from the file and embed it in that variable or we could use some special variable name, like return or export.

{% capture month %}
{% include "filter-month-number" with "01" %}
{% endcapture %}
{{ month | strip }}

The alternative is something like this that outputs the content from the include {{}}. And you have to strip it every-time because there's a ton of spaces.

reggi commented 9 years ago

Ideally I would be able to pass parameters / arguments to the filter itself as well

I just had the problem of zero-padding a number. So I made this function-zero-pad.

{% assign return = false %}
{% if number and places %}
  {% assign number_string = number | append: "" %}
  {% assign missing_places = places | minus: number_string.size %}
  {% if missing_places > 0 %}
    {% for num in (1..missing_places) %}
      {% assign number_string = number | prepend: "0" %}
    {% endfor %}
  {% endif %}
  {% assign return = number_string %}
{% endif %}

And it works like this:

{% assign number = 3 %}
{% assign places = 2 %}
{% include "function-zero-pad" %}
{% assign number = return %}

Ideally It would be a filter like this:

{{ 3 | snippet: "zero-pad": 4 }} <!--prints 0003-->
reggi commented 9 years ago

This is really the same idea of having a variable outputted from and include.

{{ include "function-month-fallback" }}

If we could assign include to a variable

{% assign something = "function-month-fallback" | include %}

then we could chain it with other filters.

reggi commented 9 years ago

This could change code from this:

{% assign date = "2015-01-01" %}

{% assign article_prev_month_number = date | date: "%m" | minus: 1 %}

{{ article_prev_month_number | json }} <!-- 0 -->

{% assign month = article_prev_month_number %}
{% include "function-month-fallback" %}
{% assign article_prev_month_number = return %}

{{ article_prev_month_number | json }} <!-- 12 -->

{% assign number = article_prev_month_number %}
{% assign places = 2 %}
{% include "function-zero-pad" %}
{% assign article_prev_month_number = return %}

{{ article_prev_month_number | json }} <!-- "12" -->

To this:

{{ "2015-02-01" | date: "%m" | minus: 1 | snippet: "function-month-fallback" | snippet: "function-zero-pad": 2 }}
jimmykhlam commented 8 years ago

Hi I just came across your function-zero-pad snippet while doing a search and it helped me so thanks for that. For others who may come across this, it seems like there's a minor bug in the logic in the most inner loop:

{% assign number_string = number | prepend: "0" %}

should probably be

{% assign number_string = number_string | prepend: "0" %}