Code Monkey home page Code Monkey logo

shopify-liquid-functions's Introduction

Shopify Liquid Functions

I hate repeating myself in shopify liquid, so to abide by the Don't repeat yourself (DRY) principle of software development, I created a spec for creating reusable functions in Shopify Liquid.

I've been using a technique for a little while now to create functions in shopify. With it's own sort of spec. I've been making them more and more, this repo will be dedicated to housing them.

Example Function

Here's an example of a function that will check if a string (string) contains another string (query).

This would be in a file called function.string_contains.liquid.

{% assign return = false %}
{% if query %}
{% if string %}
  {% assign replaced = string | remove: query %}  
  {% if string != replaced %}
    {% assign return = true %}
  {% endif %}
{% endif %}
{% endif %}

Things to note:

  • return is the return value of the function
  • each argument started with pass_ (depricated?)
  • return is assigned to false at the beginning of each funciton file
  • the value of return changes somewhere in the funciton
  • there is no support for returning multiple variables
  • wrap the whole function in if statment per outside argument.
  • up for debate should set any assigned variables to null at the end of a function {% assign replaced = null %?

Example Usage

{% assign query = '5"x7"' %}
{% assign string = 'hello world 5"x7"' %}
{% include "function.string_contains" %}
{% assign contains_query = return %}

Things to note:

  • assign the arguments
  • include the function file
  • re-assign the return value

shopify-liquid-functions's People

Contributors

reggi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.