Are you looking for something like eval? Since the code is compiled you can't do anything like that. You're dealing with a statically typed language (unlike javascript and php). Eval is considered harmful in most languages. Do you have a specific problem
that you are trying to solve?
As David Fowler says, It´s really not recommended to do this. You can use the parsers for Razor to to this though (I have used the parsers myself without problems).
The easiest way to do it is to generate a .cshtml file and save it on the server.
Can you explain your problem?
Mikael Söderström
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
Instead of saving them as a text file you can display them as a view using a custom ViewEngine (this is a blog post I wrote in swedish, but you could read it in english using Google Translate):
Do you have a specific example of what you want to do? I'm sure there is another way to do it. You say it will not be efficient, but I'm not even sure what you are trying to achieve and I'm sure you haven't measured anything.
CyberKral
Member
2 Points
13 Posts
Render dynamic content
Jul 19, 2010 10:23 PM|LINK
Hi all,
Is there a way to render dynamic content like :
@RenderContent("<p>It is now @DateTime.Now.ToString()</p>")
This would be usefull to render content extracted form database.
Thanks
davidfowl
Contributor
2766 Points
624 Posts
Microsoft
Re: Render dynamic content
Jul 20, 2010 07:16 AM|LINK
Are you looking for something like eval? Since the code is compiled you can't do anything like that. You're dealing with a statically typed language (unlike javascript and php). Eval is considered harmful in most languages. Do you have a specific problem that you are trying to solve?
Senior SDE, ASP.NET Team, Microsoft
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Render dynamic content
Jul 20, 2010 08:32 AM|LINK
As David Fowler says, It´s really not recommended to do this. You can use the parsers for Razor to to this though (I have used the parsers myself without problems).
The easiest way to do it is to generate a .cshtml file and save it on the server.
Can you explain your problem?
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
CyberKral
Member
2 Points
13 Posts
Re: Render dynamic content
Jul 20, 2010 11:05 AM|LINK
I want to post dynamic contents from the client, store in the database and render later when the content page is requested.
If I generate a .cshtml file every time I need to render, it will not be efficient.
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Render dynamic content
Jul 20, 2010 11:31 AM|LINK
If I was going to do something like that I should probably write a new View Engine which uses the parsers in Razor to render the text.
You can find a great blog post about how to use the Razor APIs here:
http://thegsharp.wordpress.com/2010/07/07/using-razor-from-a-console-application/
Instead of saving them as a text file you can display them as a view using a custom ViewEngine (this is a blog post I wrote in swedish, but you could read it in english using Google Translate):
http://translate.google.com/translate?js=y&prev=_t&hl=sv&ie=UTF-8&layout=1&eotf=1&u=http%3A%2F%2Fweblogs.asp.net%2Fmikaelsoderstrom%2Farchive%2F2010%2F06%2F17%2Fskapa-en-egen-viewengine.aspx&sl=sv&tl=en
If you don´t want to write a custom ViewEngine you could also create a custom ActionResult:
http://msdn.microsoft.com/en-us/library/system.web.mvc.actionresult.aspx
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
davidfowl
Contributor
2766 Points
624 Posts
Microsoft
Re: Render dynamic content
Jul 20, 2010 03:49 PM|LINK
Do you have a specific example of what you want to do? I'm sure there is another way to do it. You say it will not be efficient, but I'm not even sure what you are trying to achieve and I'm sure you haven't measured anything.
Senior SDE, ASP.NET Team, Microsoft
CyberKral
Member
2 Points
13 Posts
Re: Render dynamic content
Jul 20, 2010 11:05 PM|LINK
I have extended html syntax for my need, here a simplified exemple:
<my-block>
<h1>User profile:</h1>
<my-user>
...
</my-user>
</my-block>
In my asp.net project, the Page_Load of default.aspx contains
string html = Parse(xmlData);
bodyHolder.Controls.Add(new LiteralControl(html));
Can I implement this with the razor syntax?
Thanks.
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">I have extended html syntax for my need, here a simplified exemple:</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><my-block> </div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> <h1>User profile:</h1></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> <my-user></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> ...</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> </my-user></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></my-block></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">In my asp.net project, the Page_Load of default.aspx contains</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">string html = Parse(xmlData);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">bodyHolder.Controls.Add(new LiteralControl(html));</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Can I implement this with the razor syntax?</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Thanks.</div> <div></div>Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Render dynamic content
Jul 20, 2010 11:07 PM|LINK
If you are using your own syntax, I think you should have your own ViewEngine which uses the parser you have.
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
CyberKral
Member
2 Points
13 Posts
Re: Render dynamic content
Jul 20, 2010 11:40 PM|LINK
Thanks Vimpyboy, I will try to write my own ViewEngine.
My second problem is with contents that come from database.
string html = GetHtmlFromDatabase(contentId);
bodyHolder.Controls.Add(new LiteralControl(html));
Is there an equivalent of this in the razor syntax?
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Render dynamic content
Jul 21, 2010 12:17 AM|LINK
You could just print that out. If you don´t want it to be HTML encoded you will need to return it as a IHtmlString, using for example HtmlString:
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy