USING ALTTEMPLATE

Often times the same content may need to take on different shapes to serve a different purpose. One very common, and timely, example of this is switching the layout of your content for a mobile device. Traditionally, the developer must create a different document type with the same properties, create a duplicate page for the same content, and apply the alternate template to serve up the alternate view.

There are cases when you need to present certain content in multiple formats. In this section, you'll see how data that is presented in one way can be repurposed by using something in Umbraco called AltTemplate.

Umbraco enables you to create and assign multiple templates to a particular document type. Make sure that you're targeting at least one of the same document type properties across the multiple templates; otherwise none of your content will be displayed. You can allow multiple templates for one document type by selecting them in the Info tab of a selected document type, as show in Figure 4-14.

FIGURE 4-14

image

Now that you've assigned all the templates for the document type—your content container—you can dynamically load this template by appending the following to the current URL:

?altTemplate=template2

This causes the current page to reload with the alternate template applied. The beauty of this is that the content author didn't have to create two separate pages and maintain duplicate copies of the content.

A second method to calling up an alternate template is to simply append the template alias to the end of the current URL. For example, if the URL of your page is http://localhost/somepage.aspx you can apply the alternate template by changing the URL to http://localhost/somepage/template2.aspx.

image The value in the key/value pair, template2, is the alias of the template as opposed to the name. Most of the time, these two values are the same—but not always. For example, if you save a template using special characters (space, +, −, and so on), Umbraco truncates the alias by removing the special characters.

Changing the Document Type using AltTemplate

One of the many common uses of AltTemplate is to be able to take content and serve it to the browser as a different content type, thus altering how the user can interact with and use the output from the CMS. Take an example where you want to serve up a table of data in an Excel document as opposed to standard plain HTML. For example:

<table class=“data-table” border=“1”>
    <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Title</th>
        <th>Currently Employed</th>
    </tr>
    <tr>
        <td>John</td>
        <td>Doe</td>
        <td>Director Of Professional Services</td>
        <td>Yes</td>
    </tr>
    <tr>
        <td>Jane</td>
        <td>Plane</td>
        <td>Vice President</td>
        <td>Yes</td>
    </tr>
    <tr>
        <td>Mike</td>
        <td>Vick</td>
        <td>Sr. Engineer</td>
        <td>No</td>
    </tr>
    <tr>
        <td>Roland</td>
        <td>Wolters</td>
        <td>Engineer</td>
        <td>Yes</td>
    </tr>
</table>

Displayed in the browser, this would simply render a table with the data in various columns, decorated with table borders. However, let's make this same data table available as a downloadable Excel file that users can manipulate by sorting, pivoting, or using any of the other standard operations provided by Excel.

Listings 4-4 and 4-5 demonstrate the switching of content type using the altTemplate attribute.

image I've jumped ahead a little here by introducing the concept of macros (covered in Chapter 5). For now, you can assume that this will all work.

LISTING 4-4: ExcelDownload.master

image
<%@ Master Language=“C#” MasterPageFile=“/umbraco/masterpages/default.master”
 AutoEventWireup=“true” %>
<asp:Content ContentPlaceHolderID=“ContentPlaceHolderDefault”
 runat=“server”>
<umbraco:Macro Alias=“ExportTableDataToExcel”
 runat=“server”></umbraco:Macro>
</asp:Content>

LISTING 4-5: ExportTableDataToExcel.xslt

image
<?xml version=“1.0” encoding=“UTF-8”?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp “&#x00A0;”>
]>
<xsl:stylesheet
        version=“1.0”
        xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”
        xmlns:msxml=“urn:schemas-microsoft-com:xslt”
        xmlns:usg=“urn:usg-com:xslt”
        xmlns:umbraco.library=“urn:umbraco.library”
