Welcome Guest, Not a member yet? Register   Sign In
Thoughts on sending data to library from view
#1

Hi

What are your thoughts on sending data to a library directly from a view and not a controller?

I know it does not follow the MVC pattern but here's why in this scenario i think its useful.

I'm working on a basic theme library that will parse the css and js files to the view dynamically dependent on what the developer wants / needs.

In most libraries this would be done in the controller doing something like this :

$this->Theme->add_css('file1.css, file2.css');

Its useful that we only include css and js files that are needed for that's specific page instead of having a huge list of additional files slowing down page load that may not be needed for that specific page.

But ...

If we want to develop themes for a specific application that may rely on different files for the same page then modifications would need to be made in the controller and its no longer a drop in replacement of themes.

for example lets say we have an application and on our customers page we are using datatables to display our data, we only load the specific css and js file for the datatables for that page. But what if our second theme does not use datatables and uses something else.

This is where i think it would be more efficient to add the required files from the view, allowing the theme designer to include the necessary files within the theme and would serve as a drop in theme with no modification to the controller.

What are your thoughts?
Reply
#2

(This post was last modified: 12-17-2020, 12:06 AM by bivanbi.)

Since you can call view from within a view, you can make the decision what other ('sub') views to load there.

Example: Views/users/list.php

PHP Code:
<?php /** @var ViewData $data */ ?><!DOCTYPE html>
<html lang="en">
<?= view($data->theme->getHeaderViewName(), ['data' => $data]) ?>
<?= view
($data->theme->getBodyViewName(), ['data' => $data]) ?>
<?= view
($data->theme->getFooterViewName(), ['data' => $data]) ?>
</html> 
Reply
#3

(12-17-2020, 12:05 AM)bivanbi Wrote: Since you can call view from within a view, you can make the decision what other ('sub') views to load there.

Example: Views/users/list.php

PHP Code:
<?php /** @var ViewData $data */ ?><!DOCTYPE html>
<html lang="en">
<?= view($data->theme->getHeaderViewName(), ['data' => $data]) ?>
<?= view
($data->theme->getBodyViewName(), ['data' => $data]) ?>
<?= view
($data->theme->getFooterViewName(), ['data' => $data]) ?>
</html> 


Its not the matter of knowing how to do it, i have a working example. Plus there is no need to re-invent the wheel im using codeigniters default way of building the templates out like

Code:
$this->extend(theme_url('layouts/main'));
$this->section('content');

The question is more specific to 'should it be done' as it doesn't strictly follow MVC pattern. My whole concern is if i define what dependencies a particular page may need, i.e css and js files in the controller, then any additional themes created would need modifications made to the controller.

Instead i propose that the dependencies are loaded from within the view file. In our 'header' template we may have already defined our site wide css file, in our 'footer' template file we have defined our site wide specific js file(s). Now if a particular page needed additional dependencies we load directly from view (such as 'customers') that is wrapped with our header and footer template files and the included files get injected to the header or footer respectively, instead of from the controller, allowing theme designers to design the theme and thus becomes a drop in replacement with no modification to the core code of the application.
Reply
#4

How about dependency inversion and make services that views can access?

In my opinion, deciding how to present content is what View in MVC pattern responsible for, and themes belong to the presentation, so it is perfectly fine to make the decision there. And this is why I do not particularly like the examples in CI documentation where the controller does the view chaining.
Reply
#5

viewCells
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

I think view_cell() was made exactly for something like that. You call a method from a library directly in a view. It returns a block of HTML generated by your library based on the parameters it received. See http://codeigniter.com/user_guide/outgoi...cells.html
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB