Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an @functions code block in a razor file do, and when (if ever) should I use it?

Whilst looking at a theme I downloaded from the Orchard CMS gallery, I noticed that a Layout.cshtml file had this block of code at the top of the file:

@functions { // To support the layout classifaction below. Implementing as a razor function because we can, could otherwise be a Func<string[], string, string> in the code block following. string CalcuClassify(string[] zoneNames, string classNamePrefix) {     var zoneCounter = 0;     var zoneNumsFilled = string.Join("", zoneNames.Select(zoneName => { ++zoneCounter; return Model[zoneName] != null ? zoneCounter.ToString() : ""; }).ToArray());     return HasText(zoneNumsFilled) ? classNamePrefix + zoneNumsFilled : ""; } } 

I know what the declared function does (calculates which zones are populated in order to return the width of each column), my question is- what is the correct use of the @function block, and when should I ever use it?

like image 712
Chris Payne Avatar asked Dec 20 '12 15:12

Chris Payne


People also ask

What is the function of a Razor code block?

Razor code blocks Use this approach to render HTML that isn't surrounded by an HTML tag. Without an HTML or Razor tag, a Razor runtime error occurs. The <text> tag is useful to control whitespace when rendering content: Only the content between the <text> tag is rendered.

What are code blocks in views Razor?

Code block is used to enclose C# code statements. It starts with @ (at) character and is enclosed by {} (curly braces). Unlike expressions, C# code inside code blocks is not rendered.

What are Razor expressions used for?

Razor Expression Encoding Razor provides expression encoding to avoid malicious code and security risks. In case, if user enters a malicious script as input, razor engine encode the script and render as HTML output.

What is Razor view in C#?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine.

What is the difference between @code and @functions for razor components?

For Razor components, @code is an alias of @functions and recommended over @functions. More than one @code block is permissible. The @functions directive enables adding C# members (fields, properties, and methods) to the generated class:

What are the rules of Razor syntax in C?

Main Razor Syntax Rules for C#. Razor code blocks are enclosed in @ { ... }. Inline expressions (variables and functions) start with @. Code statements end with semicolon. Variables are declared with the var keyword. Strings are enclosed with quotation marks. C# code is case sensitive. C# files have the extension .cshtml.

How do Blazor templates define their razor components?

Blazor templates define their Razor components using this approach. C# code is placed in a code-behind file defined as a partial class. The following example shows the default Counter component with an @code block in an app generated from a Blazor template. HTML markup, Razor code, and C# code are in the same file:

How is the razor page code executed?

The C# code is executed on the server, and typically results in dynamic content being included within the response sent to the browser. Although not recommended, it is possible to develop Razor Page applications that rely solely on content pages.


1 Answers

The @functions block lets you define utility functions directly in the view, rather than adding them as extensions to the @Html helper or letting the controller know about display properties. You'd want to use it when you can meet these conditions:

  1. The functionality is tied closely to the view and is not generally useful elsewhere (such as "How wide do I make my columns").
  2. The functionality is more than a simple if statement, and/or is used in multiple places in your view.
  3. Everything that the function needs to determine it's logic already exists in the Model for the view.

If you fail the first one, add it as a @Html helper.

If you fail the second one, just inline it.

If you fail the third one, you should do the calculation in your controller and pass the result as part of the model.

like image 110
Bobson Avatar answered Sep 20 '22 10:09

Bobson