我有两个文件index.php和template.html。在模板文件中,我有一个div,里面包含一些PHP代码。我想要实现的是从包含所有内容的模板中拉出div并将其插入到我的主索引页面。我设法这样做,但只有在div中没有PHP代码。但是,如果有任何PHP,我会看到类似的东西" saveHTML($ snippet [1])?&gt ;;"而不是完整的PHP代码块。能否解释一下我无法移动div的原因,包括PHP代码。
index.php文件
<?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文件
<div class="post">
<?php echo $examples->saveHTML($snippet[1]) ?>;
</div>
答案 0 :(得分:1)
你可以用更简单的方式做到这一点
首先在template.html中使用%%posts%%
替换您的动态内容
即,
template.html
<div class="post">
%%posts%%
</div>
然后在index.php中使用file_get_contents获取模板的内容 之后用动态代码替换它,如下所示
$htmlFile = 'template.html';
$yourDynamicContents = 'Replace your dynamice msg here';
$contents = file_get_contents($htmlFile);
$contents = str_replace('%%posts%%', $yourDynamicContents, $contents);
$contents
有整页...您可以使用该模板回复或发送邮件,甚至可以传递打印pdf等
以上将简单干净。 P.S你可以替换$ yourDynamicContents中的任何内容,无论是css,html,js
希望上述答案有所帮助 谢谢
答案 1 :(得分:0)
您是否尝试过通过file_get_contents获取模板文件的内容?那么也许你可以使用strpos()
从substr()中提取你的div