• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bays - Easy "widgets" for your views

#1
Introducing myth:Bays: a simple way to easily create and cache reusable "view fragments" for your views.

This is a solution to the HMVC dilemma - how to I easily insert view fragments from my libraries and modules without using "bad" HMVC controller-calling-controller practices. This allows you to get content from any class that can be autoloaded (or any CodeIgniter library). That content can be optionally cached so that the library is never loaded and no processing takes place for even more speed.

Need an example usage? Ok - say you've got a Shopping Cart module that you've written. You want the cart info/button to show up an all of the store pages, but you don't want to have to build a method into a MY_Controller to load it for every page, and you definitely don't want to hassle with loading that view in every controller method that it should be displayed on. Instead, you slide over to your template view where it should show up, and add the following single line of code:

Code:
$bay->display('cart::show_info_button', 'user='. $user_id .' state=open');

Now, this bay will locate and load up the Cart library, call the show_info_button method, passing an array of key value pairs to it that contains 'user' and 'state'.

This does assume that you've made $bay available within your views. Probably handled in your MY_Controller.

Since this is framework-agnostic, you can use it any project, but you'll have to create a couple of simple framework integrations. It does come with CodeIgniter integrations already, of course.

Would love to hear if you find this useful, and any thoughts on the project as a whole and even ideas for the future of it.
Reply

#2
(05-27-2015, 10:12 PM)kilishan Wrote: Introducing myth:Bays: a simple way to easily create and cache reusable "view fragments" for your views.

This is a solution to the HMVC dilemma - how to I easily insert view fragments from my libraries and modules without using "bad" HMVC controller-calling-controller practices. This allows you to get content from any class that can be autoloaded (or any CodeIgniter library). That content can be optionally cached so that the library is never loaded and no processing takes place for even more speed.

Need an example usage? Ok - say you've got a Shopping Cart module that you've written. You want the cart info/button to show up an all of the store pages, but you don't want to have to build a method into a MY_Controller to load it for every page, and you definitely don't want to hassle with loading that view in every controller method that it should be displayed on. Instead, you slide over to your template view where it should show up, and add the following single line of code:


Code:
$bay->display('cart::show_info_button', 'user='. $user_id .' state=open');

Now, this bay will locate and load up the Cart library, call the show_info_button method, passing an array of key value pairs to it that contains 'user' and 'state'.

This does assume that you've made $bay available within your views. Probably handled in your MY_Controller.

Since this is framework-agnostic, you can use it any project, but you'll have to create a couple of simple framework integrations. It does come with CodeIgniter integrations already, of course.

Would love to hear if you find this useful, and any thoughts on the project as a whole and even ideas for the future of it.

How do I install this? Tried composer install and nothing happened.

Thanks
Reply

#3
(06-01-2015, 05:53 AM)frocco Wrote: How do I install this? Tried composer install and nothing happened.

At the command line, in the project root where your composer.json file is (per packagist.org):

Code:
composer require 'myth/bay:1.0-beta1'         // or
composer require 'myth/bay:dev-develop' // for develop branch

This will install it to your project.

Then make sure that CodeIgniter is setup to use Composer's autoloader by editing config/config.php:

Code:
$config['composer_autoload'] = 'vendor/autoload.php;

Then you need some way to instantiate the class and make it available to your views. For almost all of my projects, I have some form of setVar() method that allows me to collect data to be passed to the view, but you could do it within a standard $data array, also. Something like this in your controller's method:

Code:
$data['bay'] = new \Myth\Bay\Bay( new Myth\Bay\CI3Finder() );
$this->load->view('some_view', $data);

At this point, $bay is available in your views for you to use however you would like. See the Readme for more information.
Reply

#4
Thank you
Reply

#5
Is it possible to use Bay within CodeIgniter2 environment? What modifications should I make in Bay's source code?
Reply

#6
Hi Kilishan,

Can you explain why using a helper function to render the shopping cart would be a bad idea. This would make it global (so to speak) and can be accessed within any view. Unless I'm missing something.
Practical guide to IgnitedCMS - Book coming soon
Reply

#7
(10-15-2015, 11:32 AM)Kulavert Wrote: Is it possible to use Bay within CodeIgniter2 environment? What modifications should I make in Bay's source code?

Since it's loaded via Composer, you'd first have to get that working with your CI2 environment. In the past, I did this by copying the relevant code from CI3, which is in /system/core/CodeIgniter.php.

After that, it looks like it should work, but you may need to implement a CacheInterface and LibraryFinderInterface for CI2. You could probably use the supplied CI3 implementations as a starting point for CI2 (in fact, they may work for CI2 without modification, but I'm not 100% sure on that).
Reply

#8
(10-15-2015, 11:32 AM)Kulavert Wrote: Is it possible to use Bay within CodeIgniter2 environment? What modifications should I make in Bay's source code?
(10-16-2015, 09:51 AM)mwhitney Wrote: Since it's loaded via Composer, you'd first have to get that working with your CI2 environment. In the past, I did this by copying the relevant code from CI3, which is in /system/core/CodeIgniter.php.

I've always gone the lazy way and just add 'include "vendor/autoload.php" at the top of the index.php file. I've never ran into any issues that way. Other than that, it should all work, I believe. You can use the CI3Cache and CI3Finder files and they should work without modifications.

(10-15-2015, 11:48 AM)iamthwee Wrote: Hi Kilishan,

Can you explain why using a helper function to render the shopping cart would be a bad idea. This would make it global (so to speak) and can be accessed within any view. Unless I'm missing something.

That would work fine. My goal with this library (which I put together in an evening, so there wasn't a whole lot of thought put into it, honestly.) was to make a framework-agnostic package. That, and the desire to keep the code clean, made it use multiple methods, so a class was a natural choice. Adding a single method to call, like in a helper, would be a great addition, that would simply call the class' function. Would be easier to work with that way, definitely.
Reply

#9
I wrote a helper function for CodeIgniter3:
https://github.com/kenjis/codeigniter-wi...helper.php

See https://github.com/kenjis/codeigniter-widgets
Do you want to write tests for your apps? Our book, CodeIgniter Testing Guide would help you.
Reply

#10
Looks great!

And thanks for the pull requests.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.