1

Is it possible to retrieve the <head> of a document from a component then use it within a raw view?

I'm know if you use format=raw you can have the document only output what the component returns (i.e. without any other modules, template stuff, etc.)

I'm trying to create a view for my component that includes a bit of the template (specifically the <head>) and then only what my component generates (I plan on passing this to a a CLI utility wkhtmltopdf to generate a PDF of the output).

I need the head as my component relies on template CSS for styling, but I don't want anything else - basically:

<html>
<head>
...css, js, title, etc from template...
</head>
<body>
...ONLY output from my component, like 'format=raw'...
</body>
</html>
  • No, you can't. Try domPDF and visit this tutorial. – Farahmand Aug 4 '14 at 12:17
  • This looks good, but it would mean my component is dependent on the J! install having dompdf installed, /html/renderer copied to /pdf/renderer amongst other non-standard dependencies. Unless I write an installation script to do this... – codinghands Aug 4 '14 at 12:32
2

This is a way to perform it as you expect:

The view.raw.php would be something like this:

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

class YOURCOMPONENTViewYOURVIEW extends JViewLegacy // or JView depends on J! version
{
    public function display($tpl = 'raw')
    {
        parent::display($tpl);
    }
}

The default_raw.php file inside tmpl folder would be something like this:

<?php
defined( '_JEXEC' ) or die;
$url = jUri::base();

$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, TRUE);
$contents = curl_exec($ch);
curl_close($ch);

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($contents);
libxml_use_internal_errors(false);

$xpath = new DOMXPath($dom);
$head = $xpath->query('/html/head')->item(0);
$head = $dom->saveHTML($head);
echo '<html>' . $head . '<body>BLAH BLAH</body></html>'
  • This is almost perfect - just need to change ->saveXml at the end to saveHtml. – codinghands Aug 5 '14 at 10:50
2

Afaik, there is no <head> in JDocumentRaw. The raw format is meant to generate non-HTML output, without loading the template at all.

If you want to only generate the component output as a HTML page using the template design, then you should add &tmpl=component to the URL. This will load the component.php file in your template instead of the index.php. It should not load any module position and only output the component. This is especially useful for modal windows and the like.

  • This is interesting, thanks. Just looked at the component.php that ships with the template we use and it is missing some of the key statements, but I think I can get this to work and it keeps things as uncoupled as possible. Thanks! – codinghands Aug 3 '14 at 17:47
  • Is there any way I can capture the full output of this (i.e. component.php including my component output) from within the controller method, and do something with it? I'm playing with JDocument but no such luck yet... – codinghands Aug 4 '14 at 13:11
  • I doubt you can do this in your component. Simply because of the order in which the stuff is loaded. You would have to do that in a system plugin in an event after the rendering is done. – Bakual Aug 4 '14 at 14:06
  • Yeah, I'm coming across that. I can load component.php via $document->render(false, array("template" => "templateName", "file" => "component.php"));, but it obviously doesn't contain the component output yet. I could pass a URL to my PDF generator, but that won't have the session data I need, and there doesn't seem to be a way to load up an existing session – codinghands Aug 4 '14 at 14:07
  • The only thing I can think of doing is taking that rendered output, parsing the HTML returned, and injecting my output into the body, but that is pretty hacky. – codinghands Aug 4 '14 at 14:10

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.