Developer forum

Forum » Templates » Razor If/else on variable

Razor If/else on variable

Lars Pham
Reply

Hi..

I have this code.

@{
    var groupID = "GROUP1";
    var groupIDz = "<!--@Snippet(groupID)-->".ToString();
}

<span>@groupID</span>
<span>@groupIDz</span>

@{
    if (groupID == groupIDz){
        <text>Ens</text>
    }
    else{
        <text>Forskellig</text>
    }
}

@groupID and @groupIDz both contains "GROUP1" as seen in attached thumbnail.

But the if sentence says that they are not same. Is my syntax off or what? 

 

Skærmbillede_2014-11-10_kl._12.22.18.png

Replies

 
Mikkel Ricky
Reply

Are you sure that @groupIDz does not contain any whitespace?

Using a snippet like you do looks like a bit of a hack. What are you trying to do?

Best regards,
Mikkel

 
Lars Pham
Reply

Yes, there's no whitespace in @groupIDz. 

I'm trying to reach the product's primary groupID from a Item publisher module to output relevant items. 

 
Lars Pham
Reply

This is where I declare the snippet (from product page template):

@{
     var groupID = GetString("Ecom:Product.PrimaryGroupID");
}
<!--@SnippetStart(groupID)-->@groupID<!--@SnippetEnd(groupID)--> 

 
Mikkel Ricky
Reply

The issue is that the template is processed by Razor before snippets are processed (snippets are processed right before the complete page is sent to the client). Therefore, your if condition actually reads

"GROUP1" == "<!--@Snippet(groupID)-->"

which is always false.

Are you using the item publisher on a product details page? In that case you can use the Ecommerce API to get the actual product (using the ProductId query parameter; see Dynamicweb.eCommerce.Products.Product.GetProductByID) and get the primary group from the product itself.

Best regards,
Mikkel

 

You must be logged in to post in the forum