CodeIgniter Forums
Custom view method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Custom view method (/thread-76370.html)



Custom view method - imabot - 05-06-2020

In CI3, I created a custom view method (cView) by extending CI_Loader :


Quote:// My controller, log user data and search queries
class MY_Loader extends CI_Loader
{
    public function __construct()
    {
        parent::__construct();
    }
   
    // Custom view loader, load and return the view if display is true
    public function cView($view, $vars = array(), $display = TRUE)
    {
        if (!$display) return '';
        return $this->view($view, $vars, TRUE);
    }
}



Since CI_Loader and the core folder no longer exist, I can't figure out where I should create my custom cView function in CI4? The main constrain is that cView() calls the native view() method.


RE: Custom view method - jreklund - 05-06-2020

Here are the complete code for view. You can create your own cView and put in in app/Common.php based on that. Or overwrite the system function.

https://codeigniter.com/user_guide/outgoing/view_renderer.html

PHP Code:
if (! function_exists('view'))
{
    
/**
     * Grabs the current RendererInterface-compatible class
     * and tells it to render the specified view. Simply provides
     * a convenience method that can be used in Controllers,
     * libraries, and routed closures.
     *
     * NOTE: Does not provide any escaping of the data, so that must
     * all be handled manually by the developer.
     *
     * @param string $name
     * @param array  $data
     * @param array  $options Unused - reserved for third-party extensions.
     *
     * @return string
     */
    
function view(string $name, array $data = [], array $options = []): string
    
{
        
/**
         * @var CodeIgniter\View\View $renderer
         */
        
$renderer Services::renderer();

        
$saveData config(View::class)->saveData;

        if (
array_key_exists('saveData'$options))
        {
            
$saveData = (bool) $options['saveData'];
            unset(
$options['saveData']);
        }

        return 
$renderer->setData($data'raw')
                        ->
render($name$options$saveData);
    } 



RE: Custom view method - imabot - 05-07-2020

(05-06-2020, 11:33 AM)jreklund Wrote: Here are the complete code for view. You can create your own cView and put in in app/Common.php based on that. Or overwrite the system function.

Thank for the answer, if I want to overwrite the system function, where should I put the code ?


RE: Custom view method - jreklund - 05-08-2020

In app/Common.php, as it's loaded first.


RE: Custom view method - imabot - 05-09-2020

Thank you to all of you for these answers. I finally place my custom view in the BaseController since I use it everywhere in my apps: https://forum.codeigniter.com/thread-75176.html