나는 stackoverflow 에서이 질문을 읽고있었습니다.
/programming/104516/calling-php-functions-within-heredoc-strings
받아 들여진 대답은 다음과 같은 일반 PHP 템플릿을 수행한다고 말합니다.
template.php :
<html>
<헤드>
<title> <? = $ title?> </ title>
</ head>
<본체>
<? = getContent ()?>
</ body>
</ html>
index.php :
<? php
$ title = '데모 타이틀';
getContent () 함수 {
return '<p> Hello World! </ p>';
}
include ( 'template.php');
?>
위의 내용은 template.php가 다른 스크립트에 정의 된 변수에 의존한다는 의미에서 잘 구성되어 있지 않습니다. 그리고 include ()를 사용하여 코드를 실행할 때 include('template.php')(include ()를 사용하여 즉시 실행되지 않는 클래스 또는 함수를 포함하는 것과 반대).
더 나은 접근 방법은 템플릿을 함수로 감싸는 것입니다.
template.php :
<? php
함수 템플릿 ($ title, $ content) {
ob_start (); ?>
<html>
<헤드>
<title> <? = $ title?> </ title>
</ head>
<본체>
<? = $ content?>
</ body>
</ html>
<? php return ob_get_clean ();
}
?>
index.php :
<? php require_once ( 'template.php'); print template ( '데모 타이틀', '<p> Hello World! </ p>'); ?>
두 번째 방법이 더 낫습니까? 더 좋은 방법이 있습니까?