How do I... Put a module as a view/element into a template and/or into a page/view

edited April 2014 in Modules
What I need to do is be able to statically and dynamically present a module's controller methods as a view (read HTML), and display those views within other views, blocks and/or page content. I recently switched over to fuel, because it really looks like you guys know what you are doing, and have tackled alot of the problems I have encountered in the past.

I have been able to easily create this sort of functionality with my own stack of CI extensions (Ie. not using fuel). I've done this using HMVC, and DataMapper for ORM. What I'd like to know is how I can make this work using fuel, if I create a simple and/or advanced module.

As an example.... If I decided to have a News module, and a "get_top_3" method in a news controller within that module, that returns a list of the top 3 news items (in HTML), how would I be able to place that "view" within a layout and/or block element in fuel CMS?

Thanks ahead of time for looking at this.

Comments

  • edited 11:54PM
    I'm not entirely sure I follow but I'll give it a shot. If you are wanting to create a page in the CMS and want to output it's content in a controller, you can use something like:
    $this->fuel->pages->render('mypage', $vars);
    http://docs.getfuelcms.com/general/pages-variables

    For blocks, which are essentially partials and stored in the _views/_blocks folder, you can use the "fuel_block('my_view')" function. The added benefit of using fuel_block instead of the load->view method is that it will also check the CMS for a block with the same name and serve that up instead (assuming your fuel_mode in MY_fuel.php is set to "auto").

    With regards to "module" methods, I'm guessing you are meaning a model method that's associated with your module. Simple modules are essentially just wrappers around a model.

    For what you are explaining, we usually take the controller out of the equation and add the model::get_top_3 call at the top of a block or view file to keep things together.

    I realize this may not answer your question entirely, so please follow up with any more additional information you think may help.
  • edited 11:54PM
    Good morning!

    Thanks for the reply :)

    We are currently trying to create a front-facing (client-side) survey module for fuel. Currently, we have used a "simple" module, which as you have stated is just a MY_Model that has been extended with base_module_model functionality.

    Creating the surveys on the back-end (admin) is ridiculously easy (Thank you!) because it is a simple module. Let's pretend that the administrator can create a survey, with up to 3 questions, and because this is a simple module, there is a corresponding table on the database that relates to this. We would then like this module to show up, wherever we want. Ie. I would like to place it in the main layout, so that it shows up in every page that uses that layout. There could be other instances as well, where I may want to place the survey such as: CMS page, normal view file, a block view, other layouts, etc.

    When I say I would like it to "show up" what I mean is I would like to display a view, that is generated (most likely by a controller, using Form Builder) that allows any site visitor to vote for the current survey, which will update the database and increment the corresponding answers total.

    I should throw out a disclaimer right now :) I do have this working, but not in the way intended. I have created a controller (controllers/survey) that does use Form Builder, creates a form that displays a list of options derived from a survey record and presents it in a view file. The problem is how I display it on the page. Currently, in order to include it in the main layout page, I am AJAX loading it from a view block (_blocks/survey). In the block lives the JS code that loads the view from the controller. However, like I had stated: Not what I intend. This relies on the client having javascript enabled, and also means that any content loaded will not be indexed by search engines. There are even more reasons not to use this method, but I am still not understanding how else I can load a module (or controller) function, as a view into another view file, while still keeping an MVC approach. Please help!
  • edited 11:54PM
    FUEL likes to break the typical MVC approach especially when it comes to CodeIgniter. The reasoning is that CI's implementation of creating a controller and method to display a view is overkill for most websites and only seems logical for pages that require a lot of processing (e.g. a contact form). Instead, FUEL makes assumptions as to where to grab the proper view and associated layout. What it sounds like you are wanting is something like a block that is self-contained and portable that can be inserted into a view or layout for display. Often times we will create blocks that include the PHP logic within them or we extract it out into a convenience library (e.g. a newsletter signup form) and include that library in the block (essentially acting as controller logic for the block). This may or may not include the processing actions (e.g. what to do with the $_POST). Sometimes we leave that to a normal controller.
Sign In or Register to comment.