# modify Association created by FormFunction

**URL:** <https://community.wolfram.com/t/modify-association-created-by-formfunction/2698>\
**Category:** Questions\
**Created:** [July 17, 2014, 7:43am UTC](https://community.wolfram.com/t/modify-association-created-by-formfunction/2698 "2014-07-17T07:43:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ulricharndt](https://avatars.discourse-cdn.com/v4/letter/u/4bbf92/32.png) [@ulricharndt](https://community.wolfram.com/u/ulricharndt)\
**Post date:** [July 17, 2014, 7:43am UTC](https://community.wolfram.com/t/modify-association-created-by-formfunction/2698/1 "2014-07-17T07:43:16Z")

</div>

Hi,

I want to create a FormFunction dynamically which means the number and the names of variables asked for in the FormFunction can change.  
Therefore I would need to have direct access to the Association created by the FormFunction, for example to Apply it’s values to an StringTemplate. So far I was not able to figure out how to access this.

Any help is highly appreciated.

Ulrich

---

<div class="post-metadata">

**Author:** ![ulricharndt](https://avatars.discourse-cdn.com/v4/letter/u/4bbf92/32.png) [@ulricharndt](https://community.wolfram.com/u/ulricharndt)\
**Post date:** [July 21, 2014, 2:49pm UTC](https://community.wolfram.com/t/modify-association-created-by-formfunction/2698/2 "2014-07-21T14:49:03Z")

</div>

At least I came up with something with will do the trick - whereas I was hoping for something more elegant…  
I generate the form function fields and the association as strings.  
Then I StringReplace these in a FormFunction string and call ToExpression and Evaluate on this.  
It is working but would be interested in better solutions…

tmpl1 = “Today is `Weekday` and tomorrow it will `Weather`”;  
tmpl2 = “My hous is build with a `HousePart1` and a `HousePart2` and   
will have `GardenPart` in the garden”;  
tmplbase = tmpl2;  
placeHolder =  
Union[StringCases[tmplbase, Shortest[“`" ~~x__~~"`”] → x]];  
fffields = {“"a” \<\> ToString[#] \<\> “"”,  
“"” \<\> placeHolder[[#]] \<\> “"”} → “String” & /@  
Range@Length@placeHolder;  
astxt = “"” \<\> placeHolder[[#]] \<\> “" → #a” \<\> ToString[#] & /@  
Range@Length@placeHolder;  
txt =  
"  
FormFunction[\<\<\>\>,  
Module[{tmpl,as},

```
				tmpl=StringTemplate[tmplbase];\[IndentingNewLine] \

```

as=Association[\<\<\>\>];[IndentingNewLine]   
ExportForm[Print[TemplateApply[tmpl,as]],"String"];  
]&  
]  
“;  
txt = StringReplace[  
txt, {”\<\<\>\>" → ToString[fffields],  
“\<\<\>\>” → ToString[astxt]}];  
Evaluate[ToExpression[txt]][];

---

<div class="post-metadata">

**Author:** ![dreiss](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dreiss/32/29094_2.png) [@dreiss](https://community.wolfram.com/u/dreiss)\
**Post date:** [July 21, 2014, 3:51pm UTC](https://community.wolfram.com/t/modify-association-created-by-formfunction/2698/3 "2014-07-21T15:51:31Z")

</div>

I am interested to see what you came up with: could you format your code using the formatting tools?  
Thanks,  
David

---

<div class="post-metadata">

**Author:** ![ulricharndt](https://avatars.discourse-cdn.com/v4/letter/u/4bbf92/32.png) [@ulricharndt](https://community.wolfram.com/u/ulricharndt)\
**Post date:** [July 21, 2014, 6:29pm UTC](https://community.wolfram.com/t/modify-association-created-by-formfunction/2698/4 "2014-07-21T18:29:04Z")

</div>

Sorry, I pasted it into the code section but it looks like many things have not been present corrently.  
I attach now an notebook with the example code.

Ulrich
