问题描述
{strip}
变成
这不是任何人想要的.
那么,问题是:有没有办法避免这个?想象的方法:
- 使用参数或其他智能函数用空格替换新行
- 添加未剥离/修剪的受保护空间
这个话题在他们的论坛上没有解决方案,(除了 - 添加您自己的自定义标签).另外,请不要提供原始 PHP 或任何其他语言/框架的解决方案.
您可以采用@dev 的方法和 捕获您的数据并通过 条带修饰符:
{capture name="spaces"}
或通过 regex_replace 修饰符运行捕获的内容(本质上与拆分相同,但开销更大):
{$smarty.capture.spaces|regex_replace:"#\s+#":" "}
或放入一个名为 trimwhitespace 的新自定义块插件这利用了 outputfilter trimwhitespace:
调用这个文件 block.trimwhitespace.php 并将它放在 plugins_dir 中.在您的模板中使用它:
{trimwhitespace}
虽然这两种修饰符方法都适用于简单的 HTML 内容,但它们会破坏包含 或
标签的内容.如果你需要这些,你想使用包装好的输出过滤器.如果您希望所有输出都通过该过滤器,请忘记更改模板并将
$smarty->loadFilter('output', 'trimwhitespace');
添加到您的设置中.{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.
You can either go with @dev's approach and capture your data and run it through the strip modifier:
{capture name="spaces"}
<div
class="x"
> ... </div>
{/capture}
{$smarty.capture.spaces|strip:" "}
or run the captured content through a regex_replace modifier (essentially doing the same as split, but with more overhead):
{$smarty.capture.spaces|regex_replace:"#\s+#":" "}
or drop in a new custom block plugin called trimwhitespace that makes use of the outputfilter trimwhitespace:
<?php
function smarty_block_trimwhitespace($params, $content, Smarty_Internal_Template $template, &$repeat)
{
require_once SMARTY_PLUGINS_DIR . 'outputfilter.trimwhitespace.php';
return smarty_outputfilter_trimwhitespace($content, $template->smarty);
}
call this file block.trimwhitespace.php and place it in the plugins_dir. use it in your template:
{trimwhitespace}
<div
class="x"
> ... </div>
{/trimwhitespace}
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.
这篇关于{strip}:如何避免意外删除空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!