L
L
lumb2018-12-04 13:30:09
JavaScript
lumb, 2018-12-04 13:30:09

How to get variable from template in js?

Good day. If the script is connected directly in the template, it works fine.

function addLink(e) {
    e.preventDefault();
    var pagelink = ' <a href="' + document.location.href + '">{$_modx->resource.name | lower}</a> ';
бла бла и т.д.

I needed to transfer it to a file and after that it {$_modx->resource.name | lower}remains itself, instead of substituting the resource name in lower case.
How to make it {$_modx->resource.name | lower}work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Levchenko, 2018-12-04
@lumb

you can do something like this:
in the template
well, in js you get the value of the attribute

let resName = document.querySelector("[data-modx-resource-name]").dataset.modxResourceName;
console.log(resName);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question