Developer forum

Forum » CMS - Standard features » IncludeFile bug?

IncludeFile bug?

Bjørn Kamfjord
Reply

In my code, i use IncludeFile to include files....

But, this works:

@IncludeFile("Product.cshtml")

This does not work:

@if (assortmentId.Count() == 0) {
        IncludeFile("Product..cshtml");
    }

This gives me the error:

No overload for method 'IncludeFile' takes 1 arguments

If give another string to the includefile, it does not throw an error, but the file is not rendered....

    @if (assortmentId.Count() == 0) {
        IncludeFile("Prouct.cshtml", string.Empty);
    }

I have tried all sorts off variations to test if there is something wrong with the If statement and other possible issues, all test working as expected. Except the IncludeFile...


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

IncludeFile is not a Razor method but a Dynamicweb directive to include that file. It requires the @IncludeFile syntax.

Note that the includes are processed before the .NET code is run. This means your include file will always be included, regardless of the outcome of the if check. The code that it produces will not be processed though.

Imar

 
Bjørn Kamfjord
Reply

OK.... That makes it quite useless in my case...

Is there another way of doing it, to include a partial only if check = true ?

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

You could put the code in a helper, include the file and then conditionally execute the helper....

Votes for this answer: 1
 
Nicolai Pedersen
Reply

A simple approach is to create a helper or function in the include file and inside your conditional call the helper or method.

BR Nicolai

 

You must be logged in to post in the forum