Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Tag Parse Order?
Does anyone know if there is any way I can specify the order in which the tags for a plugin are parsed? I know that they are typically parsed in the order in which they appear in a template, but I’d like to have one specific tag in a plugin that I’m working on get parsed AFTER all the other plugin tags, regardless of where it appears on a template, as it will utilize data set up by other plugin tags.
I’m thinking maybe there’s some way to do it setting up callbacks to $pretext
and $textpattern
, or perhaps using an output buffer, but I haven’t yet found a way.
Thanks!
Michael
Offline
Re: Tag Parse Order?
Can you explain why you need to override tag parsing order? That makes it easier to find a solution.
Offline
Re: Tag Parse Order?
Sure, Ruud. I’m working on a large update for my Kimili Flash Embed plugin which has 2 tags for a user to deal with:
<txp:kml_flashembed />
– This can be put anywhere in an article, page or form to designate a swf to be embedded at that spot on the rendered page. The attributes of each instance of this tag are saved to a global array for use by:<txp:kml_flashembed_js />
– Needs to take the array of attributes from all instances of the<txp:kml_flashembed />
and use those attributes to set up Javascript necessary to register and render each SWF.
Obviously, all instances of the first tag need to be processed before the second one can be processed. If I put the second tag at the end of a page template, then everything works correctly, thanks to the default parse order. The thing is, I want a user to be able to place the <txp:kml_flashembed_js />
tag in the <head>
if they’d like, too. Making that happen would also help maintain backwards compatibility with old versions of the plugin, where <txp:kml_flashembed_js />
simply created a reference to the swfobject javascript file and nothing more. I’m sure many people put that in the <head>
of their templates.
As a bit of background, one of the driving reasons for this is the difference between how SWFObject 1.x (which the old version of the plugin uses) and SWFObject 2.x (which this overhaul uses) works. The cleanest way to work with SWFObject 2 is to register and instanciate all the swfs on a page in one place.
Does this make it clear?
Offline
Re: Tag Parse Order?
Have you tried using the textpattern_end
callback event? That triggers at the very end, after all the tag parsing has been done.
Offline
Re: Tag Parse Order?
Ruud –
I tried simply adding register_callback("kml_flashembed_js", 'textpattern_end');
immediately after the kml_flashembed_js
function is defined, but it made no difference. Perhaps I’m using it incorrectly, though, as I don’t fully understand textpattern’s callback system.
I suppose my next question is how do you use textpattern_end
to output something to the page at a specific location of your choosing while preventing the tag that marks that location from being parsed the first time TXP encounters it?
Offline
Re: Tag Parse Order?
kimili wrote:
I suppose my next question is how do you use
textpattern_end
to output something to the page at a specific location of your choosing while preventing the tag that marks that location from being parsed the first time TXP encounters it?
Typically, you’d execute a two-step approach:
- Register your own custom function as a handler for
textpattern_end
so you receive a notification from core when all page generation code has finished. All HTML will sit in PHP’s output buffer by then. - Retrieve PHP’s output buffer in said function using
ob_get_contents()
and its relatives andpreg_replace()
a well defined HTML tag with you own code (most plugin authors choose to admend the final</body>
tag).echo
the composite HTML to the client’s browser.
Offline
Re: Tag Parse Order?
Thanks for the explanation, wet! I got it working by registering 2 callbacks:
1) On textpattern
, use an ob_start()
to open up an output buffer and do the preg_replace()
that I need.
2) On textpattern_end
, call ob_end_flush()
With that combination, I’m able to place the scripts anywhere in the page without any extra tag required on the user’s part.
Offline
Re: Tag Parse Order?
kimili wrote:
1) On
textpattern
, use anob_start()
to open up an output buffer and do thepreg_replace()
that I need.
Textpattern starts an output buffer, so you might not need to add your own.
Offline
Re: Tag Parse Order?
wet wrote:
Textpattern starts an output buffer, so you might not need to add your own.
I can’t seem to hook into that properly. I tried using ob_get_contents()
instead of ob_start()
, but to no avail. Here’s the code I tried:
function _textpattern() { $html = ob_get_contents(); $this->_buildJavascript($html); } function _textpattern_end() { ob_flush(); }
Here’s what works for me:
function _textpattern() { ob_start(array(&$this, '_buildJavascript')); } function _textpattern_end() { ob_end_flush(); }
Am I missing something trying to hook into the output buffer thats started in publish.php
starts?
Offline
Pages: 1