Variable to Template Canvas parameter

I have got to be missing something simple, but I went from a normal window to a template and using template canvas because I had too much to make it fit on one screen (and this gives e a scroll bar). My window had a custom property that all my SQL queries used - an ID. Now with the Template Canvas, if I put in a fixed number the page will show the values for that record. But I can’t figure out how to pass my window’s custom property to the template’s parameter. Everything I type in the Template Canvas Customizer under Parameters --> ID (other than a fixed number) just disappears. I would also like to have this custom property value be changeable on the screen.

1 Like

You will have to dynamically create the dataset, or use cell update bindings, if the repeater has a static configuration.

Sorry, I don’t know what you mean.

The template contains about 20 SQL queries all using the ID for the where clause. But, I don’t know how to get the ID passed in.

Lets say you have a templateParameters dataset as follows:


| id | val1 | val2 |

| 5 | jim | johnson |

| 17 | kyle | chase |

You have to get the ID into the template, through the templateParameters dataset. In the case of the tempate canvas, there is a parameters column that will need to be set. Something like {“id”:1} will have to be generated by the query, or python

So do I put something under scripting for the template canvas that will make the template’s parameter “ID” equal to the Root Container’s ID (Custom property)? (ID is the only parameter I setup on the template.)

I did figure out how to make the template have a field inside of it that the users can edit and get it to show all the data for what they put in, but I want to auto populate it when I call it the first time via the custom property of the Root Container (because I set this from another window where they choose one to look at).

Using the template canvas customizer when you add a template that has parameters it will give you text boxes for each parameter, and will create a new column in the templates property.

In your case you would have one text box for ID (or whatever you’ve given for the name of your ID parameter).

Putting a static value of 1 into the text box will produce a value in the parameters column for that template of: {‘ID’:1}

In order for you to change the ID field dynamically based on some event you will need to modify the parameters column in the dataset by scripting or by returning the field with a query if you have bound the data property in that way.

Scripting

#filter event for the change of a custom property on the template canvas 
if event.propertyName == 'id':
	#get a referrence to the template canvas templates property
	dsTemps = event.source.templates
	
	#itterate throgh the data set, here you can filter for a specific template
	#or update all at once
	for row in range(dsTemps.rowCount):
		dsTemps = system.dataset.setValue(dsTemps,row,'parameters',event.newValue)
		
	event.source.templates = dsTemps

Or you can do it in the intializeTemplate extension function as well.

If you’ve bound the templates property to a query then you will need to return a parameters column with the query, with the value being equal to {‘id’:(rowID)}.

I am feeling so stupid because I cannot figure this out. I don’t know much about scripting. And, I have no idea how to “modify the parameters column in the dataset by scripting or by returning the field with a query if you have bound the data property in that way.”

I did put your script in under initializeTemplate under scripting, and nothing happened. Here is what I have:

Sorry.

Don’t be too hard on yourself, programming can be very cryptic, particularly if you’re new to a language.

If you copied that script exactly into the intializeTemplate extension function, I’m not surprised that it didn’t work. Mostly because the conditional for the if statement would never be true.

Paste it into the property change event scripting for the custom property and see what happens, just be sure that you change the string ‘id’ to the actual name of your custom property.

So that kinda made sense to me - Root Container of my window has the custom property ID. So, I right clicked on Root Container and went to Scripting, and copied the script above to the Script Editor of propertyChange. I changed ‘id’ to ‘ID’ to match the custom property name. Was I supposed to change ‘parameters’? I didn’t and got this error:
Traceback (most recent call last):
File “event:propertyChange”, line 4, in
AttributeError: ‘com.inductiveautomation.factorypmi.application.com’ object has no attribute ‘templates’

Sorry

Since your Root Container has the custom property you will have to point the script to the template canvas component.

change this:

dsTemps = event.source.templates

to something like this:

dsTemps = event.source.getComponent('TemplateCanvasName').templates

Do I need to adjust the second dsTemps statement? Now I get the same error on the last line. Here is what I have:

Yes.

Line 4 gets the value of the templates property, Line 11 sets the value.

1 Like

So I made the same change to the last line, and the error went away. But, the value is still not going thru (the parameter still has the initial value of 0.

I also tried putting a custom property on the canvas, called MyID and linked it’s value to the ID of the Root Container. Then I moved the script from the Root Container Property change to the Canvas Property Change and got the same result of staying 0.

See if I understand this script - event.newValue is supposed to contain the value of ID in the Root Container, and then change the parameter to that value with the last command.

Since the value is being set when you call the page, could I be not seeing it as a change? Maybe we need something in the initializeTemplate?

Okay,

I found my error and it’s a silly one. :rage:

So, the parameters column needs to have a JSONObject for it’s value, and the script which I gave you is only inserting the new value of the ID, which of course the template canvas doesn’t understand.

You understand correctly that event.newValue holds the value of the ID in root container.

We need to make a couple of changes to make this work.

#filter event for the change of a custom property on the template canvas 
if event.propertyName == 'ID':
	#get a referrence to the template canvas templates property
	dsTemps = event.source.getComponent('Template Canvas').templates
    #build a JSONObject to hold the parameter value
	parameters = "{'ID':'" + str(event.newValue) + "'}"

	#itterate throgh the data set, here you can filter for a specific template
	#or update all at once
	for row in range(dsTemps.rowCount):
		dsTemps = system.dataset.setValue(dsTemps,row,'parameters',parameters)
		
	event.source.getComponent('Template Canvas').templates = dsTemps

I believe that should work. My apologies for the buggy script. That’s what I get for not testing it. :pensive:

1 Like

I was wondering if that was what it the parameter text should look like.

Thank you so much! I am finishing this in China and leave Tuesday, so I am so happy to have it all working.

Thanks for this! I've been meaning to figure out a way to pass parameters to the templates, works great!