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

String literal with single-quotes causes error "Variable not found" #36

Closed
feelosophy13 opened this issue Nov 9, 2023 · 2 comments
Closed

Comments

@feelosophy13
Copy link

feelosophy13 commented Nov 9, 2023

Variable comparison inside a template throws an error. Below is a reproducible example.

R code

library(jinjar)

render(
  fs::path("template.txt"),
  variable = 'test'
)

Template file template.txt

{# this works #}
{% if 1 == 1 %}
  {{ variable }}
{% endif %}

{# this does NOT work #}
{% if variable == 'test' %}
  {{ variable }}
{% endif %}

Error

Error in `render()`:
! Problem encountered while rendering template.
Caused by error:
! Variable 'test' not found.
ℹ Error occurred on line 7 and column 20.
Run `rlang::last_trace()` to see where the error occurred.

Session information

R version 4.3.1 (2023-06-16)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.5

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Los_Angeles
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] jinjar_0.3.1   jsonlite_1.8.7

loaded via a namespace (and not attached):
 [1] utf8_1.2.4        R6_2.5.1          xfun_0.41         tidyselect_1.2.0  magrittr_2.0.3    glue_1.6.2        tibble_3.2.1      knitr_1.45       
 [9] pkgconfig_2.0.3   dplyr_1.1.3       generics_0.1.3    lifecycle_1.0.3   cli_3.6.1         fansi_1.0.5       vctrs_0.6.4       DBI_1.1.3        
[17] compiler_4.3.1    rstudioapi_0.15.0 tools_4.3.1       pillar_1.9.0      fs_1.6.3          rlang_1.1.1      
@davidchall
Copy link
Owner

This error is caused by the right-hand side of the comparison. The template syntax only supports string literals specified using double-quotes (not single-quotes). This is an unfortunate consequence of the underlying usage of JSON objects.

In your example, you should use "test" instead of 'test'. Here's a quick demo:

library(jinjar)

render("{{ x == 'test' }}", x = "test")
#> Error in `render()` at jinjar/R/render.R:44:2:
#> ! Problem encountered while rendering template.
#> Caused by error:
#> ! Variable 'test' not found.
#> ℹ Error occurred on line 1 and column 10.

render('{{ x == "test" }}', x = "test")
#> [1] "true"

Created on 2023-11-09 with reprex v2.0.2

@davidchall davidchall changed the title Variable comparison inside template throws "Variable not found" error String literal with single-quotes throws "Variable not found" error Nov 10, 2023
@davidchall davidchall mentioned this issue Nov 10, 2023
@davidchall davidchall changed the title String literal with single-quotes throws "Variable not found" error String literal with single-quotes causes error "Variable not found" Nov 10, 2023
@feelosophy13
Copy link
Author

Thank you! Great package, by the way! :D

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