5

I'm making an AJAX call to my JController and would like to send back a 'partial' view for display within the existing page, populated with some data.

Is it possible to load a file from <view>/tmpl/, inject data into it and store the resulting snippet?

I'm thinking I need to do something like:

controller.php (in action method)

$view = $this->getView('MyView', 'results'); // loads view.results.php
$view->assignRef('data', $response); // assign data
$storedView = $view->display(); // store the view

view.results.php

function display($tpl = null){
    return $this->loadTemplate('results'); // loads '/<view>/tmpl/default_results.php'
}

EDIT

I've edited the earlier incorrect code above to what seems to be working.

Is this best practice for this particular use case? Would it be considered bad form to simply add a new method to the existing view.html.php that returns the loaded template?

2
  • 2
    Notice that assignRef() and assign() are deprecated. So $view->assignRef('data', $response); should be $view->data = $response;.
    – Farahmand
    Commented Jul 30, 2014 at 21:32
  • @codinghands If you have found a solution that seems to be working, submit it is an answer instead of as part of the original question. You can answer your own questions! What you has looks to be a valid answer, for sure! Commented Sep 11, 2014 at 2:35

1 Answer 1

3

Your URL for the partial view / task needs to look like this:

index.php?option=com_example&view=partial&format=raw

You can also create a new method in the controller, if needed. Make sure that the default display() method is not overridden in a way that hinders the setting of a different view.

Now create the following files:

JROOT/components/com_example/views/partial/view.raw.php

<?php
defined('_JEXEC') or die;

class ExampleViewPartial extends JViewLegacy
{
    function display($tpl = null)
    {
        // Get the data needed for the view ...

        // Call parent
        parent::display($tpl);
    }

}

JROOT/components/com_example/views/partial/tmpl/default.php

<?php
echo 'Hello partial world!';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.