Home  >  Article  >  Backend Development  >  javascript - twig template rendering mechanism. Can jquery be used in twig?

javascript - twig template rendering mechanism. Can jquery be used in twig?

WBOY
WBOYOriginal
2016-12-05 13:44:241584browse

1: Encountered a problem when using twig

javascript - twig template rendering mechanism. Can jquery be used in twig?
defines a template page, and then the child pages inherit it.

javascript - twig template rendering mechanism. Can jquery be used in twig?

You cannot write any code outside the {%%} tag, otherwise an error will be reported. As shown in the picture above, I wrote a piece of code in the tag, and the browser reported an error, as shown below:

javascript - twig template rendering mechanism. Can jquery be used in twig?

My jquery is introduced in the template page. When I put this code separately in a js file and introduce it in the template page, there is no problem.
I would like to ask here, what is the twig rendering mechanism? Why can’t I use $ here?
Thank you

I just took a look. After adding jquery directly to the sub-page, the sub-page can indeed be used. $ can be used.
If some public resources are quoted, this will happen when placed in the template page. How to ensure the publicity of these resources?

Reply content:

1: Encountered a problem when using twig

javascript - twig template rendering mechanism. Can jquery be used in twig?
defines a template page, and then the child pages inherit it.

javascript - twig template rendering mechanism. Can jquery be used in twig?

You cannot write any code outside the {%%} tag, otherwise an error will be reported. As shown in the picture above, I wrote a piece of code in the tag, and the browser reported an error, as shown below:

javascript - twig template rendering mechanism. Can jquery be used in twig?

My jquery is introduced in the template page. When I put this code separately in a js file and introduce it in the template page, there is no problem.
I would like to ask here, what is the twig rendering mechanism? Why can’t I use $ here?
Thank you

I just took a look. After adding jquery directly to the sub-page, the sub-page can indeed be used. $ can be used.
If some public resources are quoted, this will happen when placed in the template page. How to ensure the publicity of these resources?

When your page executes js
jq has not been introduced yet

Nothing to do with Twig. It's because your jquery hasn't been loaded yet, so the code referencing $ is executed.
Try to put the code in

<code class="js">document.onload = function() {
   // 以下是你的代码
}</code>

Middle.
Using window.onload also works.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn