2011-09-10 10 views
7

मैं अपने स्वयं के एमवीसी ढांचे पर काम कर रहा हूं। नीचे एक उदाहरण नियंत्रक है जो मैंने अभी तक किया है।मुख्य टेम्पलेट फ़ाइल में एमवीसी दृश्यों को कैसे लोड करें

मेरे पास मेरे नियंत्रक में मॉडल लोड करने और फ़ाइलों को देखने का एक तरीका है।

मैं अपनी साइट के लिए अलग-अलग टेम्पलेट विकल्प भी प्राप्त करना चाहता हूं। मेरा टेम्पलेट सिर्फ एक पृष्ठ लेआउट होगा जो मेरे नियंत्रक से बनाए गए विचारों को मेरे टेम्पलेट फ़ाइल के बीच में डालेगा।

/** 
* Example Controller 
*/ 
class User_Controller extends Core_Controller { 

    // domain.com/user/id-53463463 
    function profile($userId) 
    { 
     // load a Model 
     $this->loadModel('profile'); 

     //GET data from a Model 
     $profileData = $this->profile_model->getProfile($userId); 

     // load view file and pass the Model data into it 
     $this->view->load('userProfile', $profileData); 
    } 

} 

यहाँ टेम्पलेट फ़ाइल की एक बुनियादी विचार है ...

DefaultLayout.php 

<!doctype html> 
<html lang="en"> 
<head> 
</head> 
<body> 



Is the controller has data set for the sidebar variable, then we will load the sidebar and the content 
<?php if(! empty($sidebar)) { ?> 

<?php print $content; ?> 

<?php print $sidebar; ?> 


If no sidebar is set, then we will just load the content 
<?php } else { ?> 

<?php print $content; ?> 

<?php } ?> 

</body> 
</html> 

, AJAX के लिए इस्तेमाल किया जा सकता किसी भी शीर्ष, पादुका, कुछ और बिना एक और खाका कॉल

EmptyLayout.php 

<?php 
$content 
?> 

मैं इस बारे में विचारों की तलाश में हूं कि मैं अपनी मुख्य टेम्पलेट फ़ाइल कैसे लोड कर सकता हूं और फिर मेरी मुख्य लेआउट फ़ाइल के सामग्री क्षेत्र में फ़ाइलों को शामिल और देख सकता हूं?

नमूना लेआउट फ़ाइल में, आप देख सकते हैं कि सामग्री क्षेत्र में एक सामग्री है जिसे $ सामग्री कहा जाता है। मुझे यकीन नहीं है कि मैं अपने मुख्य लेआउट टेम्पलेट में डालने के लिए विचार सामग्री के साथ कैसे पॉप्युलेट कर सकता हूं। आप किसी भी विचार है, तो पोस्ट करें नमूना

उत्तर

12

कुछ की तरह

function loadView ($strViewPath, $arrayOfData) 
{ 
// This makes $arrayOfData['content'] turn into $content 
extract($arrayOfData); 

// Require the file 
ob_start(); 
require($strViewPath); 

// Return the string 
$strView = ob_get_contents(); 
ob_end_clean(); 
return $strView; 
} 

एक छोटा सा फिर साथ

$sidebarView = loadView('sidebar.php', array('stuff' => 'for', 'sidebar' => 'only'); 
$mainView = loadView('main.php', array('content' => 'hello',, 'sidebar' => $sidebarView); 
+0

यह बहुत अच्छा है का उपयोग करें, मैं हमेशा से प्रारूप और नियंत्रक के भीतर सामग्री उत्पादन सेट/मॉडल फिर 'file_get_contents' का उपयोग करें और फिर प्लेसहोल्डर्स को दृश्य में प्रतिस्थापित करें जैसे: '

{content}

' str_replace के साथ। अच्छी चीज़ –

संबंधित मुद्दे