Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-09-24 21:02:22

morgant
Plugin Author
From: Vermont, United States
Registered: 2004-06-22
Posts: 63
Website

PHP global variable usage in a plug-in

I’ve got a fairly basic plug-in that provides a single tag for rendering a list of products (data pulled from a separate database) using a specified Textpattern form. It also implements a number of single tags for use within that form. Think <txp:article /> or <txp:file_download />.

The main tag steps through the data and fills in an array (global) called $thiswag with the necessary data, then calls parse_form(). Each of the single tags for use in the form utilize that global variable to generate their output and work fine.

However, I have four container tags which are conditional tags (basically, if_individual, if_list, if_first, if_last) and they use parse(EvalElse()). The main tag has also filled in three global variables ($is_waglist, $is_firstwag, and $is_lastwag) for use by said tags. However, those globals are always empty even though they’re initialized and referenced the same way (the only difference is they’re booleans instead of arrays).

So, is there something that EvalElse() does when called from within parse_form() that could cause this? I’ve been browsing through the source and didn’t immediately see anything, but then again I don’t know it too well yet.

Any tips on further debugging this? Any help would be greatly appreciated.

Offline

#2 2008-09-24 22:22:44

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: PHP global variable usage in a plug-in

This is not related to this problem, but if this is a public plugin, it should use a prefix (the entire plugin name, preferably) for the global variables as well, to avoid naming conflicts.

Offline

#3 2008-09-24 22:36:07

morgant
Plugin Author
From: Vermont, United States
Registered: 2004-06-22
Posts: 63
Website

Re: PHP global variable usage in a plug-in

Yup, I definitely realize that. In this case, it’s a private plug-in so I wasn’t really worried about it (and also why I’m being a little vague as to the actual tag names).

Offline

#4 2008-09-25 09:27:36

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: PHP global variable usage in a plug-in

evalElse doesn’t touch those globals you’ve created.

Offline

#5 2008-09-25 11:42:35

morgant
Plugin Author
From: Vermont, United States
Registered: 2004-06-22
Posts: 63
Website

Re: PHP global variable usage in a plug-in

Yes, I realized that as well. I was mainly concerned that evalElse() could be deferring the parsing and such to a time when I’m actually no longer in my loop and so the variables are not set. It doesn’t look like it, but I wasn’t sure if there was something along those lines that I’m missing.

I’m back to littering my code with dmp() calls and prefixing the global variable names (just-in-case), so hopefully I’ll find more clues or just plain solve it. In the meantime, anything you all have run into when using global vars would be helpful. I don’t see too many plug-ins that actually use globals and try to avoid them myself.

Thanks again for the input so far.

Offline

#6 2008-09-25 13:37:00

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: PHP global variable usage in a plug-in

EvalElse accepts two parameters: a chunk of text containing TXP tags and a condition (true / false).
It searches the chunk of text for the <txp:else /> tag and returns either the part of text+TXP-tags before the else tag (if the condition is true) or the other part. It doesn’t really parse the TXP tags like the normal parser would (the tag handler functions are not called from within evalelse), so you have to parse the return value of the call to the evalelse function.

Offline

#7 2008-09-25 16:08:07

morgant
Plugin Author
From: Vermont, United States
Registered: 2004-06-22
Posts: 63
Website

Re: PHP global variable usage in a plug-in

That’s what I thought. Thanks for confirming.

Offline

#8 2008-09-25 17:34:41

morgant
Plugin Author
From: Vermont, United States
Registered: 2004-06-22
Posts: 63
Website

Re: PHP global variable usage in a plug-in

Well, after adding a ton of debug line and sifting through it all, I found the bug: An accidental == attempting to set one of the global vars.

Changing all the global variable names helped me find it though. Thanks!

Offline

Board footer

Powered by FluxBB