Can I pass a variable from a Jekyll template to an include then render data with that variable? - ruby-on-rails

In foo.html (a post), I have the following:
{% assign table_name="application" %}
{% include table.html %}
This assignment appears to work fine in table.html
{% assign tableName = table_name %}
<p>table name is: {{ tableName }}</p> # renders "table name is: application"
Now, I'm trying to sort through an array of data that I have defined in config.yml by doing the following:
{% for header in site.table.tableName.headers %}
<th>{{ header }}</th>
{% endfor %}
This gives me no output whatsoever.
If I change the for statement to include the variable's content, not the variable, it works fine.
{% for header in site.table.application.headers %}
This leads me to believe that it's not a problem with my array but that it's either a shortcoming of Jekyll, a bug in Jekyll, or I'm not accurately constructing my statements.
Any idea how I could make this work?

Looks like this can be done. I had to think about it more programmatically. What seems to be happening is that Jekyll was expecting an object and I was feeding it a string.
{% assign tableName = "aStringName" %}
{% include table.html %}
so,
# in _includes/table.html
{% for header in site.table.tableName.headers %}
was being interpreted as
{% for header in site.table."aStringName".headers %}
When I switched to bracket notation for the object, it was perfect.
Final result:
{% for header in site.table[tableName].headers %}
or, as Jekyll sees it,
{% for header in site.table['aStringName'].headers %}

Related

Snowflake stored procedure fails from dbt

I have a problem to execute a stored procedure in Snowflake by dbt:
The description of my procedure is like: MyStoredProcedure(ARRAY, VARCHAR, VARCHAR)
So, when I want to run it, I use array_construct function to create the first argument, for example:
call MyStoredProcedure(array_construct(array_construct('str_1', 'str_2')), 'schema_name', 'table_name');
This works when I run it in Snowflake. However, when I run this from dbt it fails with this error:
Modifying a transaction that has started at a different scope is not allowed.
Which I am sure that it is something related to invoking array_construct in this call.
I should mention that to run this from dbt I have defined a macro like this:
{% macro MyStoredProcedure() %}
{% set query -%}
CALL MyStoredProcedure(
array_construct(array_construct('str_1', 'str_2')),
'schema_name',
'table_name');
{%- endset %}
{% do run_query(query) %}
{% endmacro %}
And run it of course like: dbt run-operation MyStoredProcedure
I appreciate any tip or idea to help me with this problem.
Thanks
I'm still very much learning about snowflake but from reading this section
of the docs, it appears to me that this could just be a mechanic of your multi-scope procedure call.
From what I can see, the typical (non-dbt) way of doing this in snowflake would be:
begin transaction;
insert 1;
call storedprocedure();
insert 2;
commit;
Maybe you can adjust your macro something like this which will overcome the scoping nesting issue since everything will be completed in one transaction?
{% macro MyStoredProcedure() %}
{% set query -%}
begin transaction;
CALL MyStoredProcedure(
array_construct(array_construct('str_1', 'str_2')),
'schema_name',
'table_name');
commit;
{%- endset %}
{% do run_query(query) %}
{% endmacro %}
Unable to test this since I can't reproduce the procedure itself.
Eventually I made it run as follow:
{% macro MyStoredProcedure() %}
{% set query -%}
CALL MyStoredProcedure(
array_construct(array_construct('str_1', 'str_2')),
'schema_name',
'table_name');
commit;
{%- endset %}
{% do run_query(query) %}
{% endmacro %}

Get all tags used by a Shopify shop in one request

