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[<<<fieldlist>>>,
Module[{tmpl,as},
tmpl=StringTemplate[tmplbase];[IndentingNewLine] \
as=Association[<<<aslist>>>];[IndentingNewLine] \
ExportForm[Print[TemplateApply[tmpl,as]],\"String\"];
]&
]
";
txt = StringReplace[
txt, {"<<<fieldlist>>>" -> ToString[fffields],
"<<<aslist>>>" -> ToString[astxt]}];
Evaluate[ToExpression[txt]][];