Replacing values in a URL in dbt / Jinja - dbt

I'm trying to get something similar to this, but my design doesn't work:
{% set url_mask = {"{source}": "google.com", "{medium}": "paid_channel"} %}
select
url,
{% for key, value in url_mask.items() %}
{{ dbt.replace(url, {{ url_mask.keys() }}, {{ url_mask.values() }}) }}
{% endfor %}
END AS url_format
from
(select '{source}?other_get_patameters' as URL)
What am I doing wrong?
Thank you.

Related

Get last article - Shopify / Liquid

I'm trying to retrieve the most recent article on a blog. My current code below does not output anything.
{% for article in blogs['myblog'].articles.last %}
{{ article.title }}
{% endfor %}
You don't need a loop in order to access the last item.
{% assign article = blogs['myblog'].articles.last %}
This will set article to the last item. You can then use it as expected.
{{ article.title }}
Docs: https://shopify.dev/docs/themes/liquid/reference/filters/array-filters#last
It can be done like this using forloop.last:
{% for article in blogs['myblog'].articles %}
{% if forloop.last == true %}
{{ article.title }}
{% endif %}
{% endfor %}
This assumes that blogs is a variable. Otherwise, try replacing blogs['myblog'].articles with blog.articles.

Is it possible to add more values in a for in loop Liquid Shopify?

Here's my code
{% for filter_prefix in filter_prefixes %}
{{ filter_prefix }}
{% endfor %}
There are some values I want to add to the filter_prefixes, example: Type...
Can I do it like this?
{% for filter_prefix in filter_prefixes %}
{% filter_prefix = 'Type' %}
{{ filter_prefix }}
{% endfor %}
To list all the filter_prefixes and added the Type value as well.
Please help me with this.
Thank you!
Just output the value you want. There is no need to add it to the loop since it is just something you output anyway.
Type
{% for filter_prefix in filter_prefixes %}
{{ filter_prefix }}
{% endfor %}
Or use the Shopify Array filters split and join. So you join your existing array filter_prefixes into a string, append Type and then split that back into an array.

Twig For numeric

I would like to know how how to make a like java in twig
java
i=10;
for(int x=0;x<i;x++){}
I found this on twig:
{% for i in 0..10 %}
* {{ i }} {% endfor %}
But i don't know how to change that 10 to a int variable in twig.
Just place the variable in your for?
{% set i = 10 %}
{% for j in 1..i %}
{{ j }}
{% endfor %}

shopify pass a variable to settings

i want to do something like this in Shopify:
{% for i in (0..10) %}
{% capture slide %}slide{{i}}{% endcapture %}
{{ settings.slide }}//i need the value of this one
// i want to get the values for settings.slide1, settings.slide2 etc
{% endfor %}
Another example:
{% for i in (0..10) %}
{{ settings.slide[i] }}//i need the value of this one
{% endfor %}
This is a simplified version of what im trying to achieve.
Thanks
Try this:
{% for i in (0..10) %}
{% assign current_slide = 'slide' | append: i %}
{{ settings[current_slide] }}
{% endfor %}

Increment in volt showing error

{% set counter = 0 %}
{% for remain_todolist in remain_todolists %}
{% if counter == countRem_todolist %}
['{{ remain_todolist.Project_Name }}', {{ remain_todolist.Remaining_Todos }}]
{% else %}
['{{ remain_todolist.Project_Name }}', {{ remain_todolist.Remaining_Todos }}],
{% endif %}~
{% do counter++ %}//Showing Error
{% endfor %}
Volt Increment statement is showing error
"Unknown expression 279"
What i am doing wrong ?
Have you tried without the do keyword?
Anyway, Volt loops already have counters available to use (see Loop Context), here's a version using it:
{% for list in remain_todolists %}
['{{ list.Project_Name }}', {{ list.Remaining_Todos }}]{{ loop.last ? '' : ',' }}
{% endfor %}
As #cvsguimaraes suggest you can use Loop context which should be better in your case.
I don't think that do statement is valid in Volt. If you need counter value outside loop you can increment value just like that:
{% set counter = counter + 1 %}