Copied to clipboard

Flag this post as spam?

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


  • Laurence Gillian 600 posts 1219 karma points
    Jun 20, 2012 @ 13:55
    Laurence Gillian
    0

    Newbie Question - if DocType

    Hello,

    So today, of all days. I look at a problem and say, hey! This is the right use for some sort of inline code, lets do it!

    I want to display some text, but only on the homepage.

    So many logic is

    if doctype = Home
    show this text!

    How does one go about doing this in inline razor?

  • Laurence Gillian 600 posts 1219 karma points
    Jun 20, 2012 @ 14:01
    Laurence Gillian
    0

    I got as far as...

    <umbraco:Macro runat="server" language="razor">
      @Model.NodeTypeAlias
      {
        <h2>@Model.contentHeader</h2>
      }
    </umbraco:Macro>

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jun 20, 2012 @ 14:31
    Jeroen Breuer
    0

    Try this:

    @if(Model.NodeTypeAlias == "Home")
    {
      <h2>@Model.contentHeader</h2>
    }

    Jeroen

  • andy_rose 91 posts 117 karma points
    Jun 20, 2012 @ 14:31
    andy_rose
    0

    Something along the lines of below should work. Razor is clever enough to work out when code stops and mark up begins.

    @if (Model.NodeTypeAlias == "Home")
    {
        <h2>@Model.contentHeader<h2>
    }

     

  • Laurence Gillian 600 posts 1219 karma points
    Jun 20, 2012 @ 14:34
    Laurence Gillian
    0

    Nope doesn't work!

    But the code I posted does work, so the issue is with the @if?

    Very confusing, for such a simple thing ;)

  • jaygreasley 416 posts 403 karma points
    Jun 20, 2012 @ 14:36
    jaygreasley
    0

    Does @Model.NodeTypeAlias work? should it be @Model.GetProperty('NodeTypeAlias').Value() ?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jun 20, 2012 @ 14:36
    Jeroen Breuer
    0

    I've never used inline razor before. Only is .cshtml files. Maybe remove the @ so try this:

    if(Model.NodeTypeAlias=="Home")
    {
     
    <h2>@Model.contentHeader</h2>
    }

    Jeroen

  • Laurence Gillian 600 posts 1219 karma points
    Jun 20, 2012 @ 14:38
    Laurence Gillian
    0

    Okay so, 

    @Jay - yep it works!
    @Jeroen - that didn't work, but the @Model.contentHeader bit did work...

    So I thought, maybe if I add a @ infront of the if...

    and it worked! (:

     

    <umbraco:Macro runat="server" language="cshtml">
    @if(Model.NodeTypeAlias=="Home")
    {
      <h2>@Model.contentHeader</h2>
    }
    </umbraco:Macro> 

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jun 20, 2012 @ 14:42
    Jeroen Breuer
    1

    Isn't your answer the same as my post? Razor is a combination of HTML and C#. If you're using the @ it will enable you to use C# code. This blog might help :) http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx

    Jeroen

  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Jun 20, 2012 @ 14:46
    Warren Buckley
    0

    @Lau glad you figured it out :)
    For the Razor engine to understand you are typing Razor you need to prefix the if with @ so it knows it's code and say not just normal text or HTML.

    I recommend reading over Scott Gu's post on an introduction to Razor so you can pick up the syntax

    http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx
    http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx 

    Cheers,
    Warren 

  • Laurence Gillian 600 posts 1219 karma points
    Jun 20, 2012 @ 15:00
    Laurence Gillian
    0

    Thanks for all the help! Not quite sure what was going wrong, as Jeroen rightly said, you got it earlier! Thanks again, Laux

Please Sign in or register to post replies

Write your reply to:

Draft