从包含PHP代码的外部文件导入HTML标记
I have two files index.php and template.html. In the template file I have a div, which contains some PHP code inside. What I am trying to achieve is to pull the div from template including everything inside and insert it to my main index page. I managed to do so, but only if there is no PHP code inside the div. If however there is any PHP included I see something like this "saveHTML($snippet[1]) ?>;" instead of full PHP code block. Could you please explain the reason why I am not able to move the div including PHP codes.
index.php file
<?php
//some basic stuff such as new DOMDocument(); loadHTMLFile and so on
$post = $posts->query("//div[contains(@class, 'post')]");
?>
<body>
<?php echo $templates->saveHTML($post[0]);?>
</body>
template.html file
<div class="post">
<?php echo $examples->saveHTML($snippet[1]) ?>;
</div>
我有两个文件index.php和template.html。 在模板文件中,我有一个div,里面包含一些PHP代码。 我想要实现的是从包含所有内容的模板中拉出div并将其插入到我的主索引页面。 我设法这样做,但只有在div中没有PHP代码。 但是,如果有任何PHP,我会看到类似“saveHTML($ snippet [1])?&gt ;;”的内容 而不是完整的PHP代码块。 你能否解释一下我无法移动div的原因,包括PHP代码。 p>
index.php文件 p>
&lt; ?php
//一些基本的东西,比如新的DOMDocument(); loadHTMLFile等
$ post = $ posts-&gt; query(“// div [contains(@class,'post')]”);
?&gt;
&lt; body&gt;
&lt;?php echo $ templates-&gt; saveHTML($ post [0]);?&gt;
&lt; / body&gt;
code> pre>
template.html file p>
&lt; div class =“post”&gt;
&lt;?php echo $ examples-&gt; saveHTML($ snippet [1])?&gt ;; \ n&lt; / div&gt;
code> pre>
div>
you can do it in a simpler manner
first in template.html please replace your dynamic content with %%posts%%
i.e,
template.html
<div class="post">
%%posts%%
</div>
then in your index.php get contents of template using file_get_contents after that replace it with your dynamic code like as below
$htmlFile = 'template.html';
$yourDynamicContents = 'Replace your dynamice msg here';
$contents = file_get_contents($htmlFile);
$contents = str_replace('%%posts%%', $yourDynamicContents, $contents);
$contents
has whole page...you can either echo or send mail with that template or even pass to print pdf etc
the above will do it simply and clean. P.S you can replace anything in $yourDynamicContents whether its css,html,js
Hope the above answer helps Thank you
Have you tried getting the content of your template file via file_get_contents? Then maybe you can extract your div with substr() using strpos()