Hi Nathan,
First, I would take the logic out of the view. Then on the page type with the classes, I would add something like this (Class1 & Class2 are string properties on your model):
[Ignore] public virtual string CssClass => $"{Class1}{Class2}";
Then in your view, you would just do: <h1 class="@Model.CurrentPage.CssClass">Episerver Rules!</h1>. The cool thing about this is you can add as many string as you want to CssClass and it will only render the ones that are not null. If they are all null, it simply adds <h1 class></h1>. I hope this helps!
-John
Avoid adding properties to ContentType for computed properties (even with ignore) Use custom viewmodel for that instead or you risk getting some very nasty bugs. That object is cached in background so it's basically a global static variable in that case.
Thanks for the info Daniel! After reading up a bit on the perils of global static variables, it makes total sense. So, here is my refactored approach, let me know what you think:
Lets say the individual class strings are on my page type NewsPage. Here is my custom viewmodel:
public class NewsPageViewModel : IPageViewModel<NewsPage> { public NewsPage CurrentPage { get; set; } public IEnumerable<PostedComment> CommentList { get; set; } public bool HasCommentPublishAccess { get; set; } public bool CommentFolderIsSet { get; set; } public string CssClass { get; set; } public NewsPageViewModel(NewsPage currentPage) { CurrentPage = currentPage; CssClass = GetCssClass(currentPage); } private string GetCssClass(NewsPage page) { return $"{page.Class1}{page.Class2}"; } }
Now, in my NewsPage view, which is using NewsPageViewModel as the model, I can do this:
<h1 class="@Model.CssClass">Episerver Rules!</h1>.
-John
For if Model.Class is not null, add the string value for Model.Class inside a class="" - I'm trying to output this: class="class1 class2"
The problem is that I want to add quotes in case there are multiple class names. EpiServer isn't letting me do this. Here's what I've tried...
Renders class="class1 class2"
Renders the single quote as "
Renders the same as #1 above.
Is this possibly an issue with my local version of Epi? I'm not a programmer and I don't know how Epi was set up for my company. Is there something else I should try? Thank you!