How do I make my own array?

6,411 views
Skip to first unread message

Lindsay Ward

unread,
Oct 3, 2009, 6:24:50 AM10/3/09
to Liquid Templates
Hi there,
I am using Liquid with Planning Center Online (http://
www.planningcenteronline.com/) to make a custom report. I have some
variables provided to me, but I can't figure out how to just use
these, and I need my own array.
I can not see any syntax in the "Liquid for Designers" to show a user-
created array. I have tried lots of things, including the following,
none of which work:

{% assign array = 1,2,3,4,5,6,7 %}
{% assign positions = ('Leader', 'Sound', 'Words') %}
{% assign positions = ['Leader', 'Sound', 'Words'] %}
{% assign positions = {Leader', 'Sound', 'Words'} %}
I even tried the one in the documentation example (but it's a comment
in the example):
# array = [1,2,3,4,5,6]
{% for item in array limit:2 offset:2 %}
... so I tried
{% assign array = [1,2,3,4,5,6] %}
{% for item in array limit:2 offset:2 %}

Could someone please let me know if and how I do this - and perhaps
this sort of thing could be added to the basic page. It would be great
if each example was stand-alone; like the one above could be copied
and would work, not assuming some variable 'array' already exists.

Thank you!

Lindsay Ward

unread,
Oct 7, 2009, 4:08:37 AM10/7/09
to Liquid Templates
Bump...
Is it possible to make my own array? It would really help!
Thanks.

Jônatas Davi Paganini

unread,
Oct 7, 2009, 7:46:13 AM10/7/09
to liquid-t...@googlegroups.com
 try using drops!
                                                                                                                                         
  1 require 'rubygems'
  2 require 'liquid'
  3 class MyVarsDrop < Liquid::Drop
  4   def array
  5     [1,2,3,4,5,6,7]
  6   end
  7   def positions
  8     ['Leader', 'Sound', 'Words']
  9   end
 10 end
 11 puts Liquid::Template.parse("
 12 {% for item in my.array limit:2 offset:2 %}
 13     {{ item }}
 14 {% endfor %} ").render('my' => MyVarsDrop.new)

jonatas@xonatax-mac:~/projetos/ruby$ ruby example_liquid_variable.rb


    3

    4
 
jonatas@xonatax-mac:~/projetos/ruby$

~                                                                                                                                                                                     
~                                                         
--
Att.

Jônatas Davi Paganini

P r o g r a m a d o r

Leosoft Cia de Informática

+55 46 3520-1800  / +55 46 3520-1818 (fax)

www.leosoft.com.br  - www.leosoft.com.br/coopnet

Lindsay Ward

unread,
Oct 7, 2009, 11:10:35 PM10/7/09
to Liquid Templates
Thanks for the response!

I'm not sure that I have access to this, however. I am an end user
making a report using HTML, Liquid and the template variables I've
been provided with.
I can not customise the code at the server end.

Stefan Schüßler

unread,
Nov 24, 2009, 11:26:38 AM11/24/09
to Liquid Templates
Lindsay,

you cannot declare or alter arrays using Liquid's core functions.

However, what are you trying to achieve? Maybe there's a workaround...

Cheers

Stefan

Tobias Lütke

unread,
Nov 30, 2009, 11:15:32 PM11/30/09
to liquid-t...@googlegroups.com
Liquid can't do this currently but i like the syntax. This would be a
nice addition.
Regards
-- tobi
> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google Groups "Liquid Templates" group.
> To post to this group, send email to liquid-t...@googlegroups.com
> To unsubscribe from this group, send email to liquid-templat...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/liquid-templates?hl=en
> -~----------~----~----~----~------~----~------~--~---
>
>

Tarwin Stroh-Spijer

unread,
Oct 23, 2012, 3:33:34 AM10/23/12
to liquid-t...@googlegroups.com
I'm simply using the "split" method. For example ...

{% assign myArray = "one|two|three" | split: "|" %}

Kristian Brito

unread,
Aug 14, 2015, 1:09:36 PM8/14/15
to Liquid Templates
I had an instance where I had to define an array or declare an array in liquid and this is what I cam up with that worked:

Using the split filter
{% assign my_array = "" | split: ""  %}

Then to add to my array, I would simply overwrite my array with my_array plus the new item I push to it:

{% assign my_array = my_array | push: "array_item" %}

I hope this helps someone.
Reply all
Reply to author
Forward
0 new messages