-
Notifications
You must be signed in to change notification settings - Fork 32
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
DynamicViewDataDictionary support for ViewBag #35
Comments
PS: the reason one would want this functionality is to optionally bubble up a Title for a page from a template to its containing layout, a fairly common scenario. |
I would advise you against putting such logic in the views. It should be in the model layer which feeds the views. The views should be kept out of logic as much as possible, and stay clear for editing html. |
I'm all for separation of concerns, but I'm not sure that I agree with you in this particular case. Firstly, I don't think that checking whether a value is present is too much work to perform in a view; you see that kind of thing in idiomatic Rails code all the time. But, more importantly, I'm building a static site generator (like Github's Jekyll, or Graze, which is built on Antaris RazorEngine), so most of my HTML pages don't even have their own unique model in the first place (I'm feeding in a model as an ExpandoObject that I read from a global configuration file that specifies certain site details shared across all pages.) The presence of a page in the resulting site isn't determined by anything that happens at a model or controller layer; it is determined by the presence of templates in folders defined by a conventional naming scheme. How else would you propose doing what I describe? I need to set a variable in my template that sets the title of the HTML page. The actual Given that RazorMachine does such an admirable job of replicating the use of Razor in MVC outside of MVC, it seems like supporting ViewBag behavior that corresponds to that in MVC would be consistent with the existing project approach. |
While it isn't ideal, I did find a workaround. In your _viewstart.cshtml file add: Since that gets merged into every razor file before processing it will give the Title a default and you won't get the compile error but individual pages can override the value. Of course you could also just set your default value there instead of null if you wanted to drop the logic from your views. Obviously it requires your layout to contain every ViewBag property you want to be optional in your views. Since it's probably just the properties you use in the _Layout then that might not be too big an annoyance. I'm not saying this is as good as having full support for ViewBag but hopefully it will at least spare you the messy code. Can you provide a URL for Graze? I did some searching and couldn't find it. I made a razormachine based processor to build static sites with (similar to DocPad) so I'm curious if I should just use Graze or continue to use mine. |
@SteveHiner: You can find info on Graze at https://github.com/mikoskinen/graze. I'll probably eventually get around to publishing my static site generator on GitHub. It's done, built on RazorMachine, watches files and auto-reloads the browser on updates, etc., but I need to rewrite my unit tests after a major refactoring effort I just completed, so it's not quite ready to be published. |
@rnorbauer Did you ever publish your static site generator? I never built a file watcher into mine and reloading the browser on changes could be really handy. |
It will be a very late response, but i needed same behaviour from my ViewBag dynamic object because of the same reason. I just expanded the Xipton.Razor.Core.DynamicData class and overrided TryGetValue method as in the following:
If you return true allways and set the |
Actually, there is a shortened version of the implementation:
You don't need to check if invocation of base.TryGetMember(binder, out result) returned true or false. If it returns false then it sets |
In MVC, ViewBags inherit from
DynamicViewDataDictionary
, which means that attempting to call a dynamic property on a ViewBag where that property doesn't exist returnsnull
rather than throwing aRuntimeBinderException
exception.This allows for very simple syntax in templates like:
Is there any way this functionality can be replicated in Razor Machine?
I am currently writing the equivalent code as the following monstrosity:
Perhaps I'm missing something?
The text was updated successfully, but these errors were encountered: