[英]php $output .= include code from file
Just trying to tidy up a little bit of code here. 只是想整理一下这里的代码。 I have a php command to output some html.
我有一个php命令来输出一些html。 However, one of my comands is quite a large amount of html, I was wondering if it's possible to output code referenced in a different file?
但是,我的命令之一是大量的html,我想知道是否可以输出在另一个文件中引用的代码?
For example, my current php looks like this: 例如,我当前的php看起来像这样:
$output .= '<div class="contact-form '.$css_class.'" >';
$output .= '<h4 class="form-title">'.$title.'</h4>';
$output .= 'SOME VERY LONG CODE'
Is it possible to do something like this: 是否可以做这样的事情:
$output .= include('file_with_long_code.html');
instead? 代替? I aven't tested this, but am curious to know if it works or what the proper way of doing it is
我没有测试过,但是很想知道它是否有效或者正确的方法是什么
您可以改为使用php的getfilecontent函数
$output .= file_get_contents('file_with_long_code.html');
You could do something like this: 您可以执行以下操作:
ob_start();
include('somefile.php');
$output = ob_get_contents();
Read more about output buffering in the docs: http://php.net/manual/en/function.ob-start.php 在文档中阅读有关输出缓冲的更多信息: http : //php.net/manual/en/function.ob-start.php
I recommend using a PHP Framework, most of them have a very good functionality for these Kinds of "Problems". 我建议使用PHP框架,其中大多数针对这些“问题”都具有非常好的功能。
html file.tpl.php: html file.tpl.php:
<div class="contact-form <?=$css_class; ?>" >
<h4 class="form-title"><?=$title; ?></h4>
SOME VERY LONG CODE
main file: 主文件:
<?php
ob_start();
include('file.tpl.php');
$output = ob_get_contents();
ob_end_clean();
?>
$output .= file_get_contents('file_with_long_code.html'); $ output。= file_get_contents('file_with_long_code.html');
Yes, it is possible. 对的,这是可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.