2016-09-07 4 views
0

私のecho'dレンダークラス内の文字列にアクセスしたいと思います。レンダリング機能内の文字列へのアクセス方法

コントローラファイル:

$Page = new Page; 

$string = "Some data here..."; 

echo $Page->render(); 

がbody.php:

<body> 
    echo '<p>'.$string.'</p>'; //$string returns as 
</body> 

はレンダリング:一般

class Page 
{ 
    public function render(){ 
     include 'header.php'; 
     include 'body.php'; 
     include 'footer.php'; 
    } 
} 
+0

コントローラファイルの変数がレンダリング機能の有効範囲外です。 – Rizier123

答えて

0

これらの場合の値はパラメータを介して送信されます。何かのように:

$Page = new Page; 

echo $Page->render(array(
    "string" => "Some data here...", 
    "other_variable" => "Some other value here", 
)); 

次に、あなたがあなたのrenderメソッド内でこれらの値を使用:

class Page 
{ 
    public function render($values) { 
     extract($values); 
     include 'header.php'; 
     include 'body.php'; 
     include 'footer.php'; 
    } 
} 

これが唯一のデモの目的のために非常に簡単な例であることに注意してください!

+0

これは機能しました!素晴らしいと簡単なソリューション、ありがとう! – jrobilliard

関連する問題