2016-10-14 2 views
0

콘텐츠를 제공하기 위해 템플릿 기반 시스템을 만들려고하고 있지만 해결할 수없는 문제가 발생했습니다. 내가 포함하는 데이터가 포함 된 변수를 에코하려고하면 내 HTML의 잘못된 부분에 출력됩니다.html의 잘못된 위치에 PHP가 울림

아래는 브라우저에서 실행될 내 페이지 인 'newstuff.php'입니다. 문제가되는 변수는 $php $head $content입니다.

<?php 

$php = include "templates/content/newstuff/phpCode.php"; 
$head = include "templates/content/newstuff/head.html"; 
$content = include "templates/content/newstuff/content.php"; 

include realpath(dirname(__FILE__)).'/templates/templateMain.php'; 
?> 

아래는 'tempalteMain.php'입니다. 이것은 내 tempalte입니다. 울리는 위치는 $php $head $content입니다.

<?php 
echo $php; 
?> 


<!DOCTYPE html> 
<html> 

    <head> 
     <?php echo $head; ?> 
    </head> 

    <body class="body1" onload="inputBlur2()"> 
     <div class="borderLine"></div> 
     <div class="banner"></div> 

     <div class="mainContent1" > 

      <div class="header1" > 

       <div class="headerContainer" > 

        <ul class="navList1"> 
         <li><a id = "B0" href="index.php">New Stuff</a></li> 
         <li><a id = "B1" href="MainPage.php">Products</a></li> 
         <li><a id = "B2" href="ProjectsPage.php">Projects</a></li> 
         <li><a id = "B3" href="AOrdering.php">About Ordering</a></li> 
         <li><a id = "B4" href="ContactMe.php">Contact Us</a></li> 
         <li><a id = "B5" href="FAQPage.php">FAQ</a></li> 
         <li><a id = "B6" href="SCart.php">My Cart</a></li> 
        </ul> 
       </div> 
      </div> 

      <div class="content1"> 
       <?php echo $content; ?> 
      </div> 

     </div> 
    </body> 
</html> 

다음은이가 $head PHP 변수로 전달 될 코드를 제공하는 'head.html'입니다.

<meta charset="UTF-8"> 
<link rel="stylesheet" type="text/css" href="/../StylePR.css"> 
<title>KickUp Electronics</title> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 

다음은이가 $content PHP 변수로 전달 될 코드를 제공하는 'content.php'입니다.

<p>Welcome to the new stuff page!!!</p> 

마지막으로 이것은 크롬 DOM 편집기에서 가져온 출력물입니다. content.php의 정보 위치가 잘못되어 있으며 이상한 '1'이 반복되어 표시됩니다 (페이지 소스를 볼 때 head.html의 정보가 html 태그 외부에 위치 함).

<!DOCTYPE html> 
    <html> 
<head><meta charset="UTF-8"> 
     <link rel="stylesheet" type="text/css" href="/../StylePR.css"> 
     <title>KickUp Electronics</title> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
</head> 
<body class="body1" onload="inputBlur2()"> 
<p>Welcome to the new stuff page!!!</p> <!--Wrong Location!--> 
1 





     1 



     <div class="borderLine"></div> 
     <div class="banner"></div> 

     <div class="mainContent1"> 

      <div class="header1"> 

       <div class="headerContainer"> 

        <ul class="navList1"> 
         <li><a id="B0" href="index.php">New Stuff</a></li> 
         <li><a id="B1" href="MainPage.php">Products</a></li> 
         <li><a id="B2" href="ProjectsPage.php">Projects</a></li> 
         <li><a id="B3" href="AOrdering.php">About Ordering</a></li> 
         <li><a id="B4" href="ContactMe.php">Contact Us</a></li> 
         <li><a id="B5" href="FAQPage.php">FAQ</a></li> 
         <li><a id="B6" href="SCart.php">My Cart</a></li> 
        </ul> 
       </div> 
      </div> 

      <div class="content1"> 
       1   </div> 


     </div> 

</body></html> 

많은 해결책을 찾기 위해 노력했습니다. html이 완전히로드되기 전에 echo가 실행되는데 문제가 있습니까? 어떤 도움을 주시면 감사하겠습니다!

+0

이것은 작동하지 않습니다. 당신이 생각하는 방식으로. $ php = include "templates/content/newstuff/phpCode.php"; 바로 포함 시키실 수 있습니다. TBO 나는 PHP를위한 적절한 템플릿 엔진을 볼 것이다. 나뭇 가지는 정말 쉽고 시작하기 쉽다. –

+0

원하는 위치에 파일을 포함 시키십시오. 'include'는 파일의 내용이 아닌 true/false를 반환합니다. – aynber

+0

@ 리차드 Housham 웃긴 나뭇 가지를 언급 ​​재미있는, 그때 당신이 PHP 코드를 포함 couldnt 깨달 았어 : ( – DiscreteTomatoes

답변

2

잘못 사용하고 있습니다.

$php = include "templates/content/newstuff/phpCode.php"; 

해당 파일의 출력을 출력하고, $php1에 설정 는 즉시이다 (즉 "은 일!").

처리 결과 : 실패시에 FALSE을 포함하고 경고를 발생합니다. 성공적으로 포함 된 파일에 의해 무시 된 경우를 제외하고는 1을 반환합니다. - http://php.net/manual/en/function.include.php

당신은 출력을 캡처하는 output buffering를 사용할 수 있지만, 더 나은 솔루션은 아마 templateMain.php에 직접 include 전화를 움직이고있다.

+1

사이드 노트 : Laravel과 같은 적절한 프레임 워크를 고려해보십시오. 템플릿 엔진은 모범 사례 방식으로 이러한 작업을 처리합니다. – ceejayoz

+0

정보를 주셔서 감사합니다, 나는 파일의 경로에 변수를 설정하여 작동하게하고, 대신 templateMain.php에 변수와 함께 표시됩니다. – DiscreteTomatoes