Jump to content

Topic on Extension talk:PhpTags/Magic expressions

How do you pass an argument into a phptag block?

6
Woozle (talkcontribs)

The only methods I've been able to think of don't seem to work:

  • The phptag subset does not support user functions.
  • A phptag inside a template only resolves arguments after the PHP is executed (so the string processed by PHP code is "{{{1}}}" or whatever, not the argument being passed.
  • Variables defined in a phptag in one page do not persist to transcluded phptags

I feel like there must be something obvious that I'm missing, and maybe I'll think of it after I post this...

Pastakhov (talkcontribs)

> The phptag subset does not support user functions.

Yes, it does not. But you can write a template with phptag code and transclude it, it works almost similar...


> A phptag inside a template only resolves arguments after</nowiki>...

Not sure I understand you correctly. use <phptag> instead of {{#phptag:}}

to access the arguments passed to a template use $argv[1] variable, it works similar native PHP code


> Variables defined in a phptag in one page do not persist to transcluded phptags

use global variables similar native PHP code

Woozle (talkcontribs)

$argv does not ever have anything in it except the template name (in $argv[0]), according to print_r($argv).

Woozle (talkcontribs)

I think I did figure it out, 45 minutes later -- inside the template, use this:

{{#tag:phptag |
$arg1="{{{1}}}";
// code can use $arg1
}}
Pastakhov (talkcontribs)

No, it will not work as expected because it inserts {{{1}}} as php code. yes it works in some cases, but generally it is bad idea to use the code like this.

<phptag>echo $argv[1];<phptag> should print text passed as the first parameter to the template/page, for example save the code as test_phptags template and then transclude is as {{test_phptags|1=HELLO}} on a page

Woozle (talkcontribs)

Ahh, ok -- it was the "1=" part that I must have been leaving out. I will give that a try!

Reply to "How do you pass an argument into a phptag block?"