Home > PHP Framework > ThinkPHP > body text

How to modify template tag delimiter in thinkphp

Release: 2020-05-22 09:10:42
forward
2631 people have browsed it

How to modify template tag delimiter in thinkphp

Everyone knows that in thinkphp, we assign variables to the template in the controller, and then obtain the variables through tags in the template. Suppose there is a variable is $name, then we should pass it in the template.

{$name} is used to get the variable value. The problem arises here. Suppose there are other JS and CSS in our template. For example, I have a piece of JS code in the template as follows. :

<script type="text/javascript">

Var str=6;

If(str>=6){

Alert(&#39;test&#39;);

}else{

Alert(&#39;&#39;);

}

</script>
Copy after login

If the template contains the above JS code, thinkphp will report an error when parsing the template, because the {} delimiter also appears in this JS, thinkphp will think that { } inside is also a template tag, and then it is parsed, but it is actually just a piece of JS code, so an error is reported.

How should we deal with this situation? Let's take a look at how to modify the delimiter in thinkphp:

In thinkphp, many operations to modify the system default things are implemented through the configuration file. Modifying the label delimiter is also done through the configuration file. It is specified in it. Let’s take a look at how to do it specifically. Add the following code in config.php:

<?php

return array(

&#39;TMPL_L_DELIM&#39;=>&#39;<{&#39;,

&#39;TMPL_R_DELIM&#39;=>&#39;}>&#39;,

);

?>
Copy after login

We can also see from the above code that the label delimiter has been changed to <{ }>, that is to say, if we assign a variable $this->assign('name',$name) in the controller now, we should write like this in the template: <{$name}> ;, so that it will not conflict with JS or CSS.

Recommended tutorial: "TP5"

The above is the detailed content of How to modify template tag delimiter in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51php
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template