You could use global variables but I’m not so sure that’s a good idea.
Or you can outsource the page finding logic to a function, then use the same function in the template (won’t work if you need to pass parameters that are only known in the snippet).
I’d probably use a controller (or if you need the snippet in multiple controllers, a shared controller) for the logic and then pass the value to the snippet.