Load a file within a template & use variables from template

The basics of how to develop, test, and use models.
Post Reply
neuromau
Posts: 97
Joined: Mon Apr 20, 2009 7:02 pm

Load a file within a template & use variables from template

Post by neuromau »

Hello, I have a template that defines some variables within it. Within the template, I want to load a hoc file that references those variables (about 160 lines long). How can I do this?

Here is a very simple example of what I want to do:

1. Load in the template definition in this file called testclass.hoc:

Code: Select all

begintemplate myclass
public mystring
strdef mystring

proc init() {
	set_string()
	use_string()
}

proc set_string() {
	mystring="hello brain"
}

proc use_string() {
	load_file("myfile.hoc")
}
endtemplate myclass
Note that the file called "myfile.hoc" contains only:

Code: Select all

print mystring
When I load the template definition and then enter:

Code: Select all

 objref c
c = new myclass()
I get the error "undefined variable mystring".

How can I include a file within my template that knows the variables defined within the template?
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Load a file within a template & use variables from templ

Post by ted »

neuromau wrote:How can I include a file within my template that knows the variables defined within the template?
Use xopen instead of load_file. That is, in your template replace
load_file("myfile.hoc")
with
xopen("myfile.hoc")
and presto!
objref foo
foo = new myclass()
will work without error (however, every time you create a new instance of myclass, your code will read myfile.hoc).

From which I infer that xopen() is executing in the context of the object, but load_file is executing at top level--but I don't know why this is the case.
neuromau
Posts: 97
Joined: Mon Apr 20, 2009 7:02 pm

Re: Load a file within a template & use variables from templ

Post by neuromau »

Great, thanks! I didn't realize the difference in context of these two functions either. I just tried xopen in my actual code and it is working fine now.
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: Load a file within a template & use variables from templ

Post by hines »

load_file literally executes xopen at the top level of the interpreter whereas xopen itself when called from the interpreter will carry out its work in whatever context happens to be
in effect. The load_file behavior is appropriate in this regard since it is intended for files which are to be loaded only once (e.g because they contain templates) and need to be searched for in
a sequence of standard places. Putting an xopen in a procedure in a template can be dangerous if the file that is xopened declares any name that does not already exist in the template.
This is because the name space of the template is fixed when it is first interpreted prior to constructing any instances of the template. If the xopen in the template is not in a function or procedure
but at the "top level" of the template, all the contents will become part of the template when the template is interpreted. If the xopne is in a function of the template, then the xopen will be executed
in the context of the object instance when the function is executed. No other instance will have its varialble values changed.
neuromau
Posts: 97
Joined: Mon Apr 20, 2009 7:02 pm

Re: Load a file within a template & use variables from templ

Post by neuromau »

Okay, thanks for the detailed explanation. Currently I call the xopen within the init function, so it is executing during the creation of each instance, in the context of the instance. This works fine for me right now. I will think of whether I can rearrange things so as to call it at the top level of the template so that it just runs once during the definition of the template (if I am understanding this correctly), to reduce the number of times the file must be read.
Post Reply