0

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>
1
  • The browser interprets <?php echo $examples-> as an HTML tag. This "tag" is unknown to the browser, so it ignores it and displays only the "contents". Commented Feb 28, 2016 at 7:19

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.