Sknoe / Notes

How SFRA Works: A Beginner’s Guide

Controllers, pdict, res.render() and ISML explained

8 min read

If you’re new to Salesforce Commerce Cloud (SFCC), one of the trickiest parts of learning SFRA is figuring out how all the pieces connect. You might find a controller, an ISML template, a model, and several functions passing data around, but understanding each file individually doesn’t necessarily explain how a storefront page actually gets rendered.

I came across this when moving from Angular and WordPress to SFCC. The challenge wasn’t so much writing JavaScript as understanding the architecture around it: where the data came from, how a request found its way to the right code, and why several ISML files could have the same name. Once I started following a request from the browser to the final HTML, the structure became much easier to reason about.

In this guide, we’ll follow one simple example to understand how an SFRA controller works, what res.render() does, how pdict connects the controller to an ISML template, and where data preparation fits into the picture. You don’t need prior knowledge of React, Angular, or MVC to follow along.

1. Start with the SFRA controller

Imagine a shopper opens a page on your storefront. The browser sends an HTTP request, and SFRA needs to determine which server-side code should handle it. That is where a controller route comes in.

In SFRA, controllers are server-side JavaScript modules that coordinate the work required to respond to storefront requests. You’ll often see routes registered using methods such as server.get() and server.post(). The HTTP method matters: server.get() registers a route for GET requests, while server.post() registers one for POST requests.

Here’s a deliberately small example:

javascript
'use strict';
var server = require('server');
server.get('Example', function (req, res, next) {
res.render('example/page', {
heading: 'Hello from SFRA'
});
next();
});
module.exports = server.exports();

There are a few things worth noticing here. server.get('Example', ...) registers a route named Example, and the callback contains the logic that runs when the route is invoked. The callback receives req, res, and next, which are used to work with the request, prepare the response, and continue through the middleware chain as appropriate.

The res.render() call is the part we’re interested in for now. It tells SFRA which template to render and supplies data for that template. We’ll come back to it shortly.

When you’re investigating an unfamiliar SFRA feature, start by finding the controller route. The route gives you an entry point into the request flow, which is much more useful than guessing which template might be responsible for a page.

2. Where does the data come from?

Finding the controller is only part of the puzzle. Most storefront pages need data: product names, prices, images, availability, search results, customer information, or some combination of these.

That data might originate from the Salesforce B2C Commerce APIs, be retrieved or transformed by a model, or be prepared by other application code. Depending on the feature, a controller may call a model or use a ViewModel to shape the information required by the storefront. A ViewModel can turn platform-specific objects into a structure that is easier for the storefront to consume, but not every route requires a custom ViewModel.

Consider a simplified example of preparing a heading:

javascript
var pageData = {
heading: 'Hello from SFRA'
};
res.render('example/page', pageData);

Here, we’re not fetching anything from the platform. We’re creating an ordinary JavaScript object and passing it to the rendering function. In a real storefront feature, the data might be assembled through several calls and functions before it reaches this point.

This distinction is important when debugging. If a template displays a product name, the template is not necessarily where that name was retrieved or constructed. To understand its origin, trace the value backwards through the code that prepares and passes it along.

3. What does res.render() actually do?

The res.render() call connects the controller to the ISML template. In our example, the first argument, 'example/page', identifies the template, while the second argument contains the data that should be available during rendering.

javascript
res.render('example/page', {
heading: 'Hello from SFRA'
});

Think of this as two related instructions: render the specified template, and make the supplied view data available to it. The controller doesn't need to build the final HTML string itself; the template is responsible for producing the markup.

You’ll also encounter res.setViewData() in SFRA code. It can be used to add data to the view data associated with the response, including data that other middleware or later route logic needs to access. When tracing a value, check both the data supplied to res.render() and any relevant view-data modifications along the route.

4. Meet pdict: how the template accesses data

Now that we’ve passed a heading value to the template, how does the ISML file read it? This is where pdict comes in.

In SFRA, pdict is the page dictionary available to the ISML template. It exposes view data made available for rendering, so a template can access properties without the controller having to generate the final HTML itself.

For example, our example/page.isml template might contain:

html
<h1>${pdict.heading}</h1>

Because the controller supplied heading: 'Hello from SFRA', the expression ${pdict.heading} can output that value. The resulting HTML contains the heading:

html
<h1>Hello from SFRA</h1>

