Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom 713 posts 954 karma points
    Jul 04, 2011 @ 02:58
    Tom
    0

    HTML Raw with @: syntax

    Hi guys.

    still trying to get used to the whole razor syntax just wodnering how I'd Html.Raw something like: @:@Model.pageTitle | Site Name

     

    I've tried @:Html.Raw(@Mode.pageTitle | Site Name) but i just can't get it out..

     


  • Alex 78 posts 136 karma points
    Jul 04, 2011 @ 09:38
    Alex
    1

    Hi Tom,

    @:Hello World! is essentially the same as <text>Hello World</text>

    and

    @Model.pageTitle is the same as doing <%= Model.pageTitle%>

    Basically Razor consist of 2 parsers the Markup parser and the Code parser, both parsers can identify markeup and code but only the respective parsers can render or parse markup or code. These 2 parsers call into each other so form a stack when rendering a page. When a particluar parser gets called it will also know what it's "end" is, so the makrup parser knows that if it encountewrs a <li> tag then the end of that markup block will be a </li> tag. Here is an example of how the stack works with the markup parser (MP) and the code parser (CP).

    <p><-----stack: MP  <-call into markup parser
      @foreach (dynamic node in Model.Childen)<-----stack: MP - CP<-Markup parser calls into code parser
      {
        <a href="#"><-----stack: MP - CP - MP
          My Link at
          @DateTime.Now<-----stack: MP - CP - MP - CP <- the end of this code block is the space after the "Now"
          Back to markup<-----stack: MP - CP - MP
        </a><-----stack: MP - CP
      }<- end of code block so code parser ends
    </p> <-----stack: MP

    I hope that makes a bit of sense, and whilst I believe that is correct it is only my understanding so might be entirely werong! ;-)

    So to anser your question, you should be able to do

    @Html.Raw(Model.pageTitle + " | Site Name")

     

  • Tom 713 posts 954 karma points
    Jul 04, 2011 @ 22:41
    Tom
    0

    Hi Alex,

    thanks for such a fantastic and detailed explanation! yes still very much trying to get the hang of razor! I love how clean it is.. it's just the syntax and when to use certain things that are throwing me.. like the example of the sitemap where you can declare a helper like traverse!

     

    can you also @using somelibrary.web and just call methods using it?

     

    Cheers,

    Tom

  • Alex 78 posts 136 karma points
    Jul 05, 2011 @ 09:36
    Alex
    0

    @Tom: no problem, still learning it myself so it's good to think things through! As far as I am aware you can do pretty much anything that you could do in C# ASP.NET. So yes you can definitely do @using somelibrary.

Please Sign in or register to post replies

Write your reply to:

Draft