Template7: Can't use @global variable in js_if statements

It seems that I can’t use the @global variable in js_if statements. However, I can access the @global variable in regular js statements.

What’s really interesting is that when I evaluate my expression in the js statement, it returns ‘true.’

this.id: {{this.id}}
@global.user_id: {{@global.user.user_id}}
js @global.user.user_id: {{js "@global.user.user_id"}}
js result: {{js "@global.user.user_id == this.user_id"}}
{{#js_if "this.id == @global.user.user_id"}}
    it's true
    is not true

The above will print out:

this.id: 24
@global.user_id: 24
js @global.user.user_id: 24
js result: true
is not true

Is there another way I can access the @global variable in js_if statements?

Could be a bug, will check it. But if you don’t use any bundler and window.Template7 is defined then you can workaround it as:

{{#js_if "window.Template7.global.user.user_id === this.id"}}
    it's true
    is not true

basically replacing @global by window.Template7.global

oh DUHHH! That worked!!! Why didn’t I try that??? :stuck_out_tongue:



1 Like

How I can achieve @global on server side? Any attempts to check
{{#js_if "global.Template7.global.VarName === this.AnotherVar"}} fails.

Such error occurs:

error: execsync: TypeError: Cannot read property 'global' of undefined

at Object.eval (eval at js_if (/Users/almaz/Projects/web7/node_modules/template7/dist/template7.js:484:23), <anonymous>:1:38)

at Object.js_if (/Users/almaz/Projects/web7/node_modules/template7/dist/template7.js:484:34)

at Object.eval [as /body] (eval at compile (/Users/almaz/Projects/web7/node_modules/template7/dist/template7.js:600:20), <anonymous>:10:134)

Assign it to global right after require:

const Template7  = require('./path/to/template7.js');
global.Template7 = Template7;
1 Like

Спасибо Владимир!
Как сам не догадался установить данную переменную, наверное обленился, ждал, что за это сделает мне система)

1 Like