{strip}:如何避免意外的空格删除?
{strip}
<div
class="x"
>
{/strip}
变成
<divclass="x">
这不是任何人想要的。
那么问题来了:有没有办法避免这种情况呢?想象的方法:
- 用空格替换新行,使用参数或其他 smarty 函数
- 添加未剥离/修剪的受保护空格
这个主题在他们的论坛上没有解决方案(除了 - 添加您自己的自定义标签)。另外,请不要提供原始 PHP 或任何其他语言/框架的解决方案。
{strip}
<div
class="x"
>
{/strip}
becomes
<divclass="x">
And that is not what anyone would want.
So, the question: is there any way to avod this? Imagined approaches:
- replace new lines by spaces, using parameters or other smarty-functions
- add protected spaces that are not stripped/trimed
This topic on their forum doesn't have a solution, (other than - add your own custom tag). Also, please don't offer solutions in raw PHP or any other languages / frameworks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以采用 @dev 的方法并捕获您的数据并通过strip修饰符运行它:
或者通过运行捕获的内容一个 < 一个href="http://www.smarty.net/docs/en/language.modifier.regex.replace.tpl" rel="nofollow">regex_replace 修饰符 (本质上与 split 相同,但更多开销):
或者添加一个名为trimwhitespace的新自定义块插件这使得使用输出过滤器修剪空白:
调用此文件block.trimwhitespace.php并将其放置在plugins_dir中。在模板中使用它:
虽然这两种修饰符方法对于简单的 HTML 内容都可以正常工作,但对于包含
或
You can either go with @dev's approach and capture your data and run it through the strip modifier:
or run the captured content through a regex_replace modifier (essentially doing the same as split, but with more overhead):
or drop in a new custom block plugin called trimwhitespace that makes use of the outputfilter trimwhitespace:
call this file block.trimwhitespace.php and place it in the plugins_dir. use it in your template:
While both modifier approaches would work fine for simple HTML stuff, they'd break for content including
<script>
or<pre>
tags. If you need those, you want to go with the wrapped outputfilter.If you want all your output to be run through that filter, forget altering your templates and add
$smarty->loadFilter('output', 'trimwhitespace');
to your setup.为了保护个人空间:
变得对我来说效果很好。
使用 smarty v3
To protect individual spaces:
becomes
works fine for me with smarty v3.
将您的代码分配给变量并尝试
{$articleTitle|strip:' '}
Assign your code to a variable and try
{$articleTitle|strip:' '}