[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [jfw] Passing values to View renderer from Controller



Hi Ganesh,

I had the same problem for my first app.  The way I tend to use the framework is that the base classes are there but you just need to take these and extend them to suit your own style of working.  In my opinion that's preferable to having your way of working dictated to you by the framework classes.

I ended up extending the default AbstractHtmlView (Joomla\View\AbstractHtmlView).  I use Twig so it has some Twig specific code in it. but you can see the gist here. (https://gist.github.com/purplebeanie/96469427fcea9f5c8902)

(NB: I am pretty sure I pulled a lot of this code from David Hurley's sample app and probably some other tutorials and examples else as well so if anyone recognises any of this thank you in advance and I don't claim credit for it and thank you for helping me get started!!)

I added a function addData (down on line 145) that can be called from the controller to add data to the view.  This just kept an internal $this->data array populated with anything that needed to be passed to the final output.

Then in the render method on line 113 the $this->data array is passed to the twig render instance….. or if you're not using Twig then into then used by the html / php file you include or whatever templating engine you decide to use.


Then in the controller where you have in your LoginController: 

$view = parent::execute();

In my apps I replace this with:

$view = new \Purplebeanie\View\DefaultTwigView($app,new \Purplebeanie\Model\DefaultModel($input,$app->db,null,$app),JPATH_TEMPLATES);

(NB: This constructor is overly long.  I need to sit down and get my head around Dependency Injection one weekend so that I don't need to do this.  It's also got all the namespacing so you would modify this to suit your own project)


then to add data I use:

$view->addData('items',$items);

And to render….

return $view->render();

I'm sure there's a lot of other ways to skin the same cat.  To me that's one of the cool things about the Joomla Framework.  It doesn't get in your road, it just gives you the building blocks to put together how you will.

Cheers,
Eric.






On 14/05/2014, at 4:19 AM, Ganesh Kumar <ganesh35 AT gmail.com> wrote:

Hello, 

I am using the sample app from https://github.com/dbhurley/framework-app   and trying to pass values to view.   

My controller :

namespace App\UserComponent\Controller;
use App\Controller\DefaultController;
class LoginController extends DefaultController
{
protected $defaultView = 'login';
        public function execute()
        {
            $view=parent::execute();
            
             //    $view->render->set('test', "this is a test");

            return $view;
        }
}


Please suggest me a command or a way to do this.

Ganesh

--
Framework source code: https://github.com/joomla/joomla-framework
Visit http://developer.joomla.org for more information about developing with Joomla!
---
You received this message because you are subscribed to the Google Groups "Joomla! Framework Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-framework+unsubscribe AT googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-framework.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail