Solved

Including global text/code in a Razor view engine

Posted on 2014-03-05
9
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 

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
 
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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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