CodeIgniter Forums
Views renderer merging template & content - 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: Views renderer merging template & content (/thread-75693.html)



Views renderer merging template & content - foxbille - 03-06-2020

In CI3, i was using  load->view() with "true" parameter to merge template and body content

$data['body'] = $this->load->view('ecriture/index', $data, true);
$this->load->view('template', $data);

assuming that template.php contains some <?=$body?>

With CI4 I try to use view renderer,
is this the better way to manage views the same way as i exposed above

$parser = \Config\Services::parser();
$view = \Config\Services::renderer();

echo $view->render('templates/header');
echo $parser->setData($data)->render('ecriture/index');
echo $view->render('templates/footer');

looking at Docs » CodeIgniter4 User Guide
chapter Docs » Building Responses » View Parser
it is written "...to load the parser class is through its service:"
$parser = new \CodeIgniter\View\Parser();
but this leads to : ArgumentCountError
Too few arguments to function CodeIgniter\View\Parser::__construct()

instead :
$parser = \Config\Services::parser();
works well

And so does :
$parser = new \CodeIgniter\View\Parser(NULL);
don't you think user guide might mention it ?

Thanks !
Eric


RE: Views renderer merging template & content - John_Betong - 03-06-2020

@foxbill,
> Too few arguments to function CodeIgniter\View\Parser::__construct()
> don't you think user guide might mention it ?


Yes it does:

https://codeigniter4.github.io/userguide/general/common_functions.html?highlight=view#view


RE: Views renderer merging template & content - foxbille - 03-06-2020

(03-06-2020, 07:15 AM)John_Betong Wrote: @foxbill,
> Too few arguments to function CodeIgniter\View\Parser::__construct()
> don't you think user guide might mention it ?


Yes it does:

https://codeigniter4.github.io/userguide/general/common_functions.html?highlight=view#view
Thanks,
but i meant, here -> https://codeigniter4.github.io/userguide/outgoing/view_parser.html#using-the-view-parser-class


RE: Views renderer merging template & content - next2heaven - 04-16-2020

(03-06-2020, 02:14 PM)foxbille Wrote:
(03-06-2020, 07:15 AM)John_Betong Wrote: @foxbill,
> Too few arguments to function CodeIgniter\View\Parser::__construct()
> don't you think user guide might mention it ?


Yes it does:

https://codeigniter4.github.io/userguide/general/common_functions.html?highlight=view#view
Thanks,
but i meant, here -> https://codeigniter4.github.io/userguide/outgoing/view_parser.html#using-the-view-parser-class


If you look at the documentation it doesn't say to pass anything to the parser() class when instantiating yet we get an error if we don't.  This needs to be updated I image.