Code above document header

Discussion and education for beginner / novice programmers interested in embarking on the development process to take advantage of the extensible nature of the Joomla! CMS.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Forum rules
Post Reply
saltoricco
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Sat Nov 24, 2007 6:10 pm

Code above document header

Post by saltoricco » Thu Nov 29, 2007 1:38 am

Hi,

I'm looking for a way to add a PHP script block above the HTML document header. JDocument doesn't seem to allow for that. I'd like to do something like that in the view class:

Code: Select all

   $doc =& JFactory::getDocument();
    $doc->addPhpInclude('path/to/script.php');
The reason is I'm integrating an existing application into Joomla. It uses AJAX that is processed in the calling documents above the header to prevent any output. I'd like to keep the application as original as possible.

Thanks much!

User avatar
ianmac
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 237
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Code above document header

Post by ianmac » Thu Nov 29, 2007 2:27 am

Can you clarify exactly what you are trying to do?  It isn't exactly clear...

Is the PHP Script supposed to output something that should appear above the HTML document header?  If that is the case, then I would simply add a module position in the template and create a module that would produce the desired output.

If you are just trying to return results of an AJAX request, then you want to create a raw view.

Ian
Help test my Component XML Generator Tool!
http://extensions.joomla.org/component/option,com_mtree/task,viewlink/link_id,1997/Itemid,35/
All feedback appreciated!

saltoricco
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Sat Nov 24, 2007 6:10 pm

Re: Code above document header

Post by saltoricco » Thu Nov 29, 2007 2:51 am

ianmac wrote: Can you clarify exactly what you are trying to do?  It isn't exactly clear...

Is the PHP Script supposed to output something that should appear above the HTML document header?  If that is the case, then I would simply add a module position in the template and create a module that would produce the desired output.

If you are just trying to return results of an AJAX request, then you want to create a raw view.

Ian
Hi Ian,

Thank you very much for your quick response! Yes, you assumed right, and it is AJAX requests that are to be handled by this script. Actually also other code that manipulates JavaScript (Google Map stuff). Both your suggestions sound like excellent ideas. I didn't know a module could be outside the html document.

I'm on my way, thanks again!

Holger

User avatar
ianmac
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 237
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Code above document header

Post by ianmac » Thu Nov 29, 2007 3:14 am

Help test my Component XML Generator Tool!
http://extensions.joomla.org/component/option,com_mtree/task,viewlink/link_id,1997/Itemid,35/
All feedback appreciated!

saltoricco
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Sat Nov 24, 2007 6:10 pm

Re: Code above document header

Post by saltoricco » Thu Nov 29, 2007 5:31 am

ianmac wrote: Take a look at this thread...http://forum.joomla.org/index.php/topic,217394.0.html
Uh, yes Ian, that raw view mode is very handy. Thanks again!!

Holger

User avatar
CirTap
Joomla! Intern
Joomla! Intern
Posts: 73
Joined: Mon Dec 12, 2005 5:34 pm
Contact:

Re: Code above document header

Post by CirTap » Thu Nov 29, 2007 3:15 pm

Hi,

you're aware that the templates *are* PHP code, right?
You can put any sort of php code anywhere you want. To include another file, simply use


...


Not sure what you want to appear/happen before the html tag; some header() maybe, or a nasty XML prolog, but that's about all I can think of.

Note that all the processing of templates is encapsulated in various class methods and functions.

Have fun,
CirTap
You can have programs written fast, well, and cheap, but you only get to pick 2 ...

"I love deadlines. I like the whooshing sound they make as they fly by." Douglas Adams

User avatar
ianmac
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 237
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Code above document header

Post by ianmac » Thu Nov 29, 2007 3:48 pm

Hey CirTap,

I think he wanted code above the HTML header that would suppress the stuff below it from being displayed...  not certain though...

Ian
Help test my Component XML Generator Tool!
http://extensions.joomla.org/component/option,com_mtree/task,viewlink/link_id,1997/Itemid,35/
All feedback appreciated!

saltoricco
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Sat Nov 24, 2007 6:10 pm

Re: Code above document header

