Pages

Wednesday, October 23, 2013

Using #include in a project

Why

The most common need to use an include directive is to include/reuse a common piece of component of the site, such as header, footer and advertisement.

Common Pitfalls

When using a media or image element in place of actual file name, the generated path contains ., which is invalid pathing by default unless enabled in IIS. The other issue with leading . in pathing is one cannot use that in #include virtual.

' Code in CMS template
<!--#include virtual="<%med_transformer_asp%>"-->

' Generated code in SmartEdit or Page Preview.  Invalid code
<!--#include virtual="../ImageCache/521E5D7461214F71889B2A99C6CAD3F4/514EAAB6BEBB4F86B85AF45DB2A09374/TR/transformer.asp"-->
' Code in CMS template
<!--#include virtual="<%anc_transformer_asp%>"-->

' Generated code in SmartEdit or Page Preview.  Invalid code
<!--#include virtual="./PreviewHandler.ashx?Action=Preview&Mode=0&blahblahblahblah"-->

When using #include file directive, the pathing uses \, CMS publish pathing as /. Though this is a minor issue since both syntax are acceptable.

' desired syntax
<!-- #include file="common\header.htm" -->

' generated syntax
<!-- #include file="common/header.htm" -->

The biggest issue with #include file in CMS is that the path returned is incorrect

' Code in CMS template
<!--#include file="<%med_transformer_asp%>"-->

' Generated code in SmartEdit or Page Preview.
<!--#include file="../ImageCache/521E5D7461214F71889B2A99C6CAD3F4/514EAAB6BEBB4F86B85AF45DB2A09374/TR/transformer.asp"-->
' This maps to
' ASP\ReddotTemp\ImageCache\521E5D7461214F71889B2A99C6CAD3F4\514EAAB6BEBB4F86B85AF45DB2A09374\TR\transformer.asp 
' Instead of
' ASP\ImageCache\521E5D7461214F71889B2A99C6CAD3F4\514EAAB6BEBB4F86B85AF45DB2A09374\TR\transformer.asp 
' Code in CMS template
<!--#include file="<%anc_transformer_asp%>"-->

' Generated code in SmartEdit or Page Preview.
<!--#include file="./PreviewHandler.ashx?Action=Preview&Mode=0&blahblahblahblah"-->
' This maps to
' ASP\ReddotTemp\BC498EE1113D41F7AC877F3D5BD0738E\PreviewHandler.ashx?Action=Preview&Mode=0&blahblahblahblah
' The path is correct, but the file gets deleted as soon as it is generated and displayed.

One can work around the pathing issue with manual path correction

<!IoRangePreExecute>
' Code in CMS template
<!--#include file="../<%med_transformer_asp%>"-->
<!/IoRangePreExecute>

' Generated code during publication mode, invalid code, the file is published
' to the asp folder on the web server.  The asp folder is not on the CMS
' server, so preexecution is trying to use a file that doesn't exist
<!--#include file="../asp/transformer.asp"-->

 

Solution

If you simply want to use include to include a static file, like header. In publishing mode, use #include, which uses a anchor placeholder to reference the header page. When not publishing, use the header container placeholder, which references the header page.

<reddot:cms>
<if>
    <query valuea="Context:CurrentRenderMode" operator="==" valueb="Int:2">
        <htmltext>
   <!--#include file="<%anc_header%>"-->
        </htmltext>
    </query>
    <query type="else">
        <htmltext>
            <%con_header%>
        </htmltext>
    </query>
</if>
</reddot:cms>

If you want to include an asp library and use the library during SmartEdit, Page Preview, and Publishing, reference the absolute path via #include virtual directive

<!IoRangePreExecute>
' Code in CMS template
<!--#include virtual="/cms/plugins/asplib/transform.asp"-->
<!/IoRangePreExecute>