Reputation: 3844
I'm using RazorEngine to build an email templating system and would like to maintain helper functions in separate cshtml files from my main templates. For instance, say I have a folder structure like this:
/Templates
Support.cshtml
Registration.cshtml
Marketing.cshtml
/Helpers
/Shared
Common.cshtml
Images.cshtml
/Marketing
Ads.cshtml
Products.cshtml
What I'd like is for the templates to access the helpers like so:
<body>
@Common.Header()
<img src="@Images.Resize(@Model.ImageUrl)" />
</body>
But the reason I'd like them namespaced is because I'd like say the Ads team to be able to write and maintain helpers without worrying about naming conflicts with say the common helpers or any other team. They could confidently name a helper Resize()
without worrying about @Images.Resize
because their helper would be accessed as @Ads.Resize
or @Marketing.Ads.Resize()
.
This seems somewhat possible based on how helpers work in the native implementation of Razor (https://learn.microsoft.com/en-us/aspnet/web-pages/overview/ui-layouts-and-themes/creating-and-using-a-helper-in-an-aspnet-web-pages-site) but our codebase is distributed and therefore doesn't have an App_Code folder, and also RazorEngine is different in a lot of ways so I haven't been able to get it to work.
Right now I've been getting my helpers into the templates by using string concatenation which also seems wrong, so is there a way I can make this work with RazorEngine?
Upvotes: 4
Views: 576
Reputation: 51
I suggest to move your code under Helper folder to App_Code:
1. Right clicking on your project (not solution) > Add > Add ASP.NET folder > App_Code
2. Create your template by using @helper
syntax:
@helper MakeNote(string content) {
<div class="note"
style="border: 1px solid black; width: 90%; padding: 5px; margin-left: 15px;">
<p>
<strong>Note</strong> @content
</p>
</div>
}
3. Use the template that you created previously by declaring @MyHelpers.MakeNote("My test note content.")
<!DOCTYPE html>
<head>
<title>Test Helpers Page</title>
</head>
<body>
<p>This is some opening paragraph text.</p>
<!-- Insert the call to your note helper here. -->
@MyHelpers.MakeNote("My test note content.")
<p>This is some following text.</p>
</body>
</html>
For more info, you can visit this link
Upvotes: 0
Reputation: 3685
Razor pages are compiled in to simple classes and helpers becomes methods of these classes so you cannot put them under a namespace.
But, if you do not mind putting your cshtml files in a different class library project, then it is possible using an extension called "Razor Generator".
Write following text on first line of the file.
@* Generator: MvcHelper GeneratePrettyNames : true *@
place some business critical code in file
@helper PlaceAd() { advertisement }
In file properties, set "Custom Tool" to "RazorGenerator", and "Custom Tool Namespace" a namespace you would like
reference this new dll from web application.
Reference your new business critical helper from any view
Profit
Upvotes: 0