Introduction
Oracle APEX allows developers to create highly customized report layouts using Template Directives. One powerful tool in this arsenal is the {case}
directive, which lets you implement conditional logic directly within TEMPLATE_TEXT
of a region or component. With {case}
, {when}
, {otherwise}
, and {endcase}
, you can alter the output dynamically based on column values—without writing any JavaScript or dynamic actions. This approach keeps your layout clean and declarative.
How to Create and Use APEX CASE Directive with TEMPLATE_TEXT
In Oracle APEX, TEMPLATE_TEXT
is used in Classic Reports, Cards, and other templated components to control how each row or item is displayed. The {case}
directive evaluates the value of a column and renders different markup accordingly.
Basic Syntax Example
{case STATUS}
{when 'OPEN'}
<span class="badge badge-success">Open</span>
{when 'CLOSED'}
<span class="badge badge-danger">Closed</span>
{otherwise}
<span class="badge badge-secondary">Unknown</span>
{endcase}
Step-by-Step: How to Implement It
-
Create or Edit a Report Region
-
Navigate to a Classic Report or Cards region.
-
Under the “Appearance” section, open Template Options.
-
Choose a Custom Template or enable Custom Template Text.
-
Set the
TEMPLATE_TEXT
orCard Template
to a custom expression.
-
-
Insert the {case} Directive
-
Use
{case COLUMN_NAME}
whereCOLUMN_NAME
is a column alias in your SQL query. -
Add
{when}
blocks for each value you want to handle. -
Include an optional
{otherwise}
block for any unmatched values. -
Close the structure with
{endcase}
.
-
-
Example with Card Report
<div class="card"> <div class="card-body"> <h5>{TITLE}</h5> {case PRIORITY} {when 'HIGH'} <span class="badge badge-danger">High</span> {when 'MEDIUM'} <span class="badge badge-warning">Medium</span> {otherwise} <span class="badge badge-secondary">Low</span> {endcase} </div> </div>
-
SQL Example for the Above
SELECT TASK_TITLE AS TITLE, TASK_PRIORITY AS PRIORITY FROM TASKS
Best Practices
-
Keep logic simple. Do not embed complex expressions inside the
{case}
block; pre-process in SQL when possible. -
Use consistent casing (upper/lower) for value comparison to avoid mismatches.
-
Escape HTML properly if you're displaying user data inside condition blocks.
-
Test each case by previewing sample data to ensure proper rendering.
Oracle APEX Documentation
Learn more about template directives and advanced templating in Oracle APEX here:
APEX Template Directives Reference
In Oracle APEX, you can use CASE directives in templates to dynamically generate content based on specific conditions. The syntax {case}, {when}, {otherwise}, and {endcase} are used in APEX templates to create conditional logic, allowing you to show or hide content based on certain values or conditions.
In this tutorial, we’ll demonstrate how to use these directives to display different content dynamically in your APEX application.
Step 1: Understanding the Syntax
Here’s the basic structure of a CASE directive using TEMPLATE_TEXT with {case}, {when}, {otherwise}, and {endcase}:
{case NAME/}
{when string1/}
TEMPLATE_TEXT1
{when string2/}
TEMPLATE_TEXT2
{otherwise/}
TEMPLATE_TEXT
{endcase/}
{case NAME/}: This begins the CASE block and is followed by a name or expression. The block will evaluate conditions based on the value of NAME.
{when string1/}: This specifies a condition (string1). If NAME matches string1, the content (TEMPLATE_TEXT1) will be displayed.
{when string2/}: This specifies another condition (string2). If NAME matches string2, the content (TEMPLATE_TEXT2) will be displayed.
{otherwise/}: If no conditions match, this content will be displayed as the default.
{endcase/}: This closes the CASE block.
Step 2: Example 1 - Showing Different Content Based on User Role
Suppose you want to display different messages to users based on their roles (e.g., Admin, User, Guest). You can create a CASE directive that evaluates the &APP_USER. (the current user) and shows different text depending on the role.
Steps:
Create a Static Content Region.
In the HTML Expression field, use the following template:
{case &APP_USER./}
{when 'ADMIN'/}
<h2>Welcome, Administrator!</h2>
<p>You have full access to the system.</p>
{when 'USER'/}
<h2>Welcome, User!</h2>
<p>You have limited access to certain features.</p>
{otherwise/}
<h2>Welcome, Guest!</h2>
<p>Please log in to access more features.</p>
{endcase/}
Explanation: This block checks the value of &APP_USER. (the logged-in user):
If the user is 'ADMIN', it shows a message welcoming the admin.
If the user is 'USER', it shows a message for the standard user.
If the user is neither, it displays a default guest message.
Expected Output:
If &APP_USER. is 'ADMIN':
Welcome, Administrator!
You have full access to the system.
If &APP_USER. is 'USER':
Welcome, User!
You have limited access to certain features.
If &APP_USER. is any other value (e.g., 'GUEST'):
Welcome, Guest!
Please log in to access more features.
Step 3: Example 2 - Showing Content Based on Page Item Value
In this example, let's say you want to show different content depending on the value selected in a page item (P1_STATUS). For instance, the status could be either 'Active', 'Inactive', or 'Pending'.
Steps:
Create a Page Item: Create a page item called P1_STATUS with possible values: 'Active', 'Inactive', and 'Pending'.
Create a Static Content Region.
In the HTML Expression field, use the following template:
{case :P1_STATUS/}
{when 'Active'/}
<h2>Status: Active</h2>
<p>The system is running smoothly.</p>
{when 'Inactive'/}
<h2>Status: Inactive</h2>
<p>The system is currently inactive. Please contact support.</p>
{when 'Pending'/}
<h2>Status: Pending</h2>
<p>The system status is pending. Please wait for further updates.</p>
{otherwise/}
<h2>Status: Unknown</h2>
<p>The status could not be determined.</p>
{endcase/}
Explanation: This block evaluates the value of the P1_STATUS page item:
If P1_STATUS is 'Active', it shows a message indicating that the system is running smoothly.
If P1_STATUS is 'Inactive', it shows a message about the system being inactive.
If P1_STATUS is 'Pending', it shows a message about the pending status.
If none of these values are selected, it shows a default message.
Expected Output:
If P1_STATUS is 'Active':
Status: Active
The system is running smoothly.
If P1_STATUS is 'Inactive':
Status: Inactive
The system is currently inactive. Please contact support.
If P1_STATUS is 'Pending':
Status: Pending
The system status is pending. Please wait for further updates.
If P1_STATUS is any other value:
Status: Unknown
The status could not be determined.
Step 4: Example 3 - Customizing Output Based on a Session Variable
In this example, let’s use a session variable to customize content dynamically. You can create a condition that checks whether the session variable SESSION_STATUS is 'LOGGED_IN' or 'LOGGED_OUT' to display different messages.
Steps:
Set a session variable: Set the SESSION_STATUS session variable to 'LOGGED_IN' or 'LOGGED_OUT' based on the user’s login state.
Create a Static Content Region.
In the HTML Expression field, use the following template:
{case :SESSION_STATUS/}
{when 'LOGGED_IN'/}
<h2>Welcome Back!</h2>
<p>You're logged in and ready to go.</p>
{when 'LOGGED_OUT'/}
<h2>Logged Out</h2>
<p>You have been logged out. Please log in again to continue.</p>
{otherwise/}
<h2>Status Unknown</h2>
<p>Your session status could not be determined. Please try again later.</p>
{endcase/}
Explanation: This block evaluates the session variable SESSION_STATUS:
If SESSION_STATUS is 'LOGGED_IN', it shows a welcome message.
If SESSION_STATUS is 'LOGGED_OUT', it shows a message indicating the user is logged out.
If neither condition is met, it shows a default "status unknown" message.
Expected Output:
If SESSION_STATUS is 'LOGGED_IN':
Welcome Back!
You're logged in and ready to go.
If SESSION_STATUS is 'LOGGED_OUT':
Logged Out
You have been logged out. Please log in again to continue.
If SESSION_STATUS has any other value:
Status Unknown
Your session status could not be determined. Please try again later.
Step 5: Using Dynamic Content with CASE Directive in Regions
You can dynamically change the content of entire regions based on conditions. The CASE directive can be used to show or hide entire regions or provide different content within regions depending on the condition being evaluated.
For instance, you can conditionally show a region only for certain user types or based on a condition.
Example Code:
{case :USER_ROLE/}
{when 'ADMIN'/}
<h2>Admin Dashboard</h2>
<p>You have full control over the application settings and users.</p>
{when 'MANAGER'/}
<h2>Manager Dashboard</h2>
<p>You can manage your team's performance and reports.</p>
{otherwise/}
<h2>User Dashboard</h2>
<p>Access basic application features.</p>
{endcase/}
Expected Output:
If USER_ROLE is 'ADMIN':
Admin Dashboard
You have full control over the application settings and users.
If USER_ROLE is 'MANAGER':
Manager Dashboard
You can manage your team's performance and reports.
If USER_ROLE is anything else:
User Dashboard
Access basic application features.
By applying these techniques, you can provide a personalized experience to your users by displaying the right content based on various conditions, ensuring a more dynamic and interactive user interface.
Key Takeaways:
{case NAME/} begins the conditional block and evaluates the value of NAME.
{when value/} is used to specify a condition and display content when the condition is met.
{otherwise/} defines the default content when no conditions match.
- {endcase/} closes the CASE block.
Conclusion
The {case}
directive in Oracle APEX's TEMPLATE_TEXT
enables powerful, readable, and maintainable conditional display logic within your UI. It helps create visually responsive reports and cards without relying on dynamic actions or JavaScript. When used thoughtfully and combined with clean SQL and template design, {case}
directives can greatly enhance the dynamic presentation of your data-driven applications.