Sharing variables across blocks

Permalink
I'm not sure if this is possible. I have one block on a page (call it A), in which I hit an API and set a variable value. I have another block(B) on the same page that depends on this variable. Is there a way for B to get access to the variable set in A?

Block A is a zip code lookup, while block B is a listing of the steps during registration. If the zip code isn't found, I set a variable indicating this in Block A (I'm using $this->set("zip_found", true) in the A controller). Block B needs to know if that variable was set (to know whether to advance the step indication).

The other option is for me to hit the API twice, which I don't want to do.

Any ideas?

blakeage
View Replies: View Best Answer
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
Do you mean data saved in the block's database record? or just working variables in php?

If just working variables in php, you can:
- set is as a php global
- create a library or model class with the info as static data
- set it in a user's session (will now be persistent for the user)

If the block data record, you can use c5 to retrieve the block object with its data and interrogate that.
blakeage replied on at Permalink Reply
blakeage
Thanks for responding Sir Fish. I think I may set the registration step as a session variable, and read it from Block B. Although, there is the problem of loading...if Block B for some reason loads before Block A, the step variable won't be accurate (I haven't tried, but it seems that way).
tonidomenech replied on at Permalink Reply
tonidomenech
I use:

echo print_r($global);