I would like to split my product titles, e.g.:
"big hat - red"
"small coat - blue"
"medium socks - green"
so that it only outputs the text before the "-". In other words, I want some kind of dynamic split (or a 'split and delete the remainder' operator/function) that outputs
"big hat"
"small coat"
"medium socks"
Using the below doesn't remove the text after the hyphen, and I can't simply hard-code the ending part to be removed, because the text after the hypen varies with each product.
{{ product.title | split:"-" }}
How would I achieve this?
Use split filter and then first filter to get first array element.
{% assign source_string = "before text - after text"%}
{{ source_string | split: "-" | first }}
For your code, it will be
{{ product.title | split:"-" | first }}
Related
I have a field called TEXT1 which contains the following text below in quotes. Is there a way where I can flag for instances where the word orange is being mentioned AFTER the Excluding This portion of the text?
I've tried doing where TEXT1 is not like %orange% but that would get rid of instances where the Including This portion of the text contains the word orange and the Excluding This portion didn't contain the word orange. Basically I need help with a function that will flag for when the word orange is mentioned after the phrase Excluding This within the TEXT1 field in my table.
"Including This
The best fruit is an orange
Excluding This
The worst fruit is an orange
"
Sounds like a job for full text search:
WHERE to_tsvector('english', textcol) ##
to_tsquery('english', 'The <-> best <-> fruit <-> is <-> an <-> orange & ! (The <-> worst <-> fruit <-> is <-> an <-> orange)')
I am trying to create a filter on top of a collection page with all the available variants. But, I need them to be formatted nicely so I'm using the following code:
{% for product in collections.all.products limit: limit %}
<li>
{% assign sizes = product.variants | map: 'option1' | uniq %}
{% assign colors = product.variants | map: 'option2' | uniq %}
{% assign combined_variants = sizes | concat: colors | uniq %}
{% for v-item in combined_variants %}
{{ v-item }} <br/>
{% endfor %}
</li>
{% endfor %}
{{ combined_variants | json }}
The problem is that I need it to iterate through all the products but only print unique values across all of them. How can I achieve this?
You would be better off creating custom collections for each option. Put all Size X in collection Size X. Put all Yellow Budgies in collection Yellow Budgies. Then you can craft a navigation scheme that takes customers to the products that fit their selection. All things Size X, or all things Yellow Budgie.
Building out a different filter could also be the JSON approach using the Mega Menus like Bacon etc. They deliver a payload to your them that can help customers drill to what they want. You have no easy way of doing this yourself with the code you are presenting.
I currenty have data in my CMS which outputs this on the front end:
2017-03-16T00:00:00
What I like this to do is check the above date and see how many days is left to that day and display it as:
7 days left
(assuming today is the 9th March 2017)
And then when the date reaches 0, it needs to display the text: "Closed"
Currently I have:
{% assign todaysdate = {{todaysdate]}} %}
{% assign todaysdatenew = todaysdate | convert: "date" %}
{% assign formula = {{globals.site.dateNow}} | minus: todaysdatenew %}
{% if {{globals.site.dateNow}} > todaysdatenew %}
Closed
{% else %}
{{formula | date:"%d' days'"}}
{% endif %}
However for some reason it is displaying the result as 6 days left, instead of 7 days left. How do I add 1 extra day?
If it is always one day then you can change just hardcode add the extra day by editing the line below:
{{formula | date:"%d' days'"}}
To:
{% assign formulaResult = {{formula | date:"%d' days'"}} -%}
{{formulaResult | date_add: 1, "day"}}
How can I pad a sentence with white space so that it is printed in a 'block'.
I want to print a receipt. for a given item I want to print the quantity, item name, and price.
12 x Example short name £2.00
1 x This is an example of
a long name £10.00
The 'block' to which I refer to is shown below.
12 x |Example short name | £2.00
1 x |This is an example of |
|a long name | £10.00
Using the example above I can easily handle the formatting of the quantity and the price. Using string formatting. Then for the item name the best method to use, I think, is to split the item name in code, and use the wrap functionality of StringTemplate, but I don't know how to pad the remainder of the line with whitespace
I am using .Net and StringTemplate 4. Here is a simplified version of the template I have. Assuming I have an item with the properties Quantity, ItemName (split into an array of strings), and Price.
<item.Quantity; format="{0,4}"> x <item.ItemName; wrap, separator=" ", anchor><item.Price; format="£{0,8}">
Now at the moment the only way I can think to get it to work is to calculate the white space in code and add it to the item name array.
So is there a neat way to do it in StringTemplate?
Is it possible to define a buildout variable of type dictionary?
I am trying to substitute a variable with a dictionary but buildout considers it a string.
E.g.
in buildout.cfg:
[MYPROG]
progr_args =
a : 1
b : 2
d :
d1: 1
d2: 2
in template:
my_params:
{% for key, val in parts.MYPROG.progr_args.items() %}\
${key}: ${val}\
{% end %}
Not with Buildout itself, no. Buildout configuration values are always strings; even the mr.scripty recipe, which lets you use Python code as part of your buildout configuration, stores the results of the Python code a strings.
Ever worse, initial whitespace from continuation lines is stripped, so your entry:
progr_args =
a : 1
b : 2
d :
d1: 1
d2: 2
is stored as \na : 1\nb : 2\nd :\nd1: 1\nd2: 2, having lost all indentation context.
You'll have to use Genshi itself to parse out your values. I suggest you use a separate section:
[MYPROG]
prog_params = section_name
[section_name]
a = 1
b = 2
and in your template:
my_params:
{% for key, val in parts[parts.MYPROG.progr_args].items() %}\
${key}: ${val}\
{% end %}