Understanding and Creating Blocks in OutSystems: An Overview

OutSystems is an extremely powerful low-code platform for the development of modern web and mobile applications. Among its most useful features is the concept of blocks, which allow the creation of reusable UI components that can help boost productivity and consistency during application development. In this blog post, we will learn what blocks are, their key features, advantages, and how to create them with an example to get you started.

Key Features of Blocks  

1. Reusability: Once created, it can be used multiple times in various screens or other blocks; thus, it saves a lot of development time by ensuring consistency in the UI.

2. Modularity: Blocks are designed like "black boxes," in that each has its own scope and does not access or change the parent scope directly. The isolation of concerns simplifies testing and maintenance.

3. Customizability: Every block can contain input parameters and placeholders that allow customization in content upon instantiation of the block.

4. Hierarchical Structure: Blocks can be nested in any screen and blocks, ensuring a parent-child relationship in structure. A block cannot include itself, either directly or indirectly, to avoid recursion.

5. Easy Maintenance: When a block is updated, the change reflects throughout the application wherever that block may be used, hence easing maintenance.

Creating a Block in OutSystems: Step-by-Step Guide

  1. Reusability: The AddressBlock can be used across different screens and contexts within the application, ensuring a consistent format for displaying addresses.
  1. Maintainability: If you need to change the address layout or styling, you only need to modify the AddressBlock, and all instances across the application will update automatically.
  1. Customization: The block’s parameters allow for the display of different data in each row, making it flexible for various use cases.

Benefits of Using Blocks in a Table

  1. Reusability: The AddressBlock can be used across different screens and contexts within the application, ensuring a consistent format for displaying addresses.
  1. Maintainability: If you need to change the address layout or styling, you only need to modify the AddressBlock, and all instances across the application will update automatically.
  1. Customization: The block’s parameters allow for the display of different data in each row, making it flexible for various use cases.

Blocks and Events in OutSystems: An Overview

In OutSystems, events play a crucial role in enabling interaction between UI elements and their parent components. In this blog post, we'll delve into the mechanics of events in OutSystems, explaining how they function, how they are triggered, and what happens when they're handled.

What is an Event in OutSystems?

At its core, an event in OutSystems is an element that allows a block to communicate with its parent component. By using events, a block can notify its parent that a specific action has occurred, ensuring that different parts of the application stay synchronized.

For instance, consider a block that represents a selection list. If a user interacts with the list and selects an option (e.g., choosing "Wednesday"), the block needs to inform the parent screen of this selection to update any related elements or logic. Events enable this flow of information between the block and its parent.

Why Are Events Important?

Blocks and their parent components operate within distinct scopes. This means that any interaction happening inside a block is not automatically known to the parent component. If an action inside a block, such as selecting a day of the week, needs to trigger a response or update in the parent, an event must be used.

Without events, changes within a block would remain isolated, preventing the parent from reacting appropriately. Events bridge this gap by conveying the necessary information to the parent.

Triggering and Handling Events

Here's how the event flow works:

  1. Triggering the Event: When a specific action occurs within a block (e.g., a user selects a day), the block triggers an event. This event acts as a signal to the parent component that something noteworthy has happened.
  1. Handling the Event: Once an event is triggered, the parent component must have an associated event handler to manage it. This handler is defined as a screen action at the parent level, where the logic for handling the event is written.
  1. Execution Flow: After the event is triggered, the execution flow shifts to the event handler, where the defined logic is executed. The handler can access input parameters passed by the block to make decisions or perform specific actions. Once the event handler finishes its execution, control returns to the flow in the block that triggered the event.

How Events Pass Data

Events can include input parameters that allow data to be passed from the block's scope to the parent component's scope. These input parameters ensure that the parent receives relevant information about the event, such as which day was selected in our example.

Key Takeaways

Events provide a powerful mechanism for building interactive and responsive OutSystems applications, allowing developers to create seamless user experiences where components react dynamically to user actions. Understanding how to use and manage events effectively can greatly enhance the flexibility and functionality of your applications.

Using Blocks and Events Together: A Practical Example

To demonstrate how to use blocks and events in OutSystems, let’s create an example where a product card block displays product details and communicates user interactions back to the parent screen.

Step 1: Create the Product Card Block

  1. Navigate to the Interface tab and create a new block called "ProductCard".

  1. Design the ProductCard block with widgets to display product details, such as:  

  1. Add an input parameter called Product to pass the product data to the block.
  1. Use the input parameter to populate the widgets dynamically (e.g., set the image URL, product name, and price).

Step 2: Add an Event to the Product Card Block

  1. Add an action in the ProductCard block that triggers the "ProductClicked" event. This action should be connected to the click event of the Add button.
  1. When the user clicks on the product card, the action should trigger the ProductClicked event and pass the relevant data (e.g., ProductId) as parameters.

Step 3: Handle the Event in the Parent Screen

  1. Go to the screen where the ProductCard block is used.
  1. Drag and drop the ProductCard block into the screen and bind its Product input parameter to a data source that provides the product details.
  1. Add an event handler to the screen for the "ProductClicked" event from the ProductCard block.

  1. Define the logic you want to execute when the event is triggered. For example, you could display a detailed product page, show a modal, or log the event for analytics.

Displaying Product Details on Click

Suppose you have a list of products displayed on a screen, each represented by a ProductCard block. When a user clicks on a card, the block triggers the "ProductClicked" event, passing the ProductId to the parent screen. The parent screen then handles this event by navigating the user to a detailed product page or showing a modal with more information.

the interaction flow:

Benefits of Using Blocks and Events Together

  1. Modularity and Reusability: Blocks encapsulate UI components, making them easy to use and reuse across different screens and applications.
  1. Improved Maintainability: When you need to change the design or logic of the block, you only need to do it in one place, and all instances are updated automatically.
  1. Decoupled Communication: Events allow blocks to communicate with their parent or other components without tight coupling, making the code more flexible and easier to maintain.
  1. Dynamic Interaction: Using events, you can create interactive experiences where user actions in a block trigger response in the parent screen or other blocks.

Conclusion

Blocks and events are powerful features in OutSystems that enable developers to create scalable, maintainable, and interactive applications. By using blocks, you can design reusable UI components, and with events, you can facilitate communication between components, promoting a clean, modular, and dynamic application architecture. Whether you're building complex dashboards, interactive forms, or product lists, understanding and leveraging these features will help you create better applications with less effort.

More from

EONE Technologies posts

January 24, 2025
What is a Microservices Architecture?
In today’s fast-paced digital era, businesses face growing pressure to deliver innovative solutions quickly. Microservices have emerged as a popular architecture to meet these demands, with companies like Netflix and Amazon leading the way. But is this approach right for every organization?
January 13, 2025
Best Practices for Using Client Variables
Client Variables are a powerful feature in OutSystems, enabling developers to store data directly on the client side for Reactive Web and Mobile applications. In this blog, we will delve deeply into client variables, exploring their uses, best practices, and potential pitfalls, while also addressing some lesser-known nuances to help you maximise their potential.
December 23, 2024
When to Use Agile
Agile is a project management and software development approach that emphasises flexibility, collaboration, and customer-centricity. It involves breaking projects into smaller, manageable phases and iterating through cycles of planning, executing, and evaluating. Agile is not just a methodology but a mindset that prioritises quick feedback and continuous improvement. Agile is guided by the Agile Manifesto

Ready to Take Your Business to The Next Level?

Contact us