We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

What is best method to use to insert a javascript stub into template output?

Development and Programming

JCOGS Design's avatar
JCOGS Design
148 posts
4 years ago
JCOGS Design's avatar JCOGS Design

I wish for an add-on to be able to insert a block of javascript into the parsed template output.

It is unclear from the documentation how this is most efficiently achieved, and I would value any guidance that this community can offer about the best way to approach doing this.

The ee documentation alludes to the existence of a method called ‘output’ in the javascript class (i.e. ee()->javascript->output()) - it is referred to here and here but only in passing and is absent from the documentation on the javascript class itself, so it is not clear how this method might be deployed in practice.

There are also the methods set_output and append_output in the documentation for the Output class, but again the information about how to use these methods is terse and unhelpful.

If you can shed any light on the best way forward using these (or some other) methods I would be most grateful.

       
Andy McCormick's avatar
Andy McCormick
322 posts
4 years ago
Andy McCormick's avatar Andy McCormick

JCOGS, Is this something like if your add-on parses a tag in a template that you want to then append the Javascript at the end of the output vs just allowing the user to use a tag like {exp:myaddon:js} and your add-on replaces that with the Javascript block? Since JS can really be inserted anywhere (unless depending on other scripts to be called), a simple tag is how I would do it.

       
JCOGS Design's avatar
JCOGS Design
148 posts
4 years ago
JCOGS Design's avatar JCOGS Design

It’s a bit like that - if a particular option parameter is chosen in any call to the add-on made by the template being parsed, then the javascript block needs to be loaded with the template: I can work out if the javascript is required, I just want to understand if there is an ee preferred way of inserting it into the parsed output; better to use that than come up with some home-brew solution.

Asking the add-on user to remember to insert a tag in order to load the required javascript is clunky and would not be (in my view) a good solution.

       
JCOGS Design's avatar
JCOGS Design
148 posts
4 years ago
JCOGS Design's avatar JCOGS Design

Added here in case it is useful for others:

In the absence of any better suggestions what I did was:

  • Use the template_post_parse hook to grab the rendered HTML for the page
  • Scanned the rendered HTML for evidence that the javascript addition would be needed (look for a specific class appearing in HTML)
  • If insert required, split the rendered HTML output at </body> and rejoined the two bits of the template with my javascript inserted between
  • Return rendered HTML to EE parsing…

This works. Whether a better solution exists within EE core methods will (I guess) remain a mystery.

       
Andy McCormick's avatar
Andy McCormick
322 posts
4 years ago
Andy McCormick's avatar Andy McCormick

JCOGS, I talked with the team and template_post_parse definitely seems to be the way to go. Add-ons are called earlier in the parsing process, so using ee->output methods with your add-on won’t work.

Within the template_post_parse you should be able to use append_output or other output methods. However, I would also saying that the way you’re doing this isn’t wrong either.

       
JCOGS Design's avatar
JCOGS Design
148 posts
4 years ago
JCOGS Design's avatar JCOGS Design

Thanks Andy for the further clarification: I’ll just leave it doing what it is doing then. In passing, while we are here, worth noting that the documentation for the append_output method appears in the documentation for the Output Class which is one of the “tersely” documented Classes in the docs … and so it is not made clear what element the method is ‘appending’ stuff to … one of the several places where just a couple more sentences in the docs would make them much more useful…

? 1
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.