WordPress change title with custom dynamic

I have created a custom php template file, which I use to filter results, based on user choice. I get the user’s choice with the following code

$  bla=$  wp_query->query_vars['something']; 

So I want to change the wordpress title, instead of the template name inside of wordpress panel, to something like "My template name dynamic- Website name"

I’ve found this

add_filter('pre_get_document_title', 'custom_title'); function custom_title($  title) {      return 'Test New Title'; } 

And it changes the title, however it doesn’t show the contents of the $ bla. If you echo it few lines down the code it works. I believe that is because I have to place this filter before the get_header() of the template file. Any advice?