我有一些HTML,当条件满足时,我需要将其包装在锚标记中.我可以这样做:
<?php
$html = '<div>my html</div>';
if ($condition):
?>
<a href="http://google.com"><?php echo $html ?></a>
<?php else: ?>
<?php echo $html ?>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)
...但我不想将html包装在引号中以将其转换为字符串.这有两个原因:语法突出显示消失,而且难以维护.
我该如何做以下事情?
<!-- pseudo code -->
<?php if ($condition): echo $this->wrapWithLinkTo('http://google.com', ?>
<div>my html</div>
<?php ); endif; ?>
Run Code Online (Sandbox Code Playgroud)
......所以最终的结果是:
<!-- when $condition is true -->
<a href="http://google.com"><div>my html</div></a>
<!-- when $condition is false -->
<div>my html</div>
Run Code Online (Sandbox Code Playgroud)
输出缓冲怎么样?
<?php ob_start(); ?>
<div>my html</div>
<?php
$html = ob_get_clean();
echo ($condition) ? $this->wrapWithLinkTo('http://google.com',$html) : $html;
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
291 次 |
| 最近记录: |