Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2011-11-24 19:24:54
- lithium002
- Member
- Registered: 2011-04-29
- Posts: 25
Passing a function's $atts array to another function?
Hi guys,
I’m trying to build a textpattern plugin to secure functions that I call outside of my textpattern site. I went through several useful guides online but I can’t seem to figure out a way to do what I’m trying to do, which might seem ridiculously easy to some of you (or at least here’s hoping!).
Here is my code:
function kam_membership($atts, $thing=NULL) {
extract(lAtts(array( ‘first_name’ => ‘’, ‘last_name’ => ‘’, ‘postalcode’ => ‘’, ), $atts));
$result = ($first_name != ‘’ && $last_name != ‘’ && $postalcode != ‘’) ? 1 : 0;
return parse(EvalElse($thing, $result));
}
I’m calling the tag like this:
<txp:kam_membership first_name="sdfg" last_name="sdfg" postalcode="sdf" >
I have everything I need.
<txp:else />
I need more informationn.
</txp:kam_membership>
I’m curious, though, how I could pass the attributes of txp:kam_membership
to another function within the same plugin?
For example, if I had another tag txp:kam_get_first_name
that returned the first name within the container tag txp:kam_membership
, how would I make the variables global to be used by that function within the same plugin?
Offline
Re: Passing a function's $atts array to another function?
As you said yourself, just make the attributes you need global. Or static. Or use classes and methods (which tho would need you to use wrapper functions as TXP parser only sees functions defined in global scope).
If you end up using global variables, just remember to prefix them. Rest of it should be very easy after you get hang of it. Small example:
/**
* That awesome container tag everyone is talking about.
* $kam_membership variable is used to pass the data
* from the function to another
*/
function kam_membership($atts, $thing='') {
global $kam_membership;
$kam_membership = lAtts(array(
'first_name' => '',
'last_name' => '',
'postalcode' => '',
), $atts);
extract($kam_membership);
return parse(EvalElse($thing, ($first_name != '' && $last_name != '' && $postalcode != '')));
}
/**
* Returns the first name
*/
function kam_member_first_name() {
global $kam_membership;
return htmlspecialchars($kam_membership['first_name']);
}
And you should have working single tag that returns the first name.
<txp:kam_membership first_name="John">
Hello there, <txp:kam_member_first_name />!
<txp:else />
Something is missing here, but at least you have a
first name, <txp:kam_member_first_name />.
Or do you? I didn't specifically checked for that, did I.
</txp:kam_membership>
Last edited by Gocom (2011-11-24 22:50:50)
Offline
#3 2011-11-25 21:40:21
- lithium002
- Member
- Registered: 2011-04-29
- Posts: 25
Re: Passing a function's $atts array to another function?
That was so helpful! Thanks a lot Gocom. :)
Offline