Solved

Including global text/code in a Razor view engine

Posted on 2014-03-05
9
383 Views
Last Modified: 2014-03-07
I need to imitate a server side include call in a Razor view engine. The view engine needs to treat the included text as 'dumb' text until it renders the entire view. (Becuase the text/code will be contained in a global area and will not 'know' the variable names contained within it.

E.G. (pseudo code - ignore specific syntax)

<@  Some Razor code.....

       var mylocalvariable = "value specific to this view";

>

<# include   globalCode.txt  -->

<@ Rest of Razor code
.....

>

***********************************

and then in globalCode.txt we will have something that uses that variable. E.G.

   if mylocalvariable = 'blah blah'  print  'blah blah' ;

****************************************


Obviously the real job is much more complicated then this but this trivial example works to get us to where we need. The problem is that Razor does not allow server side includes. And, rendering partial views want to render compiled/compilable objects (not 'dumb' text that only becomes real code after inserted into the local file.

Any idea how this can be done?
0
Comment
Question by:mcorsi62
  • 5
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39909091
I don't completely understand your question, but I would think that you could store the global somewhere, and then pass it to the view in the ViewData or ViewBag dictionaries.
0
 

Author Comment

by:mcorsi62
ID: 39909170
The 'global' data is cshtml code and it is the lions share of the code on the page. The 'local' data for each screen are the values used in the variables in the global data.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39909240
I would normally put the "global" stuff in the _Layout.cshtml page.  The "local" data would be the individual views from the controller methods, that use the _Layout page.  Then, I would pass any needed data from the controller to the views, as needed for each view, either with the Model, ViewBag, or ViewData constructs.
0
 

Author Comment

by:mcorsi62
ID: 39909397
Not sure how I would do that.  Can you use layouts within layouts? We have a master _layout page that applies to the entire site. The pages I am coding use this, but they are partial views that also expose a custom 'kendo' grid. Each grid is different (hence the variables) but all the base code for the grid is the same, hence the need for a 'global' include file - however, not all pages on the site will have a grid and this is a very large project. Asking for a change to the master layout page might be a bit like trying to get a budget through congress.

Can you use layouts within layouts. In other words, the global page with the main menu. I realize even though my view doesn't directly call the layout something is set up where it automatically happens. Can i also call a special layout as well? An 'internal' layout? Or is there only on layout per view?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 39909408
As far as I know, there is only 1 layout page.  Can you give me a small example of what your views look like? I can't quite get a good picture of your problem space.
0
 

Author Comment

by:mcorsi62
ID: 39910222
nvm - I can call a different layout from the global (main) one when I make the call to the view from the controller. So my grid layout will only be used for my grid views and it will contain the global layout plus the grid stuff.

Thanks for all the help! Look for a more difficult (related) question in a bit involving this same building of a view tied in with Kendo grids.

;-)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39910290
"I can call a different layout from the global (main) one when I make the call to the view from the controller."
How did you choose to do that?
0
 

Author Comment

by:mcorsi62
ID: 39912284
Something like this in the controller call to the view will override the global layout:

return View("Index", "~/Views/Shared/_GridLayout.cshtml", someViewModel);
0
 

Author Comment

by:mcorsi62
ID: 39912296
Please look at: 'Carrying data type elements to a layout/kendo Razor call'

for another, more challenging question. I can't seem to figure out how to pass a class (data type object) to the layout. It should be trivial, but it is not.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now