SOLVED

Tokenizing Picklists?

Go to solution
SanfordWhiteman
Level 10 - Community Moderator

Re: Tokenizing Picklists?

Like Darshil says, Velocity does not apply to LPs. (Although just for the record, Velocity Template Language in general is a way to create web content or any kind of text content — just not in Marketo, where it’s used for emails only.)

 

Your example of using a variable named ${ProgramLanguage} is a bit vague. I’m not sure if you meant that to be a lead characteristic, like a {{lead.token}}, or more like a program-level {{my.token}} that would apply to every asset in a program.

 

You can segment a content block that in turn includes JS (preferably loading a central external JS file via <script src>, not an inline <script> which will be much more confusing to manage). Segments of course work on lead fields.

 

Alternately, if you meant a {{my.token}}, you could output the {{my.token}} into the script source directly, like

<script src="https://pages.example.com/path/to/{{my.language}}.js"></script>

Remember that you need to make sure the {{my.token}} is properly encoded for URL use if you do this. Marketo will not encode it for you.

lianef
Level 1

Re: Tokenizing Picklists?

 Apologies, I'm still new to writing script.
If I wanted to make my form picklist values dynamic via javascript what would that look like?

We built one that does it for all  the field labels and submit button, however it's not picking up the div id tags on our rich text fields, and I also haven't been able to create any logic with the picklist field values.


Another idea I had was leaving the picklist field values off the form and instead try to build logic into the below script but I don't know where to add it. 

If text token {{my.program_language}} = french then vallues populate with french values and english stored, then continue for other languages. 
The program language is set in a text token. 

<script>
MktoForms2.whenReady(function(form) {
	var formEl = form.getFormElem()[0],
		selectEl = formEl.querySelector("#Territory__c");

	(IF {{my.program_language}} = English) 
var optionList = [
		{
			label: "English1",
			value: "ENG1"
		},
		{
			label: "English2",
			value: "ENG2"
		},
		{
			label: "English3",
			value: "ENG3"
		}
	];

	optionList.forEach(function(optionDesc) {
		var newOption = new Option();
		newOption.text = optionDesc.label;
		newOption.value = optionDesc.value;
		selectEl.add(newOption);
	});
(IF {{my.program_language}} = French
var optionList = [
		{
			label: "French1",
			value: "ENG1"
		},
		{
			label: "French2",
			value: "ENG2"
		},
		{
			label: "French3",
			value: "ENG3"
		}
	];

	optionList.forEach(function(optionDesc) {
		var newOption = new Option();
		newOption.text = optionDesc.label;
		newOption.value = optionDesc.value;
		selectEl.add(newOption);
	});
});</script>

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Tokenizing Picklists?

JS if looks like this:

if( condition ) {
  // do something
}

 

Therefore you could use your {{my.token}}, provided the value is allowed within a JS double-quoted string:

if( "{{my.program_language}}" == "English" ) {
  // do something
}

 

However I’d be very unlikely to do it this way. It’s always better to keep JS code out of your LPs and templates.

 

Instead, store the code in a separate .js file you put in Design Studio. Include a different src based on the {{my.token}}, as shown in my earlier post.