[pmwiki-users] $FmtPV

Thomas Lundgren publik at lundgren.nu
Sun Feb 6 16:44:52 CST 2011


Hi Peter,

Thank you. I´ve read. I´ve understand something 
at least. :). I´ve followed your instructions. 
But, still... I can´t get it to work.

What I´m sure about is that in config.php I´m 
including my cookbook and therefor "creating" the function there.

The Markup works. I can call the function with my 
markup (:my_function:) and if I user "return 
"Hello!" at the end of the function I´m getting a 
"Hello!" in my broweser every time the function is called.

So far so good.

The part in my function that reads a form-field 
also works (havent mentioned that part before in this thread).

These to lines reads the field (with the name 
"form_field_name") and stores it in $retstr and 
returns it. The text entered in the form is then 
displayed when the function is clled.

main_function()
{
         $retstr = stripmagic($_REQUEST['form_field_name']);
         return $retstr;
}

Still - so far so good.

But - now I want to use that string as a 
PageVariabel to be displayed on any page in my 
wiki until it is changed by a call to the same 
function and replaced with the new text in the 
form field. Just to be clear - the text should be 
user uniq. I´m not trying to do anything between different users.

I'm now trying this;

main_function()
{
         global $FmtPV;

         $retstr = stripmagic($_REQUEST['form_field_name']);
         $FmtPV['$MyVariable'] = '$retstr';

         return $retstr;
}

And thereafter I try to display the PageVariabel I´ve created with;

{$MyVariable}

On the wiki-pages where I want it to be displayed...

(I´ve also tried {*MyVariable} since I somewhere 
read that the asterix makes the PageVariabel more 
global but stil with no success.)

But... this doesent work. :(

Please, kick me in the right directions... :)

Thanx in advance and regards,
/ Thomas.



At 2011-02-04 15:55, Peter Bowers wrote:
>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:
>===(snip)===
>include_once("$FarmD/cookbook/my_cookbook.php");
>===(snip)===
>...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:
>===(snip)===
>main_function("<something>");
>===(snip)===
>
>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:
>===(snip)===
>[@
>(:my_function xyz:)
>@]
>===(snip)===
>
>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:
>
>===(snip)===
>global $FmtPV;
>===(snip)===
>
>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...
>
>-Peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.pmichaud.com/pipermail/pmwiki-users/attachments/20110206/672e924c/attachment-0001.html>


More information about the pmwiki-users mailing list