Developer forum

Forum » Templates » Dynamic @Inlcude

Dynamic @Inlcude

René Poulsen
René Poulsen
Reply

Hi,

I have a solution where the customer want's to be able to choose among several header templates. They have a developer who creates some of the templates that they wan't to be able to choose among. And they would like to keep it in simple files that they want to include in the master templates. I've created a file-field on the website settings item, where they can choose among files in a "partials > headers" folder I've created in the design folder.

I have a string containing the filenam (headerPartial) and if I try to include the file chosen in the field, this way: @Include("../partials/headers/@headerPartial"), I just get this message:

[Include file '../partials/headers/test.header.cshtml' not found in 'Templates/Designs/website/master/master.cshtml']

If I hardcode the exact same value, this way: @Include("../partials/headers/test.header.cshtml"), it works just fine.

So my simple question: Is it not possible to create dynamicweb includes like this?


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply
This post has been marked as an answer

Hi René,

 

It is not possible because of the way the template engine works. One of the first things it does is to render DW "tags", which Razor/C# known nothing about. So stuff like GetString, GetLoop and Include are processed first, and only when all fo that is parsed, will true Razor or C# be parsed.

 

This means that you cannot include variables within the @Include() statement.

 

Best Regards,

Nuno Aguiar

Votes for this answer: 1
 
René Poulsen
René Poulsen
Reply

Okay, that whas what I thought. Do you know of any other way to do what I want to accomplish - without any "hardcoding"?

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi René,

 

We've tried different approaches over the years and what works best / we use currently is:

  • The include files only have helpers - just load all variations of the partials as individual helpers
    • You can include all partials in a single file or multiple files - it's up to you
  • In the template request the helper you need using if statements or switch-case

 

That will help keep your template readable and clean, while providing a dynamic behavior.

 

Hope that helps,

Nuno Aguiar

 

You must be logged in to post in the forum