Post by saltoricco » Thu Nov 29, 2007 4:56 pm

CirTap wrote: Hi,

you're aware that the templates *are* PHP code, right? ...
Thanks, CirTap.

I'm a Joomla n00b, so that probably explains most of it.

Ian is right, I wanted the PHP code block above the HTML document. Maybe we're talking about two different templates, the Joomla one and the one from the component.

An example where code above HTML comes in handy is a Google Map page. I load markers from the database and generate JavaScript to display them, along with some AJAX stuff for edit forms that Google Maps shows in the info windows. The list of markers depends on HTML GET parameters (i.e. "show all markers in Alabama"). Currently, I load the PHP to generate the JavaScript through a PHP file that's included with a tag, added through JDocument::addScript(). That works, but really is a hack, since the PHP code also prepares data for a list that's shown below the map. It is cleaner to have a dedicated code block above the HTML page and use the data for the JavaScript include and the rest of the page.

Thanks guys, appreciate the help you offer!

Holger

User avatar
ianmac
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 237
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Code above document header

Post by ianmac » Thu Nov 29, 2007 6:58 pm

hmmm...  that's not what I was understanding.

I'm still not sure I'm exactly following...  Can you try and outline more clearly what you want to spit out to the browser in a single page load so that we can get a better grasp of what you are trying to accomplish?

Ian
Help test my Component XML Generator Tool!
http://extensions.joomla.org/component/option,com_mtree/task,viewlink/link_id,1997/Itemid,35/
All feedback appreciated!

saltoricco
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Sat Nov 24, 2007 6:10 pm

Re: Code above document header

Post by saltoricco » Thu Nov 29, 2007 8:40 pm

Hi Ian,

I try to keep everything in a component called "glidewx".

One of its views "components/com_glidewx/views/sites/view.html.php" loads the Google Maps Java Script generating code:

Code: Select all

  function display($tpl = null) {
...
    $document    =& JFactory::getDocument();
...
    $document->addScript('components/com_glidewx/includes/php/glidewx/sitemap-js.php');
...
  }
