Take the 2-minute tour ×
Tridion Stack Exchange is a question and answer site for Tridion developers and administrators. It's 100% free, no registration required.

I'm working with razor templates, I have two if's inside a foreach.

I've attached the simple code. But when I use Template Builder razor doesn't interpret the second if (it's like a text for Razor). If I add a @ before the if, I get an error.

@foreach (var cp in ComponentPresentations) {
    if (cp.Template.Title == "Identification") {                          
        <title>@cp.Component.titlePage</title>

        <head>
            @* THE PROBLEM IS HERE *@
            if (cp.Component.facebook.title != null) {
                <meta property="og:title" content="@cp.Component.facebook.title" >
            }  
        </head>
    }
}

Someone can help me?

share|improve this question
    
And the error is?????? –  Dominic Cronin Feb 19 at 12:13
    
Adding a @ in front of the if should work - what error do you get? It may be getting confused by the lack of closing meta tag –  Will Price Feb 19 at 12:13
    
Think that's it Will. <text><meta....></text> should fix it then. –  Dominic Cronin Feb 19 at 12:15
add comment

1 Answer 1

up vote 4 down vote accepted

You need to use an @ before the if - Razor treats everything as markup until it encounters an @, then it treats it as code, until it finds something it thinks is markup (like an HTML tag) until it finds something like code again (like a closing HTML tag and then a closing brace }). The error you get is probably as Razor doesn't find a closing </meta> tag, so gets a bit confused as to whether the closing brace is markup or code. You can fix this by closing the meta tag:

<meta property="og:title" content="@cp.Component.facebook.title" />

Or, like Dom suggests using a <text/> element

<text><meta property="og:title" content="@cp.Component.facebook.title"></text>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.