These are chat archives for Antaris/RazorEngine

24th
Apr 2015
Richard Bogle
@Boggin
Apr 24 2015 13:08
Why am I seeing the signature for Razor.RunCompile as RunCompile(ITemplateKey, TextWriter, Type, object, DynamicViewBag)? I've RazorEngine v.3.6.5 targetframework=net40 but that shouldn't be having this affect. I can't find RunCompile docs and the QuickStart doesn't match at all. What can I do to get this working?
Matthias Dittrich
@matthid
Apr 24 2015 13:22

The quick intro states:

The RunCompile method used here is an extension method and you need to open the RazorEngine.Templating namespace.

I'm not sure what else I can do to make it more visible :)

All examples have the // using RazorEngine.Templating; // Dont forget to include this. comment as well
Richard Bogle
@Boggin
Apr 24 2015 13:24
There you go. Thanks! That'll be my instinct for not reading the (usually misleading) comments letting me down! Why is it commented out in the code snippets, though?
Actually, even with that in I still get the wrong signature ...
Matthias Dittrich
@matthid
Apr 24 2015 13:25
Because those code snippets are in-method code and using is only valid at the top of the file and not within a method but we could change that, and because in most cases the IDE should suggest opening the namespace as well...
Richard Bogle
@Boggin
Apr 24 2015 13:26
And Resharper has greyed out the using directive saying it's not required.
Matthias Dittrich
@matthid
Apr 24 2015 13:27
Hm
Matthias Dittrich
@matthid
Apr 24 2015 13:34
There is really no magic, just normal extension methods and they are included in all builds...
can you factor out a standalone repro console app?
Richard Bogle
@Boggin
Apr 24 2015 13:35
Yeah, I can see them being used in the source code but I can also see something with the signature I'm seeing in src/source/RazorEngine.Core/Templating/RazorEngineServiceExtensions.cs . That seems to be what I'm picking up instead of the method that I'm expecting.
Matthias Dittrich
@matthid
Apr 24 2015 13:38
I'm not sure I understand the problem. Is it at least working with non-extension method syntax (IE just calling the correct static method yourself)?
Richard Bogle
@Boggin
Apr 24 2015 13:39
How do I get to the correct static method? I'm calling Engine.Razor.RunCompile() and all I ever see is the signature that require an ITemplateKey as the first param.
Matthias Dittrich
@matthid
Apr 24 2015 13:49
Try to use RazorEngine.Templating.RazorEngineServiceExtensions.RunCompile(Engine.Razor, ...)
Richard Bogle
@Boggin
Apr 24 2015 13:56
Still seeing the same signature. I'll create a small console app, as you suggested, although in a financial beheamoth (e.g. working on Windows XP) so will only be able to cut-and-paste code.
Matthias Dittrich
@matthid
Apr 24 2015 14:07
So you are not seeing the static methods from the RazorEngineServiceExtensions class? Maybe you have problems because of using an outdated C# compiler / Visual Studio? But calling the static methods directly should work on older versions as well...
Richard Bogle
@Boggin
Apr 24 2015 14:08
using System.IO;
using RazorEngine;

namespace ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            const string markup = "@{ Layout = null; } @model dynamic <!DOCTYPE html> <html> <head> <title>Tatooine</title> </head> <body> <div> These are not the @Model.Name you are looking for.</div> </body> </html>";

            Engine.Razor.RunCompile(markup, "Tatooine", TextWriter.Null, new Form() { Name = "droids" });
        }
    }

    public class Form
    {
        public string Name;
    }
}
The using RazorEngine.Templating was removed by Resharper ...
Well, it is Visual Studio 2010 but that's still .Net 4.0.
Matthias Dittrich
@matthid
Apr 24 2015 14:10
Without using RazorEngine.Templating its no wonder that it doesn't work... NO idea what your Resharper is doing...
Richard Bogle
@Boggin
Apr 24 2015 14:11
I've suspended Resharper to get that out of the mix.
Still getting the following error: Error 1 The best overloaded method match for 'RazorEngine.Templating.IRazorEngineService.RunCompile(RazorEngine.Templating.ITemplateKey, System.IO.TextWriter, System.Type, object, RazorEngine.Templating.DynamicViewBag)' has some invalid arguments
Matthias Dittrich
@matthid
Apr 24 2015 14:14
Yes because there is no method with this signature as the compiler is telling you...
Engine.Razor.RunCompile(markup, "Tatooine", TextWriter.Null, typeof(Form), new Form() { Name = "droids" }); should work
or if you are fine with dynamic templates (no static types) you can use
Engine.Razor.RunCompile(markup, "Tatooine", TextWriter.Null, model: new Form() { Name = "droids" }); (without specifying the modelType parameter which is always there and before the model parameter)
or using null: Engine.Razor.RunCompile(markup, "Tatooine", TextWriter.Null, null, new Form() { Name = "droids" }); (the last two are basically the same thing)
Matthias Dittrich
@matthid
Apr 24 2015 14:20
I agree that the Intellisense is really bad for those methods but that's an IDE issue and nothing we can really do about :(
Richard Bogle
@Boggin
Apr 24 2015 14:22
Sorry, I still don't get it. The first param should be a string, shouldn't it? string markup = "<my markup here/>"
It won't compile because it's not an ITemplateKey.
Matthias Dittrich
@matthid
Apr 24 2015 14:23
Do my examples work when adding using RazorEngine.Templating ?
Yes, without extension methods we only have ITemplateKey overload...
Richard Bogle
@Boggin
Apr 24 2015 14:25
Nope. They all have strings as the first param and the compiler is expecting ITemplateKey. It would seem that in VS 2010 & .NET 4.0 those extension methods aren't being seen. Did a previous version of RazorEngine work for the older compilers, do you think?
Matthias Dittrich
@matthid
Apr 24 2015 14:27
Okey now we are on the right track.
So you can probably use the extension methods directly, like this:
RazorEngine.Templating.RazorEngineServiceExtensions.RunCompile(Engine.Razor, markup, "Tatooine", System.IO.TextWriter.Null, null, newForm() { Name = "droids" });
It's basically the same thing but resolved by hand instead of the compiler.
Richard Bogle
@Boggin
Apr 24 2015 14:30
Ha! I thought we'd already tried that but yes, that's the bunny! Hurrah. Thank you so much for your time.
Matthias Dittrich
@matthid
Apr 24 2015 14:32
Glad it worked! I probably add to the docs that VS 2010/Net40 needs to use the extension methods manually (even if those versions are quite ancient).
Richard Bogle
@Boggin
Apr 24 2015 14:34
Heh. Yeah, they are indeed ancient but this financial institution is going to be stuck with them for a looong time! Bit sad, really.