-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how can I reference other assemblies and use inject? #6
Comments
Also having issues making use of @using directives. |
If you need to inject something in text template or reference other assemblies, probably you are overengeneering. Its not a RazorEngine responsibility go get service instances for you. Making Inject directive work would require IoC container to be built in. LocalizerIntended use public class MyTemplateBase : RazorEngineTemplateBase
{
private IStringLocalizer localizer;
public void Initialize(IStringLocalizer localizer)
{
this.localizer = localizer;
}
public string Localize(string key)
{
return this.localizer[key];
}
} string templateText = @"
<h1>@Localize(""Header"")</h1>
";
RazorEngine razorEngine = new RazorEngine();
RazorEngineCompiledTemplate<MyTemplateBase> compiledTemplate = razorEngine.Compile<MyTemplateBase>(templateText);
string result = compiledTemplate.Run(instance =>
{
instance.Initialize(this.localizerInstance); // get instance from whatever source
}); |
Referencing assembliesUsings in order to work require assemblies to be referenced on compilation phase RazorEngine razorEngine = new RazorEngine();
RazorEngineCompiledTemplate compiledTemplate = razorEngine.Compile(templateText, builder =>
{
builder.AddAssemblyReferenceByName("System.Security"); // by name
builder.AddAssemblyReference(typeof(System.IO.File)); // by type
builder.AddAssemblyReference(Assembly.Load("source")); // by reference
});
string result = compiledTemplate.Run(new { name = "Hello" }); |
Maybe it is possible to use default DI for .net core? |
Can you tell what exactly are you building so I can understand better? |
I would like to have possibility to to register engine in DI and use inject IStringLocalizer in my razor template without RazorEngineTemplateBase |
+ for adding injection. // ViewComponent1.cshtml
// C# on Server
With inject I could do deeper composition.
What I am sending is not an empty or static template. The fields are generated dynamically. It is a fully populated view with a custom "ViewController" and multiple "custom" models that can be sent back to the server. Inject is what I use to compose these components in real time. |
@Randy-Buchholz How many builder services you have? My first thought is to make them avaiable by adding in TemplateBase class. |
There is no set number of builders. Each business domain provides a set of builders. For example, the Shipping domain might have builders for Ground Shipping and Air Shipping. If someone selects "Ground" a fragment is built and sent to the browser to populate a "Shipping" section of the page. "Ground" may have a builder for Carriers that would build fragments based on individual shippers. What I am building is a framework to manage the creation and delivery of the fragments. I can render single-level Razor without problems and then put them together. But that gets messy. This may not make a lot of sense without knowing more, but conceptually I'm doing something like this:
Right now I render each one individually and merge them |
Your point is clear. Well primary issue for me is that Generally speaking I do not want to tie RazorEngineCore to particular IoC container unless there is no other choice. |
I tried to use localization in razor:
I added reference to my project. Result:
Unable to compile template: skvbsxxk.c3i(3,17): error CS0234: The type or namespace name 'Extensions' does not exist in the namespace 'Microsoft'
When I add any other reference I get the same result. Can I manage this?
The text was updated successfully, but these errors were encountered: