I'm rendering a Razor template and would like to define sections, to which I'm able to add content throughout all included pages (namely, javascripts).
However, all sections so far have come up as empty. If I set them to required: true, I get the error that the section has not been defined. To test this, I added the following code to my main template file:
#section foo {
<p>Hello</p>
}
#RenderSection("foo", true)
These lines are right on top of each other. The rendering of the template fails with the message Section not defined: "foo".
Given that the section is obviously defined, did I maybe miss something in the Project configuration or Controller to enable support for sections? All other #commands (like #RenderBody())inside the template seem to work fine, so some support for the Razor commands is clearly present.
It looks like you're defining your section and trying to render it from your shared layout. As I understand it, you need to call RenderSection in your shared layout and then define the section it your views that use that shared layout.
This link from Scott Gu is a pretty good reference for sections:
MVC 3 Layouts & Sections
Related
I'm trying to make my CSP without unsafe inline.
Since I have to manually check every file from every app, I may as well move the scripts to external files instead of creating a million word CSP entry in the web.config by adding hashes or nounces.
This seems easy enough for client side content, but many templates have razor code in then such as:
<script>
alert(#myVar);
</script>
How can I move this to external?
So in general if you JS needs some input parameters you must of course put them somewhere, and only the razor will know what they are.
The simplest way is still to just have the initial call use the variables - like in your example above. If you have security concerns, doing type-checking in razor should eliminate that for you.
For example, if you do #((int)thing.property) than it simply cannot inject any unexpected payload.
If for some reason you really, really don't want this you can use a attribute-json convention, like
<div class="myGallery" init='{"files": 17}'> gallery contents </div>
and pick it up from the js you created. but this is quite a bit of work, so I would recommend the simpler way.
I would like to be able to trigger build errors / messages based on problems I detect during jekyll builds of my site. Basically the equivalent of what I would use assert for in other contexts. My specific use case is that I construct my site's nav bar by finding all pages whose front matter sets the "show_in_nav_bar" variable set to true. I want to require that these pages set two other variables as well: "title", which I use as the link text in the nav bar, and "nav_bar_order", which is a sortable value that I use to specify the order the links should be displayed. I would like to generate an error and have the build fail if either of these variables are missing. Right now, however, jekyll happily fills in default values, which results in a successful build where the nav bar items are missing (because the text is blank) or mis-ordered.
To make the example concrete, a file with the following front matter should work:
---
title: About
show_in_nav_bar: true
nav_bar_order: 1
---
But this should generate an error like "title and nav_bar_order are required", and cause the build to fail:
---
show_in_nav_bar: true
---
Is there a supported way to do this without using a plugin? I found this page on jekyll talk about how to do this with a plugin, but I'd like to avoid that technique because I'm trying to keep the site as simple as possible, and because I deploy to GitHub Pages, which disallows custom plugins.
I've searched the internet for days now with no luck finding this.
My model has a property which holds a chunk of html containing Razor markup.
exmaple:
public class ViewModel
{
public string Content = "<div>#Html.TextBox(\"UserName\")</div>";
}
In the view, I display that with
#Html.Raw(Server.HtmlDecode(Model.Content).toString())
I need to be able to convert the Razor markup into html, although because the Content is dropped in through the model, the view engine doesn't process it.
I have tried simply dropping in the Content, using just .Raw(Model.Content), .Encode(Model.Content), nothing works.
Any thoughts?
You could use the RazorEngine package which allows you to parse and execute Razor code. This being said I would not recommend you giving your users the power of editing directly Razor templates. You are opening a huge security hole in your website.
There are other templating engines such as DotLiquid for example which are better suited for scenarios where you don't trust user input.
I'm writing template documentation for a wiki and wanted to include a working example of the template. However, I wrote the template to auto-categorize various fields and the entire template itself is also auto-categorized.
This means if I simply call on the template, it will categorize the doc page...and because the actual template page transcludes the doc page, the template page will also be categorized.
Is there a way to prevent these categories from automatically kicking in?
Something like the following should do the trick. Wrap the categorization in your template inside a parserfunction:
{{#ifeq: {{NAMESPACE}} | Help || [[Category:Some_Category]] }}
This sets the category when the template is transcluded onto a page that is not in the "Help" namespace.
Another option is to allow a parameter such as demo to avoid including the category.
If you don't mind being slightly cryptic, you could do the category in the template as {{{cat|[[Category:Some_Category]]}}}; then specifying the parameter as {{my template|cat=}} will prevent the category inclusion.
I'm not sure if I understand the question completely (what is "auto-categorize various fields"?). I am assuming here that you want to show a template "in action" on a documentation page - without attaching some categories (those categories the documentation page usually attaches to articles using this template) to the documentation page.
So
<onlyinclude>[[Category:Some_Category]]</onlyinclude>
will not do the job - as the template is in fact included. Right?
Try passing a parameter categorize=false to the template to indicate that categories are not to be attached in this case:
{{#ifeq:{{{categorize|}}}|false||[[Category:Some_Category]]}}
The double pipe after "false" means: if(categorize==false) then (empty), else [[Category:Some_Category]] - i.e. it is an equivalent construction for if(NOT(categorize==false))...
Good luck and thanks for all the fish,
Achim
I'm using _viewstart.cshtml to automagically assign the same Razor Layout to my views.
It's a dead simple file in the root of my Views folder that looks like this:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
This is more DRY than adding the #Layout directive to every single view.
However, this poses a problem for Razor partial views, because they run the contents of _viewstart.cshtml and therefore incorrectly assign themselves a layout, which makes them, um, no longer partial.
Here's a hypothetical project, showing the _viewstart.cshtml file, the shared _layout.shtml file, and a partial view ("AnonBar.cshtml").
Currently, the way that I'm getting around this is by adding the following line to every partial view:
#{
Layout = "";
}
This seems like the wrong way to denote a view as a partial in Razor. (Note that unlike the web forms view engine, the file extension is the same for partial views.)
Other options I considered but that are even worse:
Putting all partial views into a common folder, so they could share a common _viewstart.cshtml. This breaks the convention of views being in the same folder as their controller.
Not using partial views.
Is this something that is still being fleshed out by the Razor view engine team, or am I missing a fundamental concept?
If you return PartialView() from your controllers (instead of return View()), then _viewstart.cshtml will not be executed.