I need a advice on how to add some different views/tmpls to a Joomla 3.x System Plugin. I made a simple plugin to replace Joomla 3.x Offline page, but i would like to add several different views (layouts) to that plugin. I am confused with views vs tmpl. Could some one give me some guidance and give me a simple directory structure to follow.

I use this code to trigger \views\defaultview\view.html.php but i want to have more different views. Is this the correct way?

public function onBeforeRender(){
    // Get the application.
    $app = JFactory::getApplication('site');
    if (($app->getCfg('offline'))&&(!$app->isAdmin())) {
        // Get the document object.
        $doc = JFactory::getDocument();
        $view = 'defaultview';
        $basePath = __DIR__;
        $config = array( 'base_path'=>$basePath);
        require_once $basePath . DS . 'views'  . DS . $view . DS . 'view.html.php';
        JResponse::setHeader('Content-Type', 'text/html; charset=utf-8');

  • Can you be more specific about what your plugin does? Does it redirect the offline.php system page to something else? does it change the text in the offline.php page, etc. – Toni Marie Oct 26 '14 at 0:22

Your code snippet just includes a view file and stops the execution. If your view.html.php has a class, your are not going to get any output (you are not executing the view rendering).

Looking into the process, you are trying to generate a view inside the onBeforeRender event, and killing further processing. It could work, but it is better if you just redirects to any usual output method.

On the other hand, check how the SystemRedirect plugin works, calling JError/customErrorPage, it redirects to an Url or calls a JDocument rendering (both methods allow to override views and templating).

| improve this answer | |
  • Thank you for your answer. For what I want to achieve do you think this isn't the correct approach? Do you think systemredirect plugin approach is better? Thanks – n.h. Oct 26 '14 at 12:28
  • Redirect plugin is useful to redirect on errors. You can develop a similar plugin to solve your requirement, or look for an extension. – Anibal Oct 27 '14 at 12:19

If you mean templates (layouts) then you can use JLayout to render them. Make /layouts folder inside you plugin to store layouts' files. Then in your plugin:

// Set base path
JLayoutHelper::$defaultBasePath = JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name . '/layouts';

// Render mylayout.php
$renderedLayout = JLayoutHelper::render('mylayout');

Sharing layouts across views or extensions with JLayout

| improve this answer | |
  • Thank you for your answer. As a beginner when it comes templates or views I always get lost :) For what I understood JLayout its new in Joomla 3.x For what I want to achieve (have several diferent presentation types for the same output content, the user will choose wich one to use) I can have subdirectories for each diferent layout under /layouts where i will put layout file, css file, js script file...? Is this correct? No need to use views files? I apologize if I am saying some nonsense but I'm starting and I have read some things about it but I'm still with many doubts. Thanks – n.h. Oct 28 '14 at 1:45
  • This is only for layouts (templates) in which you can add your scripts or css. How to add CSS and JS: docs.joomla.org/J3.x:Adding_JavaScript_and_CSS_to_the_page You do not need to use Views for that. – Dmitry Rekun Oct 28 '14 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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