dszdiavv474681 2016-02-28 05:57 采纳率: 0%
浏览 106
已采纳

从包含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>
  • 写回答

2条回答 默认 最新

  • dpd7195 2016-02-28 08:01
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch