Monday, March 23, 2009

Making Content Occasionally Contributable

Every once in a while, you run into content areas of your web site where the content appears on several pages, but you want to limit access to contribution to just one page.  For example, if there’s a common footer on all of your pages, you may want users to see it on every page, but only change content from the home page.  This entry shows you how you can make contributed content non-contributable.

The Example

We want to create a series of pages where the content is contributable on the home page and re-used, but not contributed, on additional pages.  We will use a WYSIWYG element for the content.

The Execution

The Home Page

The home page is created as you would normally create any other page within Site Studio.  Create a layout page for the home page and then Create a contributable region.  Add a WYSIWYG element to that page. 
Make notes of the following information; you’ll need it later:

  • The Content ID (dDocName) of the WYSIWYG data file
  • The Region Name (not the ID) of the region you created
  • The Element Name (not the ID) of the WYSIWYG element

Assign Region Content to this page.  Save it, and go ahead and contribute some test content to the area.  We can use this later to test our implementation.

The Other Page

On this page, we want to use the same content item from above, but in a non-contributable fashion.  We’ll create a fragment that handles this.
Create a new fragment of “Other” type.  Name it what you like; I’ve named mine “Display XML Data File.” You’ll need to add two parameters to this fragment:

Name

Type

Required?

Description

ssDocName

managedDoc

Yes

The Content ID of the item to be displayed

ssXPath

Text

Yes

The XPath to the content

Create a snippet.  In the body of this snippet, right-click and use “Insert Parameter Declaration” for your two parameters.  This should create the following lines of code for you:

<!--$ssDocName = getValue("#active", ssFragmentInstanceId & "_ssDocName")-->
<!--$ssXPath = getValue("#active", ssFragmentInstanceId & "_ssXPath")-->


When we go to put this fragment on our layout pages, we can use the parameters to declare the Content ID (dDocName) of the file we want to display.  The parameters make this fragment reusable outside of the one document we’re trying to display here.
The only thing we have to do in this fragment is display the content of the document declared in ssDocName.  Site Studio provides a nice addition to IDOC code that allows us to do this: ssIncludeXml.  In fact, this is the exact command used to display the content on the home page that you created.  This command takes two parameters:

Parameter

Description

dDocName

The dDocName of the XML data file you want to display

XPath

The XPath to the content inside the XML file

Notice that these parameters are the same ones we defined in our fragment parameters!  How convenient!
So all we need is one additional line of code in our fragment to make this work:

<!--$ ssIncludeXml(ssDocName, ssXPath) -->

Save the fragment to a fragment library, and you’re good to go.

Putting it All Together

Now that you have the fragment, create a new section for your web site and create a new layout page for this page.  Add your fragment to the page.  You’ll need to provide two parameters for the fragment:

Parameter

Value

ssDocName

The dDocName you identified on the home page

ssXPath

The XPath to the content will be in the following format:

[Region Name]/[Element Name]/node()

For example, if your Region Name is MyRegion and your Element Name is FooterText, then your ssXPath will be:

MyRegion/FooterText/node()

Save the layout page, and you should see the content displayed.  This content display is powered by a fragment, not by a region, so it will not be contributable on this page.  However, because it is still managed content, any changes made to this content item on the home page will be carried forward to the display on other pages.

Other Uses

You can use this trick any time you want to re-use content from an XML data file—or any XML file for that matter!  This is a great way to keep content fresh and consistent on your site while not overwhelming users with some many different contribution boxes.

No comments:

Post a Comment