0

I want to implement functionality like CodeIgniter does in its loadview function - While loading the view we pass third parameter "true" so that the page will be taken as a string.

Using file_get_contents() is correct option while wrting custom mvc framework ?

I hav a function to load view as -

function loadView($directoryPath, $page, $data = array()) {

        extract($data);
        // construct path for loading the view
        $viewURL = $GLOBALS['config']['dir-views'];
        $path = $viewURL . DS . $directoryPath . DS . $page;

        // actual loading of a view.  
        include $path;
    }

1 Answer 1

2

It depends on your goals. If you want to get the raw view data then use file_get_contents(). If a view contains any php code that needs to be executed use ob_start and include.

Ob_start example:

function loadView($directoryPath, $page, $data = array()) {

    extract($data);
    // construct path for loading the view
    $viewURL = $GLOBALS['config']['dir-views'];
    $path = $viewURL . DS . $directoryPath . DS . $page;

    // actual loading of a view.  
    ob_start(); // Turn on output buffering
    include $path; // Output the result to the buffer
    return ob_get_clean(); // Get current buffer contents and delete current output buffer
}

ob_end_clean()

Sign up to request clarification or add additional context in comments.

4 Comments

I want to make AJAX call which will bring some info and then that info will be shown in popup. So contents in the popup are written in a file. So which way will be better ?. thank you for the option ob_start will look into it also.
The first thing that came to mind is to create the renderView($filePath, $params) function with ob_start and include functional, then use it like renderView('myview.html.php', $someInfo). $someInfo variable will be available in the template
Question updated. I have a function loadview as shown above. here, is there any need of using ob_start function ? I am not clear.
Actually, there is no need to use this function. Without it the loadView function will output the result. If you want this function to return string result use ob_start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.