Loading A View To A Module Or Plugin

130 views
Skip to first unread message

Bar Saar

unread,
Feb 19, 2013, 3:55:26 AM2/19/13
to joomla-de...@googlegroups.com
Hello everyone.

I am trying to create a module that call a view in a component (lets say a few divs and such).
The point is to have one code that displays the template and everyone that needs it just call it.

I am familiar with the LoadTemaplate, although i couldn't find a way to use it in a module.

So i am asking what is the best way to call a template (or a display) from an outside extension?

Thank you very much in advance!

hoochicken

unread,
Feb 19, 2013, 4:02:49 AM2/19/13
to joomla-de...@googlegroups.com
Hi,
you could use a require makeshift, i. e. require(dirname(__FILE__).'/default_text.php');
But this "solution" is causing problems when generating an override; then one has to move all(!) template files to the overriding folder (yet it works).
A loadTemplate() command would be much more beautiful; so I'm all ears/eyes for whom that knows:-)
Greetings
Mareike

Aymeric Dourthe

unread,
Feb 19, 2013, 4:16:09 AM2/19/13
to joomla-de...@googlegroups.com
Hi,

what about getting the component (getComponent in JComponentHelper) and
after execute it to render the needed view ?
Or maybe render the component directly (renderComponent) ?

Best regards,
Aymeric Dourthe
Your web expert engineer

Le 19/02/2013 10:02, hoochicken a �crit :
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joomla-dev-gene...@googlegroups.com.
> To post to this group, send an email to
> joomla-de...@googlegroups.com.
> Visit this group at
> http://groups.google.com/group/joomla-dev-general?hl=en-GB.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Bar Saar

unread,
Feb 19, 2013, 7:44:56 AM2/19/13
to joomla-de...@googlegroups.com
Hello and thanks for the quick reply.
I have tried using the getComponent and then get the template like this:

$component = JComponentHelper::getComponent('com_faqftw');
echo $component->loadTemplate('something');

but it does not recognize the loadTemplate, although component does contain the selected component (not null).

Fatal error: Call to undefined method stdClass::loadTemplate() in...


On Tuesday, February 19, 2013 11:16:09 AM UTC+2, Fitz wrote:
Hi,

what about getting the component (getComponent in JComponentHelper) and
after execute it to render the needed view ?
Or maybe render the component directly  (renderComponent) ?

Best regards,
Aymeric Dourthe
Your web expert engineer

Le 19/02/2013 10:02, hoochicken a �crit :
> Hi,
> you could use a require makeshift, i. e.
> require(dirname(__FILE__).'/default_text.php');
> But this "solution" is causing problems when generating an override;
> then one has to move all(!) template files to the overriding folder
> (yet it works).
> A loadTemplate() command would be much more beautiful; so I'm all
> ears/eyes for whom that knows:-)
> Greetings
> Mareike
>
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joomla-dev-general+unsub...@googlegroups.com.

Aymeric Dourthe

unread,
Feb 19, 2013, 8:40:21 AM2/19/13
to joomla-de...@googlegroups.com
Hi,

there is no loadTemplate method because $component is a stdClass instance not a jView.

I wonder if you can load the component through his controller and after get the view to load the template and after display it.

Something like :
$controller = jController::getInstance("com_faqftw");
$view = $controller->getView();
$view->loadTemplate('something');

But i'm not sure about this workaround.




Best regards,
Aymeric Dourthe
Your web expert engineer

To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Donald Gilbert

unread,
Feb 19, 2013, 9:13:37 AM2/19/13
to joomla-de...@googlegroups.com
The nice thing about Views in Joomla is that they are not directly dependent on the calling Controller, if coded the "Joomla" way - which is good (because in MVC, you should be able to swap out your view with any other view and still have it work). So, what I do to accomplish this exact thing is first make sure my View class is available (if I need to register it to the autoloader, I do) then set the layout, and display the view. This will call your display method and execute all logic there as expected.


JLoader::register('ViewClass', '/path/to/class.php');

$view = new ViewClass;

$view->setLayout('default');

$view->display();

Bar Saar

unread,
Feb 19, 2013, 1:37:17 PM2/19/13
to joomla-de...@googlegroups.com
Hey Donald.
I tried using the JLoader tho with no success. Could not find ViewClass even with the currect path.

I tried Fitz idea and its half worked (so half thank you :P).
Getting the JController and use its display worked:

$controller = jController::getInstance("com_faqftw");
$controller->display();

Although its not what we wanted so i did something like this:


$controller = jController::getInstance("com_faqftw");
$view = $controller->getView('faqs');
$view->loadTemplate('something');

everything works but it does not show anything (If i write a template that does not exist it gives me an error).
So i have default_something, but it won't display it in the module.

What may be the problem?

Thank you guys very much for your help!
> To post to this group, send an email to
> joomla-de...@googlegroups.com.
> Visit this group at
> http://groups.google.com/group/joomla-dev-general?hl=en-GB.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Aymeric Dourthe

unread,
Feb 19, 2013, 2:16:29 PM2/19/13
to joomla-de...@googlegroups.com
Hi,

I thought Donald advice was the good one...
Just to be sure, have you well replace ViewClass by your view class name ?

You have to render the view after loading the template

$view->display();

You can try without loading template and do :

$view->display('something');


Best regards,
Aymeric Dourthe
Your web expert engineer

To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Donald Gilbert

unread,
Feb 19, 2013, 3:03:13 PM2/19/13
to joomla-de...@googlegroups.com
Also - since you're not calling view->display(), you'll need to manually assign your variables. If you're expecting something like $this->items in your view layout, then you'll need to do:

$view->items = $view->get('Items');

Then you can call $view->loadTemplate();


To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Bar Saar

unread,
Feb 19, 2013, 3:40:42 PM2/19/13
to joomla-de...@googlegroups.com
Hey again.
I did replaced the ViewClass and it still dont work. works only when im in a component menu (because component is already loaded it).
Aimed the path to the view.html.php file.
and the second part is when it does work the get() function return null for some unknown reason (I did load the model and it worked, but it's not the way i wanted)

Any ideas why i can't see the ViewClass?

Donald Gilbert

unread,
Feb 19, 2013, 3:42:40 PM2/19/13
to joomla-de...@googlegroups.com
Are you loading the class path as JPATH_COMPONENT? if so, that will fail, because that only get's set when you're in your component view.

If this on the frontend, you'll need to point it to something like JPATH_ROOT . '/components/com_mycomponent/views/viewname/view.html.php';

Bar Saar

unread,
Feb 20, 2013, 2:36:55 AM2/20/13
to joomla-de...@googlegroups.com
Hey.
Yes, the JPATH_ROOT works!
but somehow i cant find layout (even default), I looked in the Joomla file where the error rises and he looks for the file in /components/com_content/views/faqs/tmpl\
why does he looks in the com_content? and how can we change it?

Thanks alot!

elin

unread,
Feb 20, 2013, 6:32:08 AM2/20/13
to joomla-de...@googlegroups.com
If you just want some layout stuff why not just use  a JLayout and share it between both the component and the module?

Elin

Bar Saar

unread,
Feb 20, 2013, 9:25:52 AM2/20/13
to joomla-de...@googlegroups.com
Hey Elin, Thanks for reply.

JLayout is implemented in Joomla! 3.0 and i need this for Joomla! 2.5.
Although this is a nice solution, I cannot use it.
Reply all
Reply to author
Forward
0 new messages