[Discussion] draft.setTemplateTag( tagName, tagString); // 2.Parameter is String!

I was working on a action yesterday and could not find why my template would crash on using my custom template tag set with draft.setTemplateTag( tagName, tagString);

From @zach script I had a nice code template that set a float value in the template.

Nice!

A quick copy and my dictionary/object was stored in the value.
Buut - the HTMLPreview would not work if I used this custom template with [[tagName]]

24h later I was reading the documentation and found the problem.

the second parameter is string only !

Why?

  1. Java Script converts the float to a string :see_no_evil:
  2. Java Script does NOT convert an object to string :hear_no_evil:

This would need a JSON.stringify( tagString ) and do not forget the JSON.parse( draft.getTemplateTag( tagName ) for back conversion.

My fiddled code snippet to solve it in futureā€¦

Code - read close before consumption
require("tad.js");

class TagsDatabase {
  constructor( p_debug ) {
    this.debug = p_debug;

  }
  
  store( p_tagName, p_value )
  {
  let r_done = false;
  let tagData = JSON.stringify( p_value );
  draft.setTemplateTag(
      p_tagName, tagData);

  let jsonString = draft.getTemplateTag( p_tagName );
  let value = JSON.parse( jsonString );

  r_done = ( jsonString == tagData );
   
  //if(this.p_debug)
  app.TA_msgInfo("done: " + r_done +
   "\ntagName: " +  p_tagName +
   "\nvalue: " + JSON.stringify(value) );
  return r_done;
  
  }
  
  restore( p_tagName  )
  {
  let jsonString = draft.getTemplateTag( p_tagName );
  let r_value = JSON.parse( jsonString );
  
    //if(this.p_debug)
  app.TA_msgInfo(
   "tagName: " +  p_tagName +
   "\nvalue: " + JSON.stringify(r_value) );
  
  return r_value;
  }

}

tags = new TagsDatabase( true );

tags.store( "a", {"b": 13} );
tags.restore( "a" );

Do not let Java Script fool you. :see_no_evil:
Andreas