These are chat archives for Antaris/RazorEngine

27th
Jun 2016
Paul Lee
@WalkingMist
Jun 27 2016 04:37
@matthid, so there is no way to write methods to dynamically new an instance of the ViewModel from a given key or using the key to get to the template then get to the required ViewModel type?
Matthias Dittrich
@matthid
Jun 27 2016 05:54
Depends on what you mean by no way. You can try to write your own caching provider with a simpler dictionary and ask it later for the type...
there is no way to know the type from a template at least. Consider for example layout templates, they can use whatever type their children is using
so I would say there is no sane way to do it. maybe some heuristic. the realquestion is what you want to achieve...
Paul Lee
@WalkingMist
Jun 27 2016 06:52
@matthid, what I am trying to do is a message template engine, where I would like to have a single method to call and given the Template Key and a dictionary of parameters and be able to construct the final message. So I would like to have a method on the engine where I give it a template key and it will give me a new instance of the correct ViewModel so that I can populate it somehow
maybe it is too complex, so I have changed my mind and now the caller will construct the correct ViewModel type before passing that to the engine
Pauli Østerø
@burningice2866
Jun 27 2016 06:57
Def. the way to go, no need to pass lossly typed dictionary when you can have a real class.
You can even take it a step further and decorate your ViewModel class with an attribute that holds the template key. Then the caller doesn't need to know about that, but just pass the view model and your engine can resolve the template key automatically.
Paul Lee
@WalkingMist
Jun 27 2016 06:59
@burningice2866, that's a good idea!! Thank you.
Matthias Dittrich
@matthid
Jun 27 2016 07:01
What Pauli says. Additionally you can already do it by creating a dynamic object from the dictionary (and use dynamic in your template)
Pauli Østerø
@burningice2866
Jun 27 2016 07:01
I've created several message template engines like that mostly for emails, and that has been my approach.
Paul Lee
@WalkingMist
Jun 27 2016 07:02
@burningice2866, lol, I am trying to make an email message template engine right now
@matthid I am not so sure about dynamic objects, never used it before, maybe it's time to look that up
Matthias Dittrich
@matthid
Jun 27 2016 07:08
yeah use 'null' as your template type (= dynamic, see docs) and for example an expandoobject. Fill it up with your dictionary and it should work as expected...