templates - Execute the Code in a PHP Page And Echo the HTML Output -
i absolutely don't post question here in unless can't find way solve problem myself. did lot of googling , not able find solution 1 problem describe.
here problem. creating templated php website. templated mean below:
<?php include("header.php");?> <div id="content"> <div id="main"> <h2><?php echo($page_title);?></h2> <?php echo ($page_content); ?> </div> <?php include("sidebar.php");?> </div> <?php include("footer.php");?>
as can see here page template echoes content of $page_content variable between header , footer sections build page. keep code clean , separated (in own way) have been placing html content in .txt files (let's page1_content.txt) , assigning txt content variable ($page_content) below:
$page_content = file_get_contents("page1_content.txt");
my problem starts when place php code in page1_content.txt, lets' call file page2_content.php (yes, change file .txt .php). assign content of file $page_content variable below usual:
$page_content = file_get_contents("page2_content.php");
now, when page template echoes page2_content.php contents php code in echoed string , not executed, trying query database , stuff in file php code. mean, want php code inside page2_content.php executed , cumulative html code echoed "echo" line inside template file.
how can achieve this? please ask me questions if need more info/clarification.
thanks
edİt: many people here suggested solution including file. actually, tried including file before didn't working, broke template, though on wrong track , quit "include" way of doing this. since here advising use include tried again. replaced php code in "page2_content.php" basic 1-line code see if gets executed before adding generated html code without breaking template , worked. apparently php code had problem @ first place , hence broke template execution.
now have changed template structure , pages using template, , seems work nicely. lot everybody. have up-voted every answer suggesting use include :)
as @ali suggested, include files. other option highly suggest do not use eval() function.
Comments
Post a Comment