question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error processing inline-if twig statement

See original GitHub issue

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"
    ]
}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:9
  • Comments:8

github_iconTop GitHub Comments

8reactions
twbartelcommented, Mar 16, 2020

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?

3reactions
rubascommented, Feb 11, 2021

Update The ternary operator works.

<li class="page-item {{ 'current' in page.class ? ' active' }}">

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I use php inline with a twig template? - Stack Overflow
I found the answer myself. I am using Laravel 5.1, with rcrowe/TwigBridge. I am able to do this: {{ app.request.is('admin/users') ?
Read more >
Twig for Template Designers - Documentation - Twig - The ...
This document describes the syntax and semantics of the template engine and will be most useful as reference to those creating Twig templates....
Read more >
Valid Twig syntax is incorrectly escaped in Views rewrites
I'd try not to name it after "Twig" or for the purpose of Twig if possible ... but the rewrite field is processed...
Read more >
Twig Operators | Branch CMS Documentation
A ternary operator is basically a condensed version of an if/else statement. Long form. If the variable exists, output it. Otherwise, output something...
Read more >
Twig: The Basics - SymfonyCasts
Welcome to the world of Twig! Twig is a templating language for PHP, which is a boring way of saying that it's a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found