More than one conditions in {{#if}} statement in bigcommerce stencil - conditional-statements

I want to use 2 conditions in stencil theme (bigcommerce). How can I do so?
{{#if schema && product.brand.name '!=' ''}}
I have also used
{{#all schema product.brand.name '!=' ''}}
but did not get the desired result.
Also how can we check whether some variable or property is empty or not because following statement did not work for me:
{{#if product.brand.name '==' ''}}
Regards,

You should be able to use the {{#and}} helper to compare exactly two conditions.
{{#and schema product.brand.name}} There's both a schema and brand name! {{/and}}
This will check that both "schema" and "product.brand.name" evaluate to something truthy (meaning they're defined and they're not explicitly False or 0, etc)
Checking if a variable is empty is similarly just an if statement checking for it being defined:
{{#if customer}} There's a customer! {{/if}}
There's lots of good examples of these helpers being used in the Cornerstone repo, it might be helpful to search that repo for a few examples to get a feel for things.
Here's an example of the "and" helper: https://github.com/bigcommerce/cornerstone/blob/3350ea5c2a8cbb53819145bdcdda41dc6fef4f0c/templates/components/products/price-range.html#L1
Hope this helps!

Related

DBT dynamic config

I have a generic test and need it to be always saved under a particular name for the given table it is running on, e.g. on table report_revenue the generated generic test name will always be diff_check_report_revenue. Right now the default dbt naming behavior for generic tests is kinda messy (it sets the test name based on the test config, which is a great idea for most cases, but unfortunately not for my particular one).
According to the docs it looks like the [alias]https://docs.getdbt.com/reference/resource-configs/alias is exactly what I need. However, I also need to set the name dynamically based on the table that is tested. I know it can be set in the yml config by setting the field alias, but I hope there might be a more elegant solution.
When I try the following:
{{
config({
"severity": 'warn',
"tags": ["diff_check"],
"alias": 'diff_check_' + {{ model | replace("XXXXXXX") | trim }}
})
}}
It just doesn't work and dbt completely ignores the alias property. model is the relation on which the test is running. It's probably just my own wrong syntax, but I'm stuck and humbly asking for advice. Thanks a lot in advance!
The docs are super confusing on test config, since they group together generic tests and singular tests, and the behavior is different.
You can use a config() block inside the definition for a generic test to configure it, and some keys (e.g., severity) work fine, but alias is not one of them.
I think alias is meant for singular tests only. To give generic tests a unique identifier (only possible since v1.1), you are supposed to use the name property (not config). Docs. Does this make sense? No. Does it make it easy for you to do what you want to do? Also no.
I'll point out that the default naming convention for a generic test includes the name of the test followed by the name of the model, but assuming that isn't good enough, your only option will be to add a name property to every test, where you define the test in the properties (fka schema.yml) file. And it looks like the name property doesn't jinja-template its value (so you can't use jinja to populate the test name). So you'll have to do a lot of this:
models:
- name: my_model
tests:
- diff_check:
name: diff_check_my_model
You could fork dbt-core. The relevant code is here.

Using data build tool(dbt) and snowflake, how can I check if a column is a date field?

I'm a Junior so apologies if my explanation isn't that great.
I've created a macro on dbt to add a default row with defined values or default values based on data type.
What I'm trying to achieve is to check if the column is a datatype date field, then it will return the default variable {{ date_vi }} which I've defined as '1900-00-00', but I'm getting an error:
dbt.adapters.snowflake.column.SnowflakeColumn object' has no attribute 'isdate which tells me there is no is_date() which is confusing because is_date() works on snowflake normally.
I have now noticed on the dbt docs:
https://docs.getdbt.com/reference/dbt-classes#column
and the source code on github for snowflake:
https://github.com/dbt-labs/dbt-snowflake/blob/main/dbt/adapters/snowflake/column.py
That is_date() isn't actually available with the snowflake adapter, the code I was trying to get working was: {% elif col.is_date() %}{{ date_vl }} so I'm wondering what would be the be best way to check if a column is a date datatype? Hopefully I explained it enough as I'm still fairly new.
Cheers.
You can use https://github.com/calogica/dbt-expectations#expect_column_values_to_be_of_type
Or look at their implementation at https://github.com/calogica/dbt-expectations/blob/main/macros/schema_tests/column_values_basic/expect_column_values_to_be_in_type_list.sql for ideas of your custom implementation.

Get variables in Sphinx templates

I can't figure out how to get variables into Sphinx documents via a template. I am certainly making an elementary mistake, but while there is lots of documentation for using Jinja/Flask templates for web service and some documentation for Sphinx using it, I am having trouble doing the following. Maybe it's not possible and I'm barking up the wrong tree, but then this is fairly different from how variables work in general in web (as opposed to doc) templates?
I am working within a much larger project. Suppose in my project's conf.py I make a variable, say
LANGS = ['en', 'de', 'cn']
I know that this works because if I do the docbuild (we have a custom docbuild but I don't think it does anything really crazy other than a customized logger and eating a bunch of 'chatter') with the following line in conf.py
print len(LANGS)
it shows up during the docbuild.
But now of course I want to access this variable in my template. As far as I can tell, we override index.rst with templates/index.html, which inherits from the basic layout.html for Sphinx. And if I do
<p>We have {{ LANGS|len }} languages</p>
I get
We have 0 languages
Now, this is weird, because sometimes I can cause an error in the build by referring to variables not defined (though not consistently), so that somehow it 'knows' that the variable is defined but thinks it has length zero. Or does a "null" variable have length zero automatically?
How do I get this variable defined - or is it not possible?
What I want to do is then do something for each language in the list (make an outside link, in particular), but I figure there is no point in trying {% for %}/{% endfor %} or whatever if I can't get this working. Maybe Sphinx implements only a subset of Jinja?
Anyway, please help!
There are at least two ways to pass variables to a template:
Via html_context:
A dictionary of values to pass into the template engine’s context for all pages. Single values can also be put in this dictionary using the -A command-line option of sphinx-build.
Example:
# conf.py:
html_context = {'num_langs': len(LANGS)}
<!-- template: -->
<p>We have {{ num_langs }} languages</p>
Via the html_theme_options. This requires adding an option to theme.conf (you can create a theme by inheriting from a standard one):
[options]
num_langs = 1
Then you can set num_langs in conf.py via html_theme_options:
html_theme_options = {'num_langs': len(LANGS)}
and use it in a template:
<p>We have {{ theme_num_langs }} languages</p>

How to search by name attribute?

I'm trying to find an input element <input id="telephone" name="telephone">. I tried soup.find('input', name='telephone') and didn't find it. However soup.find('input', id='telephone') works fine. I think the problem is that 'name' has two meanings, the name of the tag and the name attribute. So, how can I search by the name attribute?
Obviously in my example I can search by the id attribute, but that's not there in my actual predicament.
You can also use the attrs parameter. For example:
soup.find('input', attrs={'name': 'telephone'})
or, more simply in your case:
soup.find('input', {'name': 'telephone'})
(See https://www.crummy.com/software/BeautifulSoup/bs4/doc/#the-keyword-arguments)
You don't have to pass the tag name ('input' in your case) - you can just search by attributes alone - but I've included it to match the details of your question.
Hope this is helpful.
Im not sure that you wrote.
soup.find('input', name='telephone');
Would you try to use this?
soup.find('input[name="telephone"]');
Hope it works.

count(*)> expression in xpath building

While reading an online blog i came across the below xpath,
xpath=//body/div[3]/form/fieldset/select[count(*)>1]
and the UI HTML looks like
What will be the xpath output? Is the Author tries to check whether the Select options are more than one ? (boolean answer)
This query will return all <select/> elements with more than one option to choose from. If so, the predicate is true and the <select/> element gets included, otherwise not.
Actually this is not quite correct, as it would fail to recognize <option/>s in <optgroup/>s:
<select>
<optgroup>
<option>foo</option>
<option>bar</option>
<option>batz</option>
</optgroup>
</select>
Which definitely has more than one option, but still only one direct child node. A better solution probably would be to use (I cut off the path in the beginning).
//select[count(.//*)>1]