当前位置:网站首页>Smart curly bracket escape

Smart curly bracket escape

2022-06-09 04:35:00 Zero sky

Use smarty The template , It's usually used ‘{’ and ‘}’ As a delimiter (delimiter).

Sometimes , We need to be in html Output braces in the code , If you write it directly in the template , Will be smarty The parser for is considered a delimiter , Then it will report an error :

smarty error : syntax error: unrecognized tag Unrecognized tags !

How to solve it ? Yes 2 Kind of way :

1: Built-in variables

ldelim, rdelim ldelim and rdelim are used for displaying the literal delimiter, in our case “{” or “}”. The template engine always tries to interpret delimiters, so this is the way around that. ldelim and rdelim Used for output delimiters , That's curly braces ‘{’ and ‘}’. If you only output a few curly braces , Please use this method .

2: Text escape

We often html It's written inside javascript function , It is inevitable to write a lot of curly braces , At this time, the above solution is not applicable ,smarty Provides a tag to escape a piece of code :

{literal}…{/literal}

{literal}
    <script type="”text/javascript”">
    function sayHello() {alert(‘Hello World!’)}
    </script>
{/literal}

such , You can write all kinds of symbols in it at will , Don't worry about smarty The engine will parse incorrectly !

原网站

版权声明
本文为[Zero sky]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021700495974.html