Copied to clipboard

Flag this post as spam?

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


  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Dec 07, 2015 @ 15:31
    Frans de Jong
    0

    Strange razor syntax error

    I'm always having a weird issue with razor in umbraco. I found a solution but it is ugly and I just want to know if I'm doing something wrong...

    If I for instance want to use this code in a partial view:

     string countryCode = @Umbraco.Content(language.Id).countrycode;
     string url = "";
     if (@countryCode == "nl")
     {
           url = "/";
     }
     else
     {
           url = "/" + @countryCode;
     }
    

    I's missing a bracket }

    If I add a empty span or even in the code between the strings and the If-else block it works without an error.

    example:

     string countryCode = @Umbraco.Content(language.Id).countrycode;
     string url = "";
     <span></span>
     if (@countryCode == "nl")
     {
           url = "/";
     }
     else
     {
           url = "/" + @countryCode;
     }
    

    Is this normal? Am I doing something wrong or is it a bug? I've been using this solution for a year now but it annoys me to have to use it without knowing why...

    Frans

  • Comment author was deleted

    Dec 07, 2015 @ 15:34

    Not sure I follow what you mean, so the first one gives an error? And adding the empty span get's rid of that error?

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Dec 07, 2015 @ 15:38
    Frans de Jong
    0

    The last example gets rid of the error.

    If I declare variables, strings or whatever in a codeblock and follow with a switch or a if statement it throws a error on the front-end and in Visual studio about a missing bracket.

    In this example if I don't use the span Visual studio intellisense stops working at else.

  • Comment author was deleted

    Dec 07, 2015 @ 15:46

    And it's all surrounded in a code block so @{} ? What is the error you get?

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Dec 07, 2015 @ 15:52
    Frans de Jong
    0

    Error

    In this case its wrapped in a

    @foreach(){ }

  • gary 385 posts 916 karma points
    Dec 07, 2015 @ 16:15
    gary
    100

    Hi

    It's probably the @ on the variable countryCode.

    If you are in a code block, ie your for each the variable should be found without the @, effectively this breaks the code block.

    The span, ends the @countryCode first block, so throws no error, but the second @countryCode breaks the previous, hence you have an extra curly bracket on the end.

    Ok, tried to explain, but sounds really stupid.

    Try it without the @ on countryCode, should clear it up

    Regards

    Gary

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Dec 07, 2015 @ 16:48
    Steve Morgan
    1

    Gary I think you've done a great job of explaining it!

    I always think of "entering code mode" - either a @ or a @{ will start code mode.

    When you're in a @{ section you'll "exit" code mode if you open an html tag.. but re-enter it when you close the tag.. confusing.. yeah just a bit!

    So in short, don't use @ on variables in the middle of code blocks.

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Dec 07, 2015 @ 16:48
    Frans de Jong
    0

    Nice catch Gary,

    I saw I use the @ in every project. on every variable I declare.

    I'll try it tomorrow when I'm at work.

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Dec 08, 2015 @ 07:35
    Frans de Jong
    0

    And of course that was the solution.

    So If I'm in a codeblock I can't use a @ unless I break the codeblock with HTML than I have to use a @.

    Thanks a lot Gary. You've made my week :P

Please Sign in or register to post replies

Write your reply to:

Draft