Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable Name with Hyphen Misinterpreted in Conditional Statements #307

Open
sai-prasad-1 opened this issue Jul 28, 2024 · 10 comments
Open

Comments

@sai-prasad-1
Copy link

I am encountering an issue when trying to use a variable with a hyphen (-) in its name within a conditional statement in a markdown file. The variable loads and renders correctly using {{site.data.scam-banner}}, but when I attempt to use it within an if block or similar control structures, it throws an error. It appears that the hyphen in the variable name is being interpreted as a minus sign.

{% if site.data.messages.scam-banner %}
{{ site.data.messages.scam-banner }}
{% else %}
Error: 'messages.farewell' variable not found
{% endif %}

[error] stack trace is suppressed; run last Compile / run for the full output
[error] (Compile / run) liqp.exceptions.LiquidException: parser error "mismatched input '-' expecting {TagEnd, '.', NEq, '==', '>=', '>', '<=', '<', '[', '?', 'contains', 'and', 'or'}" on line 7, index 29

is this an bug or does liqp not support this type of variables.

@sai-prasad-1
Copy link
Author

@bkiers

@msangel
Copy link
Collaborator

msangel commented Jul 28, 2024

@sai-prasad-1 Hello!
Could you please provide sample code how you define this variable?

@msangel
Copy link
Collaborator

msangel commented Jul 28, 2024

According to Ruby syntax the is not valid part of identifier, so not wonder the error there as it looks like expression:
site.data.messages.scam -(minus) banner.

Are these variables works in original liquid templator?
image

@sai-prasad-1
Copy link
Author

I am using this package in Scala, and the variables are loaded from a YAML file as a HashMap and passed to the LIQP parser. I am able to access these variables using {{ }} in any template but not within any tags or blocks.

// Convert the dataMap to Java Map for LIQP usage
    val convertedDataMap = new util.HashMap[String, Any]()
    dataMap.foreach { case (k, v) => convertedDataMap.put(k, v) }
    innerMap.putAll(convertedDataMap)
    siteVariables.put("site", innerMap)
    val md = liqpParser.parse(rawMarkdown).render(siteVariables)
    ```
This is how the values are passed.

if you need anything else please let me know

@msangel
Copy link
Collaborator

msangel commented Jul 28, 2024

Glad to inform that right now the library behave exactly as liquid native implementation.
Its allow these as variable for assigning and printing BUT fails to use in if conditions.
So not seeing error here as behavior ported properly.

image

@msangel
Copy link
Collaborator

msangel commented Jul 28, 2024

That's was liquid... looks like liquid required the second argument even the documentation stayed it is not.... Weird...
image
image
So the error above because no second argument of condition.

anycase, site.data.messages.scam-banner is an expression. Expression that shouldn't be in if tag

@msangel
Copy link
Collaborator

msangel commented Jul 28, 2024

Ok. That expectation about second argument in example above came after failure correctly parse the if expression in original liquid/jekyll. as the minus in not allowed there either, so it thinks there more input.

SO the original library thinks variable with minus is not allowed in if conditions

image
image

@sai-prasad-1
Copy link
Author

so in short what is the way to go around and solve or overcome the issue like i am using the JEKYLL flavour of liqp and the same thing is working over there in jekyl, and also as you mentioned the block is not considering the value as null/nill so is this something that could be fixed over time

@msangel
Copy link
Collaborator

msangel commented Jul 31, 2024

@sai-prasad-1 I see no other workaround then not use variables with minus in their names. Don't believe original Liquid/Jekyll will allow them either as it is invalid ruby identifier as well.

@msangel
Copy link
Collaborator

msangel commented Jul 31, 2024

I tried to workaround this with assign/capture, but their syntax also not allows such names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants