Developer forum

Forum » Templates » Razor: Include file from ancestor folder

Razor: Include file from ancestor folder

Martin Nielsen
Reply

Hi DW,

I'm trying to nest includes in a Razor file, but i cannot get it to work properly.

My desired include order:

  • eCom/Products/product.cshtml
    • Include: "eCom/Products/includes/product-related-products.cshtml", Tag: @Include("includes/product-related-products.cshtml") <- Works
      • Include: "eCom/ProductList/includes/product-grid-item.cshtml", Tag: @Include("WHAT SHOULD I WRITE?")  <- I can't get this include to work

I tried varies version of include path but i always get the "System.ArgumentException: No template could be resolved with name 'path'" error.

Some of the variations i tried:

@Include("../../productlist/includes/product-grid-item.cshtml")

@Include("../productlist/includes/product-grid-item.cshtml")

@Include("/Templates/ecom/productlist/includes/product-grid-item.cshtml")

@Include("/Files/Templates/ecom/productlist/includes/product-grid-item.cshtml")

Is it not possible to exit the root folder e.g. Ecom/Products in this case?

// Martin


Replies

 
António Ramos
Reply

Try @Include("../Products/includes/product-related-products.cshtml")

If you are inside /ProductList and need to get include from folder /Products you need to rollback one folder and get the correct directory

Best regards,

António Ramos

 
Mikkel Ricky
Reply

You have to use @IncludeFile to include files in your template.

Best regards,
Mikkel

 
Martin Nielsen
Reply

Hi,

Thank you. I tried your suggestions but it still doesn't work

If i use @IncludeFile i get no error message, but nothing is included to my template.

@António, i'm trying to do it the other way around.

 

My folder structure:

  • Templates
    • Ecom
      • Product
        • Includes
          • product-related-products.cshtml
        • procuct.cshtml
      • ProductList
        • Includes
          • product-grid-item.cshtml

File content:

product.cshtml

<div class="related-products">
    @IncludeFile("includes/product-related-products.cshtml")
</div>

product-related-products.cshtml

<ul>
@foreach (LoopItem product in GetLoop("eCom:Related.MostPopularProducts").Take(5))
{
    IncludeFile("../productlist/includes/product-grid-item.cshtml");
    <li>LoopTest</li>
}
</ul>

product-grid-item.cshtml

<li><!--@Ecom:Product.Name--><li>

 

Output when i view my page

<div class="related-products">
    <ul>
        <li>LoopTest</li>
    </ul>
</div>

 

 

 
Mikkel Ricky
Reply

IncludeFile is not a real Razor function. When a template is read, every occurrence of @IncludeFile("…") is replaced with the actual content of the included file (if it exists) and therefore you have to always use @IncludeFile (with @) – even inside a Razor code block. @IncludeFile works exactly like <!--@Include(…)--> does.

This also means that you cannot mix HTML and Razor syntax when including files (Your example above does this, but that may be a typo).

Best regards,
Mikkel

 
Martin Nielsen
Reply

Hi Mikkel,

Can i use the normal dynamicweb include tag (<!--@Include-->) in a razor file?

It's feels wrong to use a tag that that looks like Razor but actually isn't.

I would never have guessed that @IncludeFile is not a razor tag....

 

Also, i don't see where i mixed html and cshtml..

 

 
Mikkel Ricky
Reply

@IncludeFile is a bit of a hack, but it's there to help people using intellisense when editing Razor templates in Visual Studio.

You can use <!--@Include(…)--> in your Razor templates.

Best regards,
Mikkel

 
Kenneth Radoor
Reply

@IncludeFile might be a hack, but it is not a good one, as it slowly kills the server and consumes all memory.

Replacing  @IncludeFile with @Include, cut down rendering time of a page with a factor 50, from 50 sec. to about 1 sec.

/Kenneth

 
Nicolai Høeg Pedersen
Reply

Hi Kenneth

Maybe you could ellaborate a bit - sounds like a nasty bug.

Thanks, Nicolai

 
Kenneth Radoor
Reply

Hi.

The senario is:

A master-template, pretty straight, with a bare minimum of logic, a single menu, and a few includes of snippes of (cs)html. thise snippes has no logic, just there to get a cleaner master template, and to make it easier to make layout changes.

Page-templates, again very simple, mayby with a menu. but no includes.

Paragraph -templates, where the paragraphs are Items. 2 includes, one with some share functions, and one with some shared helpers.

Using IncludeFile, server rendering of any given page toke about 50 sec.

Using Include, cut down server rendering time to about 1 sec.

According to Hostnordic all memory on the server was maxed out, changing to Include, memory consumption dropped to a normal level.

My best guess is the razor templates is getting compiled on each call, using @IncludeFile, and it for some reason is leaking memory. 

 

 

 

 
Nicolai Høeg Pedersen
Reply

Hi Kenneth

Thanks, we will look into it. Can you mail me your templates (Entire design folder)?

BR Nicolai

 
Tom Kamphuis
Reply

Hi guys,

Any follow up on this issue? Can we safely use @IncludeFile or should we use @Include instead? 

Cheers,
Tom

 
Dmitrij Jazel
Reply

Hi Tom, just wanted to share a thought, if we need to include a file, from ancestor folder.

f.eks.: you have ProductList template in eCom. Includes folder - is a sibling to ecom - it is on the same level.

