用outputbuffer分隔Smarty tpl's而不破坏结构


Delimit Smarty tpl's with outputbuffer without structural damage

我想分隔每个fetch ed tpl,以便之后用JavaScript识别它。
下面是我的代码:

if( ! function_exists('ate_output')){
    function ate_output($tpl_output, &$smarty){
        $tpl = $smarty->template_resource;
        $open = "<div class='ate-div' dta-tpl='$tpl'>";
        $close = "</div>";
        $tpl_output = $open . $tpl_output . $close;
        return $tpl_output;
    }
}

$smarty->registerFilter('output','ate_output');

是否有更好的方法来分隔tpl而不破坏原始布局的风险?

由于div是块元素,布局可能会中断,如果布局中断,您可以尝试.ate-div{display:inline;}

在这种情况下,你能做的最好的事情就是使用一些内联标签,就像有人在评论中提到的那样。它不应该破坏你的布局,但我认为你应该选择你不使用或几乎不使用这个网站的标签。否则,它可能会从CSS中获取一些样式,并且布局将被破坏。

所以你可能会使用一些标签:b, font, big, small, u假设你不在你的布局中使用它们,并且在CSS中设置它们不会影响你的布局。

例如b标签你可以在CSS中这样做:

b {font-weight: normal;}

或者您可以简单地为元素添加样式,因此您可以设置:

$open = "<b class='ate-div' dta-tpl='$tpl' style='font-weight: normal;'>";

您可以使用标记来记录。写入tpl_output。这将为您提供tpl名称和输出,以便在客户端javascript中使用。

if( ! function_exists('ate_output')){
    function ate_output($tpl_output, &$smarty){
        $tpl = json_encode($smarty->template_resource);
        $tpl_output = json_encode($tpl_output);
        return <<<EOT
<script type="text/javascript">
var tpl = {$tpl}; // use this however you'd like
var tpl_output = {$tpl_output};
document.write(tpl_output); // gross, but it works
</script>
EOT;
    }
}