[pmwiki-users] $FmtPV

Peter Bowers pbowers at pobox.com
Fri Feb 4 08:55:43 CST 2011

On Fri, Feb 4, 2011 at 9:12 AM, Thomas Lundgren <publik at lundgren.nu> wrote:
> At 2011-02-04 05:17, Peter Bowers wrote:
>> On Fri, Feb 4, 2011 at 1:04 AM, Thomas Lundgren <publik at lundgren.nu>
>> wrote:
>> > On a page I call a cookbook-script-function with a markup like;
>> >
>> > (:my_function:)
>> >
>> > In the cookbook i have a function that checks the value in a form-field
>> > every time it is called and sets the result in a PageVariable.
>> >
>> > I want to use that variable on any page as any other PageVariable like;
>> >
>> > {$MyVariable}
>> >
>> > BUT. I can´t get this to work...
>> >
>> > I can use $FmtPV in config.php and in the cookbook-script and set the
>> > variabel $MyVariable. But I can´t change the variable content in the
>> > function that is called in the cookbook-script.
>> A couple easy things to check right off...
>> (1) Make sure you have $FmtPV declared as a global in your function
>> (2) Make sure your markup rule is ordered such that the changes you
>> are making occur before they are used (i.e., '<{$var}' or before, use
>> ?action=ruleset with diag turned on to confirm the order)
> Many thanks for you quick answer - but...
> (1) I´m declaring $FmtPV as a global in my cookbook-script (se below).

Yes, it was probably too much to hope for that we would have 2 such
easy fixes inside a couple weeks... :-)  (Although see below --
lightning *does* strike twice in the same place sometimes... :-)  )

> (2) I think I don´t understand what you are saying. :)  I don´t user
> Markup() to set the variable in any way - only to create the function that
> sets the variable.... So where and how should I use the '<{$var}' ?

Actually, no.  You're not "creating" the function at that point -- you
are "calling" the function there.  At the point when you have the line
in your config.php:
...you have "created" the function.  PHP is an interpreted language
(in most scenarios) but it does all the interpretation "up front" at
the moment of include'ing or require'ing the code.  So when your
Markup says this:

> Markup('my_function', '_begin',  '/\\(:my_function(.*?):\\)/e',
> 'main_function("$1")');

You are saying "look for the pattern that looks like (:my_function
<something>:) and when you find it *call* the function as if you had
code like this:

And the '_begin' says to do that at the very beginning when you've
just started processing rules.  One disadvantage of this is that if
you were to put something like this:
(:my_function xyz:)

it would be undefined as to whether your function would be run or not.
 (By standard usage it shouldn't be if enclosed with [@...@] or
[=...=] or (:markup:) [=...=] (:markupend:) or etc.

So I would change it to "<{$var}" instead of "_begin", but I doubt
that is your problem...

Where you have a problem is in your *placement* of the line:

global $FmtPV;

If you want it to be global for a given function, you have to declare
it global *within that function*.

Instead of this:

> global $FmtPV;
> function main_function( $opts )
> {
>        { code that set $ToMyVariable to what it should be... }
>        $FmtPV['$MyVariable'] = '$ToMyVariable'';
>        return;
> }

you should have this:

> function main_function( $opts )
> {
>    global $FmtPV;
>        { code that set $ToMyVariable to what it should be... }
>        $FmtPV['$MyVariable'] = '$ToMyVariable'';
>        return;
> }

I think when you fix that I think you will have good success.  Of
course, there may be other problems, but I know that's at least the
first layer of the onion...


More information about the pmwiki-users mailing list