lundi 13 octobre 2014

XTemplate: Inserting a variable into a variable

I'm pulling data from a JSON array and I would like to insert one of the data array items into an XTemplate variable in order to determine a final data array item.

Here's a sample (simplified) JSON data array:



Code:



"questions": [
{
"questionID": "1",
"questionNum": "1",
"questionType": "2",
"argument": "In what year was the Spanish Armada defeated by English naval forces?",
"answer": {
"correctAnswer": "C",
"optionA": "1558",
"optionB": "1571",
"optionC": "1588",
"optionD": "1592",
"optionE": "None of the above"
}
}
]

Here is some pseudocode to try to illustrate what I am trying to do:

Code:



<tpl if="questionType == 2">
<div>{answer.option{values.answer.correctAnswer}}</div>
</tpl>

I'm trying to use 'correctAnswer' to determine the option. Basically if the 'correctAnswer' is "C", I would like the TPL to grab the {answer.optionC} data. I'm just not sure how to insert one value into another. Everything I have tried so far has ended up returning the string '{answer.optionC}' instead of parsing it in the TPL. Any help would be appreciated.



XTemplate: Inserting a variable into a variable

Aucun commentaire:

Enregistrer un commentaire