I've tried searching around, but it doesn't seem like anyone has had this problem before. I'm still a bit new to hacking at the MediaWiki API, so I figured I'd ask here before diving into the MediaWiki source code.
From within the callback of either a tag or parser function hook, is
there a way to determine whether or not the page being rendered is a template?
Here's a simple example of about what I'm talking:
$wgParser->setHook( 'foo', 'fooParse' );
function fooParse( $input, $args, $parser ) { if( WE ARE RENDERING INSIDE A TEMPLATE ) return 'FOO'; else return 'BAR'; }
Here's my underlying motivation: I want to be able to label sections for transclusion from within a template (using either the LST or DPL extensions). Currently, this is how it's done:
//// Article A //// <section begin=content /> This is some text to be transcluded. <section end=content />
//// Article B //// %% Transclude the text from labeled section `content' of [[A]] %%
The above code works perfectly, however, here's what I want to be able to do:
//// Template:BeginConent //// <section begin=content />
//// Template:EndContent //// <section end=content />
//// Article A //// {{BeginContent}} This is some text to be transcluded. {{EndContent}}
That, however, doesn't work (the transcluded text ends up being empty). Here's my theory on why it doesn't work from looking at the LST and DPL extensions' code:
They each do something like this:
$wgParser->setHook( 'section', array( __CLASS__, 'noop' ) );
where 'noop' is a function that just returns the empty string, which is what hides the <section ... /> tags from being rendered in the wiki text. Then, inside the functions that do the transclusion, it actually parses the raw DOM of the page to be transcluded looking for the <section> tags (which are still in the raw DOM even though they have not been rendered to the wiki text).
Although I admit that I do not fully comprehend the order in which MediaWiki parses/expands/transcludes templates, my theory is that the `<section begin=content />' tag inside the `BeginContent' template above is being rendered inside the template before it is transcluded into article A, thus, the section tags are not being included in A's DOM. My theoretical solution to this problem is to hack the `section' tag's hook in DPL such that it will only render to an empty string when inside of the ultimate article.
Is this even possible? Does anyone have any alternate theories and/or suggestions?
Thanks!
- Evan
You may be interested in the InternalParseBeforeLinks hook in Parser.php. This runs after all template transclusion has finished, but before actual wikitext rendering.
Note that it won't help you if you want to use it in combination with tag extensions <likethis> since those are stripped out early in the parsing process (long before InternalParseBeforeLinks) and then executed/reinserted later on.
However, if you wanted to use magic words or html comments (like <!-- begin thing -->), those /may/ work. I'd have to check though, my gut tells me that html comments are stripped out earlier on in the parsing process.
To answer your original question about telling when you're inside a template, I know there's a function in the Parser class for recursing down through the Temlpate transclusion tree, but I don't remember what it's called or if there are any hooks therein which would be of any use.
-- Jim R. Wilson (jimbojw)
On Wed, Mar 26, 2008 at 11:04 AM, Evan Sultanik evan@sultanik.com wrote:
I've tried searching around, but it doesn't seem like anyone has had this problem before. I'm still a bit new to hacking at the MediaWiki API, so I figured I'd ask here before diving into the MediaWiki source code.
From within the callback of either a tag or parser function hook, is there a way to determine whether or not the page being rendered is a template?
Here's a simple example of about what I'm talking:
$wgParser->setHook( 'foo', 'fooParse' );
function fooParse( $input, $args, $parser ) { if( WE ARE RENDERING INSIDE A TEMPLATE ) return 'FOO'; else return 'BAR'; }
Here's my underlying motivation: I want to be able to label sections for transclusion from within a template (using either the LST or DPL extensions). Currently, this is how it's done:
//// Article A ////
<section begin=content /> This is some text to be transcluded. <section end=content />
//// Article B //// %% Transclude the text from labeled section `content' of [[A]] %%
The above code works perfectly, however, here's what I want to be able to do:
//// Template:BeginConent ////
<section begin=content />
//// Template:EndContent ////
<section end=content />
//// Article A //// {{BeginContent}} This is some text to be transcluded. {{EndContent}}
That, however, doesn't work (the transcluded text ends up being empty). Here's my theory on why it doesn't work from looking at the LST and DPL extensions' code:
They each do something like this:
$wgParser->setHook( 'section', array( __CLASS__, 'noop' ) );
where 'noop' is a function that just returns the empty string, which is what hides the <section ... /> tags from being rendered in the wiki text. Then, inside the functions that do the transclusion, it actually parses the raw DOM of the page to be transcluded looking for the <section> tags (which are still in the raw DOM even though they have not been rendered to the wiki text).
Although I admit that I do not fully comprehend the order in which MediaWiki parses/expands/transcludes templates, my theory is that the `<section begin=content />' tag inside the `BeginContent' template above is being rendered inside the template before it is transcluded into article A, thus, the section tags are not being included in A's DOM. My theoretical solution to this problem is to hack the `section' tag's hook in DPL such that it will only render to an empty string when inside of the ultimate article.
Is this even possible? Does anyone have any alternate theories and/or suggestions?
Thanks!
- Evan
MediaWiki-l mailing list MediaWiki-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-l
mediawiki-l@lists.wikimedia.org