Dynamic pages with scripts

Hi, I’m having trouble figuring out how to access a function defined inside a template. For instance…

  <div class="page" data-name="home">
          <!-- some content -->

export default () => {

	function foo()

	return $render;

Is there a way to access the function foo defined inside the template ?

What I am actually trying to do is fetching a page template from a server and creating that page dinamically (although still haven’t gotten to that part), problem is that said page will, most likely, have/need some scripting, so I need to figure out a way to load said scripts dinamically as well…

…what would the best course of action here? any thoughts?

My idea so far is fetching the template with functions defined as in the example and then access them directly there…but still can’t access those functions defined inside of the template (and/or know if it’s the propper way to do this).

Thanks in advance =D