The question is what functionality are you trying to achieve by this keyword. Depending on what you are trying to do you can use an extension method on HtmlHelper so that you can do something like @Html.YourKeyword , that is pretty easy.
If you want @whatever, then you need to do a little more work but it is possible.
Until you can provide more specific detail of what functionality you want from @whatever, no one will be able to explain how you can go about getting it.
if you want a helper like a panel (or say the htmlform helper) , which outputs some htlm, then arbatraty markup, then ending html (say </form>) you have you helper implement Dispose (and output the trailing html in the dispose method). then in the the markup
you use a using statement (just like the form)
@using (Html.myhelper()) {
//
<div></div>
}
you write Html helpers, becuase they are passed the view context which allows direct output of html, and access to the model.
they are not passed to section either. section creates a view context. the code inside a section writes to the view context stream. then when a section output, it just copies the stream.
there is no notion of nested controls like in webforms. there is just an output stream that htmlhelper write to. so helpers either write directly to the view context stream, or return a string that is written to the stream. for example @model.value is a
shortcut for:
Imugly
Member
10 Points
11 Posts
How to add custom keywords into razor engine
Nov 08, 2012 05:18 AM|LINK
Mikesdotnett...
All-Star
154858 Points
19858 Posts
Moderator
MVP
Re: How to add custom keywords into razor engine
Nov 08, 2012 07:33 AM|LINK
Can you explain what you are trying to achieve? Is it something along these lines: http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Imugly
Member
10 Points
11 Posts
Re: How to add custom keywords into razor engine
Nov 09, 2012 02:55 AM|LINK
i just want to add a key word like what "section" does.
somebody said i can achieve this by modifying CSharpCodeParser.cs. but i don't know how.
any ideas?
CodeHobo
All-Star
18647 Points
2647 Posts
Re: How to add custom keywords into razor engine
Nov 09, 2012 03:11 AM|LINK
The question is what functionality are you trying to achieve by this keyword. Depending on what you are trying to do you can use an extension method on HtmlHelper so that you can do something like @Html.YourKeyword , that is pretty easy.
If you want @whatever, then you need to do a little more work but it is possible.
Blog | Twitter : @Hattan
Imugly
Member
10 Points
11 Posts
Re: How to add custom keywords into razor engine
Nov 09, 2012 03:49 AM|LINK
i do want @whatever, do you have any ideas about how this can be done?
Mikesdotnett...
All-Star
154858 Points
19858 Posts
Moderator
MVP
Re: How to add custom keywords into razor engine
Nov 09, 2012 06:02 AM|LINK
Until you can provide more specific detail of what functionality you want from @whatever, no one will be able to explain how you can go about getting it.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Imugly
Member
10 Points
11 Posts
Re: How to add custom keywords into razor engine
Nov 09, 2012 02:24 PM|LINK
this is what i want to do. i want to create a keyword that works just like @section.
@section whatever extraParams {
// context automatically changes to markup, don't have to say '@'
<div>sidebar</div>
}
bruce (sqlwo...
All-Star
36836 Points
5443 Posts
Re: How to add custom keywords into razor engine
Nov 09, 2012 03:32 PM|LINK
what do you need the {}'s for? you can;
@{
callmethod1();
<div>
@callstringfunction()
<div>
callmethod2();
}
if you want a helper like a panel (or say the htmlform helper) , which outputs some htlm, then arbatraty markup, then ending html (say </form>) you have you helper implement Dispose (and output the trailing html in the dispose method). then in the the markup you use a using statement (just like the form)
@using (Html.myhelper()) {
//
<div></div>
}
you write Html helpers, becuase they are passed the view context which allows direct output of html, and access to the model.
Imugly
Member
10 Points
11 Posts
Re: How to add custom keywords into razor engine
Nov 09, 2012 03:43 PM|LINK
if you use @using (Html.myhelper()) { // <div></div> }
<div></div> will stay there, and it won't be passed into the Html.myhelper
bruce (sqlwo...
All-Star
36836 Points
5443 Posts
Re: How to add custom keywords into razor engine
Nov 09, 2012 07:06 PM|LINK
they are not passed to section either. section creates a view context. the code inside a section writes to the view context stream. then when a section output, it just copies the stream.
there is no notion of nested controls like in webforms. there is just an output stream that htmlhelper write to. so helpers either write directly to the view context stream, or return a string that is written to the stream. for example @model.value is a shortcut for:
ViewContext.Writer.Write(model.value);