(I guess the relative URL should be Joomla'fied.)

"sitemap-js.php" has this:

Code: Select all

  $site = new Site();
  $siteList = $site->loadList($siteFilter);
"Site" is the object that represents a hang gliding site. It gets me the latitude and longitude to put a marker on the map. I then iterate through $siteList and putput JavaScript for Google Maps to add a marker:

Code: Select all

        // Loading the sites:
<?php
        if (is_array($siteList))
        {
            foreach($siteList as $site)
            {
                if ($site->hasGeoLocation())
                {
?>
        var marker<?= $i ?> = new GMarker(new GLatLng(<?= $site->displayGeoLocation() ?>), siteIcon);
        GEvent.addListener(
            marker<?= $i ?>,
            "click",
            function()
            { marker<?= $i ?>.openInfoWindowHtml(
                getInfoWndHtml(<?=
                        $site->id . ',' .
                        $site->type . ',"' .
                        addslashes($site->name) . '","' .
                        addslashes($site->town) . '","' .
                        addslashes($site->state) . '","' .
                        addslashes($site->country) . '","' .
                        addslashes($site->url1) . '","' .
                        addslashes($site->description) . '"' ?>)); }
            );
        batch.push(marker<?= $i ?>);
<?php
                    $i++;
                }
            }
        }
There is more JavaScript, of course. The component's template (components/com_glidewx/views/sites/tmpl/map.php) has a little form to select select country, state to be displayed in the map (simplified here). And then the Google Map:

Code: Select all

<form method="get" action="<?= $_SERVER['PHP_SELF'] ?>">
<fieldset>
State:<input name="state" type="text" id="state" value="<?= $state ?>" size="4" maxlength="2" /> 
<input type="submit" />
</fieldset>
</form>
<div id="map" style="width: 600px; height: 600px"></div>
As I said, it works but is not a pretty solution. I.e. the variable "$state" used in the form of the template is filled in the PHP file that is included to the page through a JavaScript tag:

Code: Select all

<script type="text/javascript" src="components/com_glidewx/includes/php/glidewx/sitemap-js.php"></script>
I am thinking about hacking JDocument and adding a method that allows me to include a PHP file at the top of the Joomla template. You said I could do that through a module, but I'd like to keep all the weather application stuff within the component.

Did that make things a little more clear? Sorry for the long post, thanks for listing.

Holger

User avatar
ianmac
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 237
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Code above document header

Post by ianmac » Thu Nov 29, 2007 9:19 pm

Still not sure...

Perhaps you can show what your page output should look like and annotate on it where it comes from...

I was under the understanding that it was syntactically incorrect for an HTML page to have anything except the DOCTYPE declaration before the opening tag, so I guess I'm a little confused as to exactly why you would want to output anything before that.

Ian
Help test my Component XML Generator Tool!
http://extensions.joomla.org/component/option,com_mtree/task,viewlink/link_id,1997/Itemid,35/
All feedback appreciated!

User avatar
ianmac
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 237
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Code above document header

Post by ianmac » Thu Nov 29, 2007 9:23 pm

I am thinking about hacking JDocument and adding a method that allows me to include a PHP file at the top of the Joomla template. You said I could do that through a module, but I'd like to keep all the weather application stuff within the component.

Maybe a helpful question for me to ask would be what would the PHP file at the top of the Joomla! template output?

Ian
Help test my Component XML Generator Tool!
http://extensions.joomla.org/component/option,com_mtree/task,viewlink/link_id,1997/Itemid,35/
All feedback appreciated!

saltoricco
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Sat Nov 24, 2007 6:10 pm

Re: Code above document header

Post by saltoricco » Thu Nov 29, 2007 9:43 pm

ianmac wrote:Maybe a helpful question for me to ask would be what would the PHP file at the top of the Joomla! template output?
Hi Ian,

The PHP above the header doesn't output anything. It loads data and fills data structures that are used in the header (actually in the JavaScript linked in by the header).

Yes yes, this is what the View is for. Hmm. Maybe I should make the decision to really buy into Joomla's MVC concept. I wasn't so sure about this step, frankly. The weather application needs pretty hefty calculations and I'd like that code to be as close to the presentation as possible, for easier debugging. As (former) J2EE developer I completely enjoy the simplicity of PHP, i.e. not requiring MVC per se. I know of the advantages of MVC, but the core of my weather app. doesn't need it. There is more to the site though, so Joomla will help me greatly nonetheless. The great response from you and others on this forum make a decision to integrate into Joomla easier.

Thanks!

Holger

User avatar
ianmac
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 237
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Code above document header

Post by ianmac » Thu Nov 29, 2007 10:06 pm

ah...  right...  I see...

So why not use JDocument->addScriptDeclaration()?

http://dev.joomla.org/component/option, ... claration/

I think this is what you want to do...

When Joomla! handles a request, the first thing it does is initialize the framework.  Then it will dispatch the application to your component.  The output of your component is stored in a buffer in JDocument.

Then the JDocument render method is invoked and it picks up the template/theme index.php file.  This file is parsed to detect which modules need to be loaded.  The module output is computed and the placeholders in the index.php file are substituted for this data.  Then the placeholder for the component is replaced with the earlier computed component output.

This is a roundabout way of saying that all the PHP code that your component outputs is executed before anything is displayed from the template.  In fact, nothing is sent to the browser until after the very last line of index.php, where there is an echo JResponse::toString() call.  This call will take all the buffered output and crank it out to the browser.

Things can be added to the header at any point during execution of your component - it makes little difference where this is done.

If your component outputs stuff, it is all displayed in the order that it is output (obviously).  So that means that you can still debug fairly easily no matter where the processing happens because you can just stick an 'echo' in and it outputs.

Hopefully this helps...  MVC is certainly powerful, and I still think it would be helpful for what you are trying to do...  encapsulation is a good thing.  While MVC can become very complex, it really doesn't have to be mindboggling.

Ian
Help test my Component XML Generator Tool!
http://extensions.joomla.org/component/option,com_mtree/task,viewlink/link_id,1997/Itemid,35/
All feedback appreciated!


Post Reply