Conditional layout templates |
I want to use layout templates instead of doing this:
PHP Code: echo view('html_top', $vars); The think is - my application handles views differently if they are AJAX calls or not. If they are AJAX it returns a JSON with the view inside of it. If not, it will return the view with top html and bottom html. For this to work I have created a View library, like so: PHP Code: public function view($view, $vars = array()) But view templates are not triggered from the controller, but from the view. Is there any way of echoing a template in a controller with another view inside of it? I guess I can pass $request->isAJAX() to the view and do something like this: PHP Code: <?php if ($isAJAX): ?> But it doesn't feel right, and I would then have to put it in all views. I rather do it from the controller or a lib.
Your template can use include() to render the main content, and for your ajax call you can return only this view. See https://codeigniter4.github.io/userguide...w-partials
PHP Code: <?= $this->extend('default') ?>
You can also echo a view in a view.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |