Search This Blog

Showing posts with label {otherwise}. Show all posts
Showing posts with label {otherwise}. Show all posts

Tuesday, July 1, 2025

How to Create and Use APEX CASE Directive with TEMPLATE_TEXT Using {case}, {when}, {otherwise}, and {endcase}

 In Oracle APEX, the use of Template Directives such as {case}, {when}, {otherwise}, and {endcase} within TEMPLATE_TEXT allows developers to create dynamic, conditional content inside custom templates like Card Reports or Classic Reports. These CASE directives operate similarly to a traditional CASE expression in SQL or PL/SQL, enabling conditional rendering of output depending on the value of a specific item or column. This approach helps developers display custom labels, styles, or icons based on data values without writing additional PL/SQL logic or creating multiple templates. It simplifies complex UI behavior by embedding logic directly into the template, keeping the application clean and maintainable.

In Oracle APEX, the {case}, {when}, {otherwise}, and {endcase} directives are powerful tools used within TEMPLATE_TEXT to apply conditional logic directly in your report templates. These directives enable you to control how specific portions of a report are rendered based on the value of a particular column or item. This is particularly useful in Card Reports, Classic Reports, and custom region templates where dynamic styling, icons, or text output is required.

To use the CASE directive, begin by editing the TEMPLATE_TEXT of a report template, such as a Card Region or Classic Report. You will wrap conditional logic using the {case} directive followed by one or more {when} conditions. You can also include an optional {otherwise} block to define the default behavior. Every conditional block must end with {endcase}.

Example use case: displaying a status icon based on a column named STATUS.

<span class="status-icon">
  {case STATUS}
    {when 'OPEN'}
      <i class="fa fa-circle text-success"></i>
    {when 'CLOSED'}
      <i class="fa fa-circle text-danger"></i>
    {when 'PENDING'}
      <i class="fa fa-circle text-warning"></i>
    {otherwise}
      <i class="fa fa-circle text-secondary"></i>
  {endcase}
</span>

In this example, based on the value of the STATUS column, a different Font Awesome icon color is displayed. The condition matches the value exactly as provided in the {when} directive. If no condition matches, the {otherwise} block will render the fallback content.

Steps to Implement in Oracle APEX:

  1. Open App Builder and select your application.

  2. Navigate to Shared Components > Templates, or directly edit a region or card report.

  3. Edit the Region or Report Template, scroll to TEMPLATE_TEXT.

  4. Insert your conditional logic using {case}, {when}, {otherwise}, and {endcase}.

  5. Save your template and refresh the report to see it in action.

You can also nest other directives inside these blocks such as {apex$column_name} for data substitution or use HTML to customize output.

The APEX Template Engine interprets these directives at runtime, rendering the HTML output accordingly. This eliminates the need to use PL/SQL or JavaScript for display logic in many scenarios, making your reports cleaner, more performant, and easier to maintain.

Examples

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:

  1. Create a Static Content Region.

  2. 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:

  1. Create a Page Item: Create a page item called P1_STATUS with possible values: 'Active', 'Inactive', and 'Pending'.

  2. Create a Static Content Region.

  3. 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:

  1. Set a session variable: Set the SESSION_STATUS session variable to 'LOGGED_IN' or 'LOGGED_OUT' based on the user’s login state.

  2. Create a Static Content Region.

  3. 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.

By incorporating the {case} directive in your TEMPLATE_TEXT, you can control the display based on a column's value—for example, showing different icons based on a status field, or applying different color classes. This technique not only enhances the end-user experience but also significantly reduces the need for external logic or JavaScript manipulation. Once you become familiar with how the {case} directive functions, it becomes a powerful part of your Oracle APEX template toolkit, enabling rich data-driven displays in a compact, readable format.

Monday, June 30, 2025

How to Create and Use APEX CASE Directive with TEMPLATE_TEXT Using {case}, {when}, {otherwise}, and {endcase}

 In Oracle APEX, the CASE directive is a powerful template feature that enables dynamic content rendering based on conditions directly within your HTML templates. Using the CASE directive along with the keywords {case}, {when}, {otherwise}, and {endcase}, developers can implement conditional logic inside templates without writing complex PL/SQL code. This approach allows you to customize how data and UI elements are displayed based on specific values or states, improving the flexibility and user experience of your APEX applications.

To create and use the APEX CASE directive with TEMPLATE_TEXT, you start by embedding the CASE syntax within your HTML or region templates. The structure begins with {case expression}, where the expression is evaluated, followed by one or more {when value} blocks that define the output for matching values. The {otherwise} block specifies a default output if none of the conditions match, and the directive is closed with {endcase}. This simple yet effective logic lets you show different content, styles, or icons based on the data. For example, you might display a green badge for "Active" status and a red badge for "Inactive," all controlled inside the template with CASE directives.

Using the CASE directive enhances maintainability and readability of your templates by centralizing conditional display logic. It reduces the need for multiple static templates or complicated dynamic actions and can be combined seamlessly with other substitution variables. Mastering this directive empowers developers to build highly customizable interfaces that adapt dynamically to underlying data values, providing end-users with clearer visual cues and context-sensitive information.

In Oracle APEX, the CASE directive is a versatile feature used within TEMPLATE_TEXT to add conditional logic to your templates. This directive enables you to control what content or formatting is displayed based on dynamic values, all within the template itself, without needing to write additional PL/SQL code. The directive uses the keywords {case}, {when}, {otherwise}, and {endcase} to define conditions and their corresponding outputs, allowing for clean, maintainable, and powerful template customization.

To create and use the APEX CASE directive, start by embedding the {case expression} at the point in your template where you want to evaluate a variable or column value. The expression inside {case} typically references a substitution string, such as a column name or an item. Following this, you define one or more {when value} blocks. Each {when} block specifies the output to render when the expression matches that particular value. If none of the {when} conditions match, the {otherwise} block defines a default output. The directive is closed with {endcase} to signal the end of the conditional logic.

Here is a basic example to illustrate the syntax:

{case STATUS}
{when ACTIVE}
Active
{when INACTIVE}
Inactive
{otherwise}
Unknown
{endcase}

In this example, the value of STATUS is checked. If it is "ACTIVE", a green "Active" label is shown; if "INACTIVE", a red "Inactive" label; otherwise, a generic "Unknown" label appears. This allows you to create dynamic and visually distinct displays based on data values without complex logic elsewhere.

To implement this in Oracle APEX:

  1. Open your application and navigate to Shared Components.

  2. Select Templates, then choose the template you want to modify, such as a report or card template.

  3. Locate the TEMPLATE_TEXT field where you want to add conditional logic.

  4. Insert the CASE directive syntax with appropriate substitution strings matching your application’s columns or items.

  5. Save your changes and run your page to see the conditional rendering in action.

Using the CASE directive improves maintainability because it consolidates conditional formatting within the template itself, avoiding scattered logic across multiple components or processes. It also enhances user experience by providing clear visual cues directly tied to data states.

The APEX CASE directive with TEMPLATE_TEXT is a powerful tool to add dynamic, condition-based content rendering inside your templates. By mastering the use of {case}, {when}, {otherwise}, and {endcase}, developers can create cleaner templates, reduce code duplication, and build flexible, user-friendly interfaces that respond intelligently to underlying data values.

Example

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:

  1. Create a Static Content Region.

  2. 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:

  1. Create a Page Item: Create a page item called P1_STATUS with possible values: 'Active', 'Inactive', and 'Pending'.

  2. Create a Static Content Region.

  3. 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:

  1. Set a session variable: Set the SESSION_STATUS session variable to 'LOGGED_IN' or 'LOGGED_OUT' based on the user’s login state.

  2. Create a Static Content Region.

  3. 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.

In conclusion, the APEX CASE directive with TEMPLATE_TEXT offers a streamlined and efficient way to embed conditional logic directly within templates. By using {case}, {when}, {otherwise}, and {endcase}, developers can create versatile, responsive user interfaces that adapt based on data values, all while keeping template code clean and maintainable. Leveraging this directive unlocks new possibilities for dynamic presentation in Oracle APEX applications, making it an essential tool for advanced customization.