http://screencast.com/t/XQxTTO54F65q

we use:

@IncludeFile("../includes/ListProductsPortrait.cshtml")

and it works.

App version: 8.6.0.2

/Dmitrij

 
René Poulsen
Reply

Hi,

I created another post on including SVGs some days ago: http://developer.dynamicweb.com/forum.aspx?ThreadID=4273 and just saw this post today.

 

In a paragraph template for an item I want to include a SVG file to get the SVG inline. If I just hardcode a path to the file like this, it works:

@Include("/Files/templates/designs/designfolder/images/svgfile.svg")

 

But if I try to include the SVG from a field on the item where I choose the file, it does not work:

@Include("@GetString("Item.Icon"))

This just produces this error: "Include file /Files/templates/designs/designfolder/images/svgfile.svg not found in Templates/designs/designfolder/paragraph/paragrapgtemplate.cshtml

 

The @GetString("Item.Icon") has the exact same path as the hardcoded example that works. I've tried "dotting" (../../.....) myself out of the paragraph folder to the right folder - and this doesn't work either.

Is this not possible at all? Or am I just missing something? :-)

 

 
Dmitrij Jazel
Reply

Hi Rene,

Ok, please corect me if I am wrong, but your path here: @Include("/Files/templates/designs/designfolder/images/svgfile.svg")

And here:Include file /Files/templates/designs/designfolder/images/svgfile.svg

And eventually: (Where are the /Files/???)  Templates/designs/designfolder/paragraph/paragrapgtemplate.cshtml

These 3 are different paths. sure no tipe mistake here?

As last option, I would do something like this:

string svg_path = @GetString("Item.Icon")

@Include(svg_path) or @IncludeFile(svg_path)

This @("@("")") might just be an issue.

 

Lastly, Razor in Dynamicweb is just like Razor in MVC, or other platforms, try something like this:

@Html.Raw(File.ReadAllText(Server.MapPath("~/test.svg")))

More info here: http://forums.asp.net/t/1855306.aspx?Import+svg+with+razor+syntax

 

OR you can also try something like this:

https://msdn.microsoft.com/en-us/library/gg589526(v=vs.85).aspx

 

Hope this pelps

 

/Dmitrij

 
René Poulsen
Reply

Hi Dmitrij,

The first to you mention is the same.

And the last one in the paragraph template is the error i get. I tried copy pasting the path to the file from the error i got - and it works when i hardcode it, so no typos ;-)

It's been some years since I've been developing ind ASP.NET / C# so I'm not that familiar with MVC. I'll try it out. Do I need to be using any namespaces to get this to work?

 
Dmitrij Jazel
Reply

No, no namespeces needed.

In my last link, you can include SVG into HTML template - that should work also in .cshtml templates.

https://msdn.microsoft.com/en-us/library/gg589526(v=vs.85).aspx

Did you try this example?

I am pretty sure you should be able to do that in HTML, there is quite some info about this on the webz :) 

https://www.google.no/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=include+SVG+file+in+html

- Let the force be with you -

/Dmitrij

 
René Poulsen
Reply

Just get these errors right now:

Line 6: The name 'Html' does not exist in the current context
Line 6: The name 'File' does not exist in the current context
Line 6: The name 'Server' does not exist in the current context
 
René Poulsen
Reply

Btw, I already tried the

string svgpath = @GetString("Item.Icon") and used that string in a @Include. It just produces the same error as when I use @GetString("Item.Icon") directly in the @Include.

 
Dmitrij Jazel
Reply

Did you try this?

https://css-tricks.com/using-svg/

 
René Poulsen
Reply

Try what exactly? I just want to be able to choose a SVG file on an item - and get it out in the template with an @Include. I just want it inline - no object tags, no img tags - just pure inline SVG from the file I choose as the icon on an Item (Paragraph) in DW.

It would be really nice if it could be done with @Include - or, as you mention, Html.Raw - but as I mentioend, the Html.Raw just produces theese errors:

Line 6: The name 'Html' does not exist in the current context

Line 6: The name 'File' does not exist in the current context

Line 6: The name 'Server' does not exist in the current context

Maybe it's just some namepaces missing?

 
Dmitrij Jazel
Reply

Well I think we are a bit offtopic in this discussion here, Guys wanted to know how to "Razor include file from ancestor" not how to include SVG file.

Link http://developer.dynamicweb.com/forum.aspx?ThreadID=4273 is not working, not sure why.

So please create another thread for this. And let us continue there... just put the link to new thread.

 

Secondly, I am always using @IncludeFile("../includes/ListProductsPortrait.cshtml") if I want to include whatever... but for you - your hardcoded example @Include("path...") works, that's fine. So can't see what else you can do with it.

 

Namespace is not included, well as far as I found out it uses namespace: System.Web.Mvc wich is most likely not included in Dynamicweb.

Aniways, my way of thought was to - search alternate routes - what if it would be not Dynamicweb, but just a .HTML website, or Web-Forms application.

 

But again, why Object tag is Not OK for you?

please create another thread for this. And let us continue there... just put the link to new thread.

 

 
René Poulsen
Reply

Oops, I deleted one number from the link :-) It's: http://developer.dynamicweb.com/forum.aspx?ThreadID=42730

 

You must be logged in to post in the forum