猿问

PHP 是否具有相当于 ASP.NET 占位符的功能?

ASP.NET 具有 <asp:Placeholder> 功能。例如...


// Server side C# Code.

myph.Visible = flag;


<!-- Later in the HTML section. -->

<asp:Placeholder runat="server" ID="myph">

    HTML goes here.

</asp:Placeholder>

如果“Visible”属性设置为true,则将内部的HTML 发送到客户端。如果为 false,则删除 HTML。内容可能有更多的服务器端 <% %> 代码,其中也包含/删除取决于标志。


我正在将一个 ASP.NET 项目移植到 PHP。PHP 有没有办法标记大的 HTML 块(在内部使用小的 <?php echo ?> 块)并标记以在其他地方包含/删除它们?


我希望答案不是使用 - if (flag) echo "lots of html"


扬帆大鱼
浏览 181回答 1
1回答

qq_遁去的一_1

您可以使用一些模板引擎(Smarty、Twig 等)将 php 代码和 html 分开。但如果你想使用内联 php,只需使用<?php$block_visible = false;?>rest of your code here&nbsp;<?php&nbsp;if ( $block_visible ) {&nbsp;&nbsp; &nbsp;?>&nbsp; &nbsp; .... your code here ...&nbsp; &nbsp;<?php}&nbsp;?>or this one (to avoid curly braces)<?php if ($block_visible) : ?>&nbsp; Your html here<?php endif; ?>
随时随地看视频慕课网APP
我要回答