Notice what happened here. The controller supplied the data, pdict exposed it to the template, and ISML used it to produce HTML. pdict doesn't independently fetch product data or decide what the page needs; it gives the template access to data that has been made available for rendering.

5. How does ISML turn that data into a page?

ISML is the server-side template language used by Salesforce B2C Commerce. An ISML template combines HTML with dynamic expressions and template constructs to generate the response. In our example, ${pdict.heading} is replaced with the value available in the page dictionary when the template is processed.

This is different from sending an ISML file to the browser and asking the browser to interpret it. The Commerce server processes the template and returns the resulting HTML. The browser receives that HTML and displays it; any client-side JavaScript then runs in the browser as part of the storefront experience.

Request to HTML
  1. The browser requests a storefront URL.

  2. A controller route handles the request.

  3. The application prepares the data needed by the page.

  4. res.render() identifies the ISML template and supplies view data.

  5. The template reads values through pdict.

  6. ISML produces HTML, which the browser receives and displays.

This is the basic SFRA request-to-render flow. Real storefront pages can involve middleware, cartridge overrides, models, ViewModels, includes, decorators, and client-side requests. Those details matter, but you now have a mental model that helps you place them in context.

Under the hoodHow view data moves through middlewareA route can have several middleware steps that contribute data before SFRA completes the response.

Looking closer

Each middleware step receives req, res, and next. A step can add values with res.setViewData(), then call next() so the next step can run. SFRA merges view data contributed along the chain; the response is rendered after the route’s middleware has completed. That is why a value might be added outside the controller callback you first inspected. For example, one middleware can prepare locale data while another prepares page-specific values. When debugging, follow the chain and check where each expected property is added.

6. Why can several ISML files have the same name?

This is another source of confusion when navigating a large SFRA codebase. You might find several files named product.isml or content.isml in different cartridges. They aren't necessarily duplicates, and SFRA doesn't simply choose a file at random.

Cartridges organise storefront code, and the cartridge path influences how resources are resolved. Custom cartridges can extend or override existing functionality, so the same relative template path may be available in more than one cartridge. Which implementation is used depends on the relevant template path and cartridge resolution rules.

When investigating a template, start with the path passed to res.render(). Then inspect the cartridge structure and cartridge path to understand which file is being resolved. The filename alone isn't enough to establish which template the storefront uses.

This is also why learning about sandboxes, cartridges, and templates as separate concepts helps. They answer different questions: where you work and test, how application code is organised and resolved, and how a page is rendered.

7. Debug an SFRA page by following the data backwards

Suppose you find ${pdict.product.name} in a template, but the product name is missing. Rather than changing the ISML expression immediately, follow the value through the request flow.

Start with the controller route and identify the template it renders. Inspect the data passed to res.render() and any relevant calls to res.setViewData(). Then trace the functions, models, or ViewModels responsible for preparing the product data. If middleware runs along the route, check whether it adds or modifies the view data. Finally, verify that the expected property exists when the template is rendered.

Where to go next

Once the request-to-render flow makes sense, the next concepts to explore are controller middleware, cartridge paths, product models and ViewModels, and ISML includes and decorators. Each adds detail to the same underlying process: handling a request, preparing the right data, and rendering a response.

If you’re new to SFRA, you don't need to memorise every convention before you can work effectively in the codebase. Start with one route, follow one value, and identify the template that renders it. As you repeat that process, the relationships between the files become much easier to recognise.

Did this click?

Tap the bulb if this explanation helped.

Further reading

To see how route data is shaped for ISML, continue with SFRA Models and ViewModels: How Data Gets Ready for ISML. To trace those ideas through a complete shopper action, read Build Add to Cart: A Backend-to-Frontend SFRA Journey.

Interview questions

What is `pdict` in SFRA?

`pdict` is the page dictionary through which an ISML template accesses data supplied for rendering. It helps connect controller-provided data with the template.

What does `res.render()` do?

It renders an ISML template and supplies the data that the template uses to produce the response.

What is the difference between a controller and an ISML template?

A controller route handles a request and coordinates the work needed to respond. An ISML template defines how the page markup is generated using the data available to it.

How would you debug `${pdict.product.name}` if it renders an empty value?

Identify the template, locate the controller route that renders it, inspect the data passed to the template and any middleware that modifies the page dictionary, and trace where the product data is created.

Why might multiple cartridges contain templates with the same name?

Cartridges can provide or override storefront functionality. The relevant template path and cartridge resolution rules determine which implementation is used.