The use of the following snippet will let you store dynamic sidebar in a variable.

Snippet Source/Credit: Stack Exchange