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条)

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题