When I try this URL in my browser:
https://USERNAME:PASSWORD#SHOP.myshopify.com/admin/products/tags.json
It gives me a nice list of all the tags in my shop.
I need this list, but when I try an API call with Python it says 'Not found'. Since I can do it with my browser, there must be a way to do it programmatically.
Getting all the tags by looping over all products is too long (+- 5-10 seconds), whereas this gives me everything I need instantly.
How can I make this request?
Thanks
Currently, Shopify Rest API still doesn't have an endpoint to get all tags, vendors, types. And GraphQL Admin API can only get at maximum first 250 results.
You can get all of them through Shopify liquid ( Not working with the passworded store ).
Create a new search template name: search.foo.liquid
{% layout none %}
{
"vendors" : {{shop.vendors|json}},
"types" : {{shop.types|json}},
"tags" : {% capture output %}
{% for collection in collections %}
{% if forloop.index == 1 %}""{% endif %}
{% if collection.all_tags.size > 0 %}
{% for tag in collection.all_tags %}
{% if output contains tag %}
{% else %}
,"{{tag}}"
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endcapture %}
{{ output | strip_newlines | prepend: '[' | append: ']' }}
}
Access: https://your-store.myshopify.com/search.json?view=foo in browser to view the result, Use cURL to get it in your application.
The tags aren't exposed via the REST API, but they're accessible via the GraphQL Admin API:
{
shop{
productTags(first: 100){
edges{
node
}
}
}
}
Check this for extra info

How to load index from for loop inside widget to use it in if condition

I have widget load pieces and I set for loop to load single item inside this widget and I want inside for loop to set index for each element to take advantage from it to make if condition, How I can do that?
here is my for loop inside widget:
{% for piece in data.widget._pieces %}
and I tried to set index like this but it didn't work:
{% set index = data.piece.__dotPath | replace(".", "-") %}
Using nunjucks's built-in loop object, you can conditionally check what index your loop is at and choose to do something special like
{% for piece in data.widget._pieces %}
{% if loop.index === 1 %}
... something special
{% else %}
... something normal
{% endif %}
{% endfor %}

How to get a SubString of variable using stencil templating tool for swift

I'm using the stencil template language tool for swift found here:
https://github.com/stencilproject/Stencil
Using master branch.
The Problem
given the following .json file
{
"xcassets" : "dev Sources test1"
}
I want to be able to retrieve the first word delimited by " " which is "dev".
What I've Tried
The latest version of Stencil has the Split Function. But the problem is I can't figure out how to access the first element in the resulting array, and it's not in the documentation either.
I've tried the following in the stencil file:
{{xcassets|split:" "[0]}}
{{{{xcassets|split:" "}}[0]}}
{{xcassets|split:" ".first}}
{{xcassets|split:" "}}.first}}
{{xcassets|split:" "|[0]}}
{{xcassets|split:" "|.first}}
{{xcassets|split:" "|first}}
None of which worked.
What I'm trying to avoid
I know I can do it this way, but there must be a better way.
{% for element in xcassets|split:" " %}
{% if forloop.first %}
{{ element }}
{% endif %}
{% endfor %}
Anyone have suggestions for a better tool?
I guess this is the only way:
{% for element in xcassets|split:" " %}
{% if forloop.first %}
{{ element }}
{% endif %}
{% endfor %}

Django, Display thumbs in Admin?

how ill show in the admin section the user picture?, i was looking the template of the admin and i can't see how...
i think that Django show the fields in the section code (change_list template), but jeje ..how? jeje
{% block result_list %}
{% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}
{% result_list cl %}
{% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}
{% endblock %}
Thanks guys :)
We use Justin Driscoll's fabulous django-photologue.
If photologue is installed properly (recommend running python manage.py plinit as part of the installation) and the media is being served correctly the following should provide you with an easy-to-use admin_thumbnail() as follows:
models.py
from photologue.models import Photo
class Entry(models.Model):
entry_photo = models.ForeignKey(Photo)
...
admin.py
def show_entry_thumbnail(item):
return item.entry_photo.admin_thumbnail()
show_entry_thumbnail.short_description = _('Entry Photo')
class ItemAdmin(admin.ModelAdmin):
list_display = (show_entry_thumbnail, ... )
There also appears to be an alternative method using sorl-thumbnail: it's mentioned here.

Resources