# <pz:contentSelector> Tag

The <pz:contentSelector> tag implements a Content Selector defined in the Content Selector designer.

A content selector rule first evaluates a set of conditions that you define in the Content Selector designer (for example, whether or not a user fits a specified classification). If the conditions evaluate to true, all matching content is retrieved from the Virtual Content Repository based on a content query defined in the Content Selector designer.

To cache the results of the content selector rule, set the useCache attribute to true. If the cache has not timed out, subsequent calls to the <pz:contentSelector> tag will return the cached results without re-evaluating the content query.

The <pz:contentSelector> tag is empty (attributes only) and returns an array of Node objects from the Virtual Content Repository as a result of executing the content query.

Use the following tags in conjunction with <pz:*> tags to access and display the returned content:

- <es:forEachInArray> to iterate over the Node objects in the array.
- <cm:*> tags to access metadata attributes in the content, retrieve content, and put it into the HTTP response output stream.

Content Selectors can display text content (content property values and binary text content such as HTML files) and non-text binary content (such as graphics). To render non-text binary content, you must use the ShowBinary servlet, as shown in Example 2.

## Syntax

<tagName attribute="value" />

## Attributes

rule

Required (String) - The name of the content selector in the content selector definitions created in the Content Selector designer.

max

Optional (String, long) - Limits the maximum number of content items returned. If not present, or if equal to -1L, it returns all of the content items found.

sortBy

Optional (String) - Use this attribute to sort the retrieved content in ascending or descending order. Enter the content attributes on which you want to sort the content.

However, you can sort content only on content properties that are explicit, meaning the values are stored in their own database column. System properties (see Building Content Queries with Expressions) are explicit by default. But the content properties you create in the WebLogic Administration portal (properties contained within the content "types" you create) are implicit. If you want to sort on those properties, you must make them explicit by doing the following:

Note: You can sort on explicit properties only against a BEA repository. Third-party repositories might behave differently.

1. In your database, create a column in CM_NODE table of type Integer to store the explicit property values you want to sort on. Name the column whatever you like (within the naming contraints).
2. In the WebLogic Administration Portal, set the property you want to sort on to be explicit, and enter the name of the database column you created.
   For example, if you have a property called "myproducts," and you created a database column called "PRODUCT" for it, the following is how you would define the "myproducts" property: select the "Is Explicit?" option and enter "PRODUCT" as the special reference.
   Any content nodes that use this property will have the property value stored in the PRODUCT database column, and the retrieved content can be sorted by that property.

> Use ASC and DESC to sort retrieved content in ascending or descending order.
> Examples:
> sortBy=“myproducts ASC”
> sortBy="cm_modifiedDate DESC" (This is a system attribute that is explicit by default.)

query

Optional (String) - The search query to execute based on the query syntax described in Building Content Queries with Expressions. In most cases, the query is defined when you create a content selector, which is then used in the rule attribute for this tag. If you use the rule attribute, you do not need to use the query attribute.

appendQuery

Optional (String) - A content query string to append to the resulting content query from the rule. If the content selector rule conditions do not match, this value will be ignored. If this value starts || (two vertical bars), it will be logically OR'ed with the content query. If it starts with && (two ampersands), it will be logically AND'ed with the content query. If it doesn't start with || or &&, it will be logically AND'ed with the content query.

id

Required (String) - The array variable name that contains the Node objects found. If no content is found, the variable is assigned an empty array (not null) of Node objects.

useCache

Optional (String, Boolean) - Determines whether content is cached. This attribute can have one of two values:

False (default value): The content cache is not used. If false (not specified), the cacheId, cacheScope and cacheTimeout settings are ignored.

True: Content cache is used.

cacheId

Optional (String) - The identifier name used to cache the content. Internally, the cache is implemented as a Map, which will become the key. If not specified, the id attribute of the tag is used.

cacheTimeout

Optional (String, long) - The time, in milliseconds, for which the cached content is valid. If more than this amount of time has passed since the content was cached, the cached content will be cleared, retrieved, and placed back into the cache.

Use -1 for no-timeout (always use the cached content). Default = -1.

cacheScope

Optional (String) - Specifies the lifecycle scope of the content cache. Similar to <jsp:useBean>.

Possible values:

- application: Any JSP page in the web application that any customer requests can access the cache.
- session (the default): Any JSP in the web application that the current customer requests can access the cache.
- page: Only the current JSP that any customer requests can access the cache.
- request: Only the current user request can access the cache. If a customer re-requests the page, the content selector re-runs the query and recreates the cache.

## Example 1

This example shows retrieval and display of text-based content from the Virtual Content Repository. The code identifies a user to target and uses a Content Selector rule created in the Content Selector designer called "PremierCustomerSpotlight" to run a query and print the title of a document retrieved from the query. If the value of the "name" attribute in the <cm:getProperty> tag is a text-based binary file, such as an HTML file, that text will be rendered on the page.

    <profile:getProfile profileKey="bob" profileId="myProfile" scope="session"/>
    <pz:contentSelector rule="PremierCustomerSpotlight" id="docs" />
    <ul>
    <es:forEachInArray array="<%=docs%>" id="aDoc"
    type="com.bea.content.Node">
    <li>The document title is: <cm:getProperty id="aDoc" name="Title" conversionType="html" />
    </es:forEachInArray>
    </ul>

## Example 2

This example shows retrieval and display of non-text binary content (graphics) from the Virtual Content Repository. Notice in the HTML <img> tag the required use of the getContextPath method, the ShowBinary servlet, and the getPath method to render the graphics.

    <html>
    <head><title>Here's Your Content</title></head>
    <body>
    <p>Here's your content:</p>
    <p>
    <pz:contentSelector id="nodes" rule="modern" />
        <es:forEachInArray array="<%=nodes%>" id="node" type="com.bea.content.Node" >
            <img src="<%=request.getContextPath() + "/ShowBinary" + node.getPath()%>" >
        </es:forEachInArray></p>

    <pz:contentSelector id="nodes" rule="classic" />
        <es:forEachInArray array="<%=nodes%>" id="node" type="com.bea.content.Node" >
            <img src="<%=request.getContextPath() + "/ShowBinary" + node.getPath()%>" >
        </es:forEachInArray></p>

    </body>
    </html>

Related Topics

Creating Content Selectors

<es:forEachInArray> Tag

<cm:getProperty> Tag

<cm:getNode> Tag

<cm:search> Tag