I don't recommend building jsons manually
Code: Select all
[r, foreach(Attribute, "['Wisdom', 'Dexterity']"), code: {
I would just use the functions
Code: Select all
[r, foreach(Attribute,json.append("","Wisdom", "Dexterity")), code: {
I often build my output and put it in to a json array. Then I put it in to a normal string with this conversion.
Code: Select all
[H: output = json.toList(outputArray,"")]
I try to keep all my html standard like style="..." rather than using single quotes. Single Quote output that's just text and not script I use the html entity. I can then surround my html with single quotes.
I also use %{varname} in the text to plug in dynamic values and then use strformat to convert. Here are some output creation examples.
Code: Select all
[H: card.template = '<table width="300px" class="tableOut">
<table width="100%" class="tableIn">
<tr valign="top"><td class="col1">Race:</td><td class="col2"><b>%{raceLine}</b></td></tr>
<tr valign="top"><td class="col1">Summary:</td><td class="col2"><i>%{var.summary}</i></td></tr>
<tr valign="top"><td colspan=2 class="bar"> </td></tr>
<tr valign="top"><td class="col1">Ability:</td><td class="col2">%{var.abilityadjustments}</td></tr>
<tr valign="top"><td class="col1">HP:</td><td class="col2">%{var.hitpoints}</td></tr>
<tr valign="top"><td class="col1">Size:</td><td class="col2">%{var.size}</td></tr>
<tr valign="top"><td class="col1">Vision:</td><td class="col2">%{var.vision}</td></tr>
<tr valign="top"><td class="col1">Move:</td><td class="col2">%{var.move}</td></tr>
<tr valign="top"><td colspan=2 class="bar"> </td></tr>
<tr valign="top"><td colspan=2 class="spancol">RACIAL TRAITS</td></tr>
<tr valign="top"><td colspan=2 class="indent">%{traitList}</td></tr>
</table></table>']
Btw, I wrote a macro to convert a json object in to local variables. Wish this was in MT normally like the varsFromStrProp, except for jsons.
Code: Select all
@@ @json.toVars
@PROPS@ fontColor=black ; autoExecute=true ; fontSize=11pt ; sortBy= ; color=pink ; playerEditable=true ; applyToSelected=false ; group=Utility ; tooltip= ; minWidth=94 ;
<!-- json.toVars(obj,prefix): varList
obj - variable is a json object
varList - A json object of prop names to original key word.
This function defines the keys of a json object as local variables with the format prefix ("var") + "." + lowercase keyword with no spaces.
-->
[H: sub.obj = arg(0)]
[H, if(argCount() >= 2): sub.prefix = arg(1); sub.prefix = "var"]
[H, if(json.isEmpty(sub.prefix)): sub.prefix = "var"]
[H: sub.varList = "{}"]
[H, if(json.type(sub.obj) == "OBJECT"), code: {
[H, foreach(varName,sub.obj), code: {
[H: sub.varName = keyToVar(varName,sub.prefix)]
[H: sub.varList = json.set(sub.varList,sub.varName,varName)]
[H: set(sub.varName,json.get(sub.obj,varName))]
}]
}]
[H: macro.return = sub.varList]
!!
I plan to write the other function that takes the local vars and puts them back to the original object structure using the sub.varList object.
oh, forgot the prop name clean up.
Code: Select all
@@ @keyToVar
@PROPS@ fontColor=black ; autoExecute=true ; fontSize=11pt ; sortBy= ; color=pink ; playerEditable=true ; applyToSelected=false ; group=Utility ; tooltip= ; minWidth=94 ;
<!-- keyToVar(keyword,prefix): propName
keyword - any string typically found as a key to json object
prefix - default "var." adds on to keyword to make prop name.
propName - final convertion of keyword to a valid prop name format.
-->
[H, if(argCount() >= 2): macro.return = arg(1) + "." + replace(lower(arg(0)),"[^a-zA-Z0-9_.]",""); macro.return = replace(lower(arg(0)),"[^a-zA-Z0-9_.]","")]
!!