Bind class to data object property in Vue.js and Slim - vue.js

I have the following slim template:
.dc-credit-card-label 'v-bind:class' => "{MyClass}"
span.dc-credit-card--stars ••••
span.dc-credit-card--stars ••••
span.dc-credit-card--stars ••••
span
| {{ payment.last4 }}
So I am trying to set the MyClass = 'some-cool-class'
but this doesn't work
Is there any chance to do this?

A comment from this git issue:
You just use the directive tags like normal but make them slim syntax.
.signed-in v-if="user"
= "Wecome, {{ user.name }}"
a.sign-out-button v-on:click="signOut" Sign out
So, use it "normal":
.dc-credit-card-label :class="{MyClass}"
span.dc-credit-card--stars ••••
span.dc-credit-card--stars ••••
span.dc-credit-card--stars ••••
span
| {{ payment.last4 }}

Related

How to remove comma from last items if name3 and name4 are not available (Laravel 8)?

#foreach($items as $item)
<p>Names:{{ $item->name1 }}, {{$item->name2 }}, {{$item->name3 }}, {{$item->name4 }}</p>
#endforeach
result: Names: Mimi, Adam, ,
You could use tenary statements if you want to keep your code lean:
#foreach($items as $item)
<p>Names:{{ $item->name1 }}, {{$item->name2 }}, {{$item->name3 }}{{$item->name3?', ':'' }}{{$item->name4 }}{{$item->name4?'.':'' }}</p>
#endforeach
Otherwise a simple if () else () conditioning would achieve the same effect.
I assume that $items is an eloquent model collection, you can simply use join to concatenate them:
#foreach($items as $item)
<p>Names:{{ join(', ', $item->only('name1', 'name2', 'name3')) }}</p>
#endforeach

Make a condition from array

I need your help, i tried many attempts without success, from my php controller i have following array :
$products = array(
'name' => 'shirt'
array(
'attribues' => array(
array("code" => "1", "label"=>"xs"),
array("code" => "2-", "label"=>"small"),
...
)
)
....
);
In product categories page, where i list all products i have following twig for an product item :
{% for product in products %}
<div>{{ product.name }}</div>
{% endfor %}
Here the aim it to able to filter by products attributes keys so i think something like this, the "product_tab" is a value from a tab where i click :
{% for product in products %}
<div v-if="[{{product.attributes|keys|join(',')}}].includes(selected_code)">{{ product.name }}</div>
{% endfor %}
The tab list :
<div data-tab="1" :click="show_products">xs</div>
<div data-tab="2-" :click="show_products">small</div>
// the vuejs part
const app = {
data() {
selected_code:"1"
},
methods:{
show_products:function(e){
this.selected_code = e.currentTarget.getAttribute("data-tab")
}
}
}
But i get error in console, vuejs as Uncaught SyntaxError: Unexpected token ',' and it brokes the page rendering. I know that the error is because of this line :
v-if="[{{product.attributes|keys|join('"','"')}}].includes(selected_code)"
But i don't know how to make this working, when i dump products.attributes|keys|join like below :
dump(product.attributes|keys|join('"','"'));
it outputs :
"["1-","18-total=0.5","81-"]"
You notice the double quotes around, I'm stumped and I can't find what to do to make this page work.

How do I check if collection contains an object with a specific key in handlebars

Assuming custom_fields contains this data, and I want to find out if it has an item/object with the name = "hide_options". I want to pass this to a component.
WARNING
Using occurrences in this way is a hack. If the name isn't unique enough, you may get false-positives
[
{"id":"12","name":"hide_options","value":"true"},
{"id":"13","name":"state","value":"colorado"},
{"id":"14","name":"city","value":"colorado, springs"}
]
The closest I've come up with is this:
templates\components\products\product-view.html
{{> components/products/conditionallyVisibile
hideOptions=(occurrences (join (pluck product.custom_fields "name") ",") "hide_options")
}}
components/products/conditionallyVisibile.html
<div>
hideOptions {{ hideOptions }}
</div>
Am I missing an easier Array or Collection helper that would make this easier? Most of the Array/Collection helpers are block helpers.
EDIT:
I was missing a significantly easier way to do this via the Filter Array Helper
{{#filter product.custom_fields "hide_options" property="name"}}
{{> components/products/conditionallyVisibile hideOptions=true }}
{{else}}
{{> components/products/conditionallyVisibile hideOptions=false }}
{{/filter}}
EDIT 2:
the scalar-form of
(occurrences (join (pluck product.custom_fields "name") ",") "hide_options")
is almost-equivalent to the block-form
{{#inArray (pluck product.custom_fields 'name') 'hide_options' }}

Shopify product.price.liquid Math Filters not displayed correct

I've added some code in the product.price.liquid to make a simple math formula:
<span id="extrainfo">
<span id="perkg">{{ variant.price | times: 100.0 | divided_by: variant.weight | money }}</span>/100g
</span>
Whole Code of the liquid here
Result: On the Product detail page, the result of the math formula is shown right=> Detail Page, but on the collections page or homepage, there comes "Inf"=> Collection Page
Any Ideas?
The last | money parameter might be confusing. Try like this
{% assign variant_price = variant.price | times: 100.0 %}
{% assign variant_price = variant_price | divided_by: variant.weight %}
{% assign variant_price | money %}
{{ variant_price }}

Liquid Warning: Unexpected character

To assign class depending on page category I have following in my code:
{% assign category_class = 'category-' | append: {{ page.category }} %}
As expected I get <div class="category-sometext". But when building I also get warning about an unexpected character in this line.
What's wrong and how I can fix it?
You need to remove the {{ }} around page.category as you are already inside {% %}. So:
{% assign category_class = 'category-' | append: page.category %}