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.
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.
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.
Added here in case it is useful for others:
In the absence of any better suggestions what I did was:
template_post_parse
hook to grab the rendered HTML for the page</body>
and rejoined the two bits of the template with my javascript inserted betweenThis works. Whether a better solution exists within EE core methods will (I guess) remain a mystery.
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.
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…
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.