xmlns:Exslt.ExsltCommon=“urn:Exslt.ExsltCommon”
xmlns:Exslt.ExsltDatesAndTimes=“urn:Exslt.ExsltDatesAndTimes”
xmlns:Exslt.ExsltMath=“urn:Exslt.ExsltMath”
xmlns:Exslt.ExsltRegularExpressions=“urn:Exslt.ExsltRegularExpressions”
xmlns:Exslt.ExsltStrings=“urn:Exslt.ExsltStrings”
xmlns:Exslt.ExsltSets=“urn:Exslt.ExsltSets”
        exclude-result-prefixes=“msxml umbraco.library Exslt.ExsltCommon
Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions
Exslt.ExsltStrings Exslt.ExsltSets usg”>
  <xsl:output method=“xml” omit-xml-declaration=“yes”/>


  <msxml:script language=“CSharp” implements-prefix=“usg”>
    <msxml:assembly name=“System.Web” />
    <msxml:using namespace=“System.Web” />

    <![CDATA[
public void changeOutPut(String ContentType, String FileName) {
        HttpContext.Current.Response.ContentType = ContentType;
        HttpContext.Current.Response.AddHeader(“content-disposition”,
                        “attachment;filename=“ + FileName);
}
]]>

  </msxml:script>

  <xsl:param name=“currentPage”/>

  <xsl:template match=“/”>
    <xsl:variable name=“fileName” select=“concat(‘name-data-’,@nodeName,‘.xls’)” />

    <xsl:value-of select=“usg:changeOutPut(‘application/msexcel’,$fileName)” />
    <table cellpadding=“0” cellspacing=“0” border=“0”>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Title</th>
            <th>Currently Employed</th>
        </tr>
        <tr>
            <td>John</td>
            <td>Doe</td>
            <td>Director Of Professional Services</td>
            <td>Yes</td>
        </tr>
        <tr>
            <td>Jane</td>
            <td>Plane</td>
            <td>Vice President</td>
            <td>Yes</td>
        </tr>
        <tr>
            <td>Mike</td>
            <td>Vick</td>
            <td>Sr. Engineer</td>
            <td>No</td>
        </tr>
        <tr>
            <td>Roland</td>
            <td>Wolters</td>
            <td>Engineer</td>
            <td>Yes</td>
        </tr>
    </table>
  </xsl:template>

</xsl:stylesheet>

The No Template Effect

If no template is selected, the effect will be a blank white page with no output at all. This is because Umbraco relies solely on the use of master pages to render the content. So, if no template is selected, then effectively no way exists to output content to the browser.

Checking the template ID of nodes/pages in the XML cache is also possible, as discussed in Chapter 2 and shown in Listings 4-6 and 4-7.

LISTING 4-6: Checking the templateId in Umbraco v 4.x

image
<node id=“1049” parentID=“1048” level=“2” writerID=“0” creatorID=“0”
nodeType=“1046” template=“0” sortOrder=“1” createDate=“2008-06-
01T23:12:54” updateDate=“2010-08-01T08:20:16” nodeName=“Installing runway
modules” urlName=“installing-runway-modules” writerName=“Administrator”
creatorName=“Administrator” nodeTypeAlias=“RunwayTextpage” path=“-
1,1048,1049”>…</>

LISTING 4-7: Checking the templateId in Umbraco v 4.5

image
<Textpage id=“1049” parentID=“1048” level=“2” writerID=“0” creatorID=“0”
nodeType=“1046” template=“0” sortOrder=“1” createDate=“2008-06-01T23:12:54”
updateDate=“2010-08-06T07:55:42” nodeName=“Installing runway modules”
urlName=“installing-runway-modules” writerName=“Administrator”
creatorName=“Administrator” path=“-1,1048,1049” isDoc=“”>…</>

When the templateId is 0 or blank, it's a good indication that a template has not been selected for the node/page in question. The possibility also exists that Umbraco may need to rebuild the cache (if you have it configured to be on, which is the default value). As shown in Figure 4-15, simply right-click on the top Content node in the content tree and select Republish entire site.

FIGURE 4-15

image

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.162.26