[英]Show PHP/HTML as text on page in php template
如何在我的php模板实际上不尝试读取php代码的情况下,在“ pre”和“ code”标签内的页面上将以下代码作为文本吐出。 使用js和CSS,“ code”和“ pre”标签会阻止实际代码运行。 如何使用以下HTML / PHP组合执行此操作。
我尝试使用htmlescaping,但这只会吐出php。 我不知道这里是正确的组合。
<pre>
<code>
<?php elseif(get_row_layout() == 'three_col_panel'): ?>
<!-- ==================================== -->
<!-- === 3 COLUMN ICON TEXT GRID ======== -->
<!-- ==================================== -->
<div class="icon-panel-three block">
<div class="main-wrap">
<div class="container">
<div class="row">
<?php if( have_rows('pillar')): ?>
<?php while ( have_rows('pillar')) : the_row() ?>
<div class="col-md-4 icon-column">
<div class="inner-content">
<?php $image = get_sub_field('image');
$url = $image['url'];
$alt = $image['alt'];
$size = 'medium';
$thumb = $image['sizes'][$size];
?>
<a href="<?= get_sub_field('image_link'); ?>"><img src="<?= $thumb; ?>" alt="<?= $alt; ?>"/></a>
<div class="text">
<h1><a href="<?= get_sub_field('image_link'); ?>"><?= get_sub_field('heading')?></a></h1>
<p><?= get_sub_field('paragraph')?></p>
</div><!--end text-->
<?php if( have_rows('column_link')): ?>
<?php while ( have_rows('column_link')) : the_row() ?>
<a href="<?= get_sub_field('col_link_url'); ?>" class="body-button"><?= get_sub_field('col_link_text'); ?><img src="<?= get_template_directory_uri(); ?>/img/button-arrow.svg" alt="click here" /></a>
<?php endwhile; ?>
<?php endif; ?>
</div><!--end inner-content-->
</div><!--end col-->
<?php endwhile; ?>
<?php endif; ?>
</div><!--end row-->
</div><!--end container-->
</div><!--end main-wrap-->
</div><!--end tile-panel-->
</code>
</pre>
要输出的文本必须在变量字符串中或在单独的文件中。
我建议您使用文件解决方案:它更简单,代码更清晰,并且您可以使用一个文件显示一个以上的代码。
如果您更喜欢字符串方式,则必须用单引号将其包装,并转义内部的单引号:
$string = '<?php elseif(get_row_layout() == \'three_col_panel\'): ?>
<!-- ==================================== -->
(...)';
您不能使用双引号,因为它们允许php变量求值。
如果愿意,还可以使用Nowdoc字符串语法 :在这种情况下,不需要转义。
如果文本是字符串,请使用highlight_string( $string )
,如果要输出完整的文件,请使用highlight_file( $filepath )
。
您不需要用<code></code>
包装:上面的命令为您进行了包装。
作为礼物,您将获得彩色语法!
对于字符串:
<code><?php echo htmlentities( $string ); ?></code>
对于文件:
<code><?php echo htmlentities( file_get_contents( $filepath ) ); ?></code>
您只需将所有内容放在双引号:“ ...”之间,并在其中转义双引号:
<div class=\"icon-panel-three block\">
<div class=\"main-wrap\">
.... etc
您可以做的另一件事是:将它们全部放在.txt文件中,并用php <?php $homepage = file_get_contents('mycode.txt'); echo $homepage; ?>
显示内容<?php $homepage = file_get_contents('mycode.txt'); echo $homepage; ?>
<?php $homepage = file_get_contents('mycode.txt'); echo $homepage; ?>
使用php可以尝试使用php函数“ highlight_string();
”,如下所示:
<?php
highlight_string('<?php phpinfo(); ?>');
?>
它将使用颜色编码将('input')输出为字符串。 查看其php文档: http : //php.net/manual/zh/function.highlight-string.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.