prettier-plugin-twig-melody
prettier-plugin-twig-melody copied to clipboard
Error processing inline-if twig statement
Got this error while testing:
Error
[error] templates/shop/products/_item.twig: Error: ERROR: Invalid token
[error] 2 | <select name="purchasableId" id="purchasableId" class="purchasableId">
[error] 3 | {%- for purchasable in product.getVariants() -%}
[error] > 4 | <option {% if not purchasable.isAvailable %}disabled{% endif %}>
[error] | ^
[error] 5 | {{ purchasable.description }}
[error] 6 | {{
[error] 7 | purchasable.salePrice|commerceCurrency(
[error]
[error] A tag must consist of attributes or expressions. Twig Tags are not allowed.
[error] at TokenStream.error (/mytestproject/node_modules/melody-parser/lib/index.js:1359:22)
[error] at Parser.error (/mytestproject/node_modules/melody-parser/lib/index.js:830:21)
[error] at Parser.matchAttributes (/mytestproject/node_modules/melody-parser/lib/index.js:818:22)
[error] at Parser.matchElement (/mytestproject/node_modules/melody-parser/lib/index.js:726:14)
[error] at Parser.parse (/mytestproject/node_modules/melody-parser/lib/index.js:627:32)
[error] at Object.parse (/mytestproject/node_modules/melody-extension-core/lib/index.js:1187:36)
[error] at Parser.matchTag (/mytestproject/node_modules/melody-parser/lib/index.js:857:29)
[error] at Parser.parse (/mytestproject/node_modules/melody-parser/lib/index.js:610:32)
[error] at Parser.matchElement (/mytestproject/node_modules/melody-parser/lib/index.js:736:41)
[error] at Parser.parse (/mytestproject/node_modules/melody-parser/lib/index.js:627:32)
Source file
<select name="purchasableId" id="purchasableId" class="purchasableId">
{%- for purchasable in product.getVariants() -%}
<option {% if not purchasable.isAvailable %}disabled{% endif %}>
{{ purchasable.description }}
{{
purchasable.salePrice|commerceCurrency(
cart.currency
)
}}
</option>
{%- endfor -%}
</select>
.prettierrc
{
"tabWidth": 4,
"plugins": ["./node_modules/prettier-plugin-twig-melody"],
"twigMultiTags": [
"nav,endnav",
"switch,case,default,endswitch",
"ifchildren,endifchildren",
"cache,endcache"
]
}
Hi @rbrv, this is a limitation in the Melody parser (separate project). I cannot give an ETA for a fix, but issues like that can be worked around by doing something like
{%- for purchasable in product.getVariants() -%}
{% set optionDisabled = not purchasable.isAvailable ? 'disabled' : '' %}
<option {{ optionDisabled }}>
...
</option>
{%- endfor -%}
Does this help you for now?
I understand. We can change to code as you proposed. Thanks for the follow-up!
Ran into the same issue, confirming that the work around does its job for now.
Trying to implement it inside the PrestaShop repository for our twig files, since we've a bunch of stuff already done, we can't use a workaround like setting the var before :/
What would it take to fix this?
@twbartel Is there an issue open for this in the parser project? I looked for one and couldn't find it. Did I miss it or has no issue been opened yet?
@twbartel Do you have any update on this? This makes porting an existing code base over pretty annoying ...
Update The ternary operator works.
<li class="page-item {{ 'current' in page.class ? ' active' }}">