Hi, I'm having some trouble desciding what's the best way to do the following, and I'm hoping someone could be of any assistence.
I have a App_Code\Helpers.cshtml that contains (ofcourse) some helper-functions.
For instance, a function that renders a list of blocks for a given list of content-items.
I came a cross of limitations.
I can't use things such as GetPropertyValue() on an IPublishedContent
I don't have access to @Html to use the .Raw() or .RenderPartial() functions.
I need to create an instance of the @UmbracoHelper in every function.
Because this file is compiled on runtime, a single change makes the first page-visit really slow when developing. Even the first page-visit within the backend takes quite some time after an edit.
Just to name a few.
I try to use PartialMacro's when possible, but I think the possible parameter for a PartialMacro are limited. I can't create a PartialMacro that accepts a List<DynamicPublishedContent> for instance. As far as I know.
What would be the best way to use these functions? Any suggestions?
Partial Views seem good. Pretty much the perfect use case for "Given a type of input, render some HTML."
You can also write your own extension methods in a class file: one the one hand, it's a pain to write HTML in a class file but on the other hand, it's compiled so it's fast!
public static class MyHelpers {
public static IHtmlString RenderList(this HtmlHelper html, IEnumerable<IPublishedContent> contentList) {
... //use TagBuilder or StringBuilder to build a string based on the input
}
}
Best practice for re-usable blocks
Hi, I'm having some trouble desciding what's the best way to do the following, and I'm hoping someone could be of any assistence.
I have a App_Code\Helpers.cshtml that contains (ofcourse) some helper-functions.
For instance, a function that renders a list of blocks for a given list of content-items.
I came a cross of limitations.
GetPropertyValue()
on anIPublishedContent
@Html
to use the.Raw()
or.RenderPartial()
functions.@UmbracoHelper
in every function.Just to name a few.
I try to use PartialMacro's when possible, but I think the possible parameter for a PartialMacro are limited. I can't create a PartialMacro that accepts a
List<DynamicPublishedContent>
for instance. As far as I know.What would be the best way to use these functions? Any suggestions?
You should be able to send the list to a Partial like:
if you then in your Partial start with:
Then you should be able to do something like:
That's one way to do it, there are almost unlimited options though!
But that is just a single list. How about adding more parameters? Is that also possible this way?
Of course, that becomes very clean!
Model:
In controller:
View:
Partial View:
Ow, that is nice.
I still need to use the whole MVC process more in umbraco I guess. I would place the "controller"-code above in my view at this point. :)
Thank you, this clears up a lot.
Partial Views seem good. Pretty much the perfect use case for "Given a type of input, render some HTML."
You can also write your own extension methods in a class file: one the one hand, it's a pain to write HTML in a class file but on the other hand, it's compiled so it's fast!
Hm, I've somehow allways seen partial-files as a part of a view. Not as a method of re-using a block of code.
I will have a look at that. Thank you!
is working on a reply...