Is it possible to pass js variable to template file

Put you JS and PFP code in same file and try. Try to use some hidden fields in template file to set any value from JS, using the ID or name of the hidden field.