Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including @bloomreach/ng-sdk with all npm packages installed. Try it out:

require("@angular/common/package.json"); // @angular/common is a peer dependency. require("@angular/core/package.json"); // @angular/core is a peer dependency. require("@angular/platform-browser/package.json"); // @angular/platform-browser is a peer dependency. require("tslib/package.json"); // tslib is a peer dependency. var ngSdk = require("@bloomreach/ng-sdk")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@bloomreach/ng-sdk v14.3.3

Bloomreach SPA SDK for Angular

Bloomreach Angular SDK

NPM License

Bloomreach Angular SDK provides simplified headless integration with Bloomreach Experience Manager for Angular-based applications. This library interacts with the Page Model API and Bloomreach SPA SDK and exposes a simplified declarative Angular interface over the Page Model.

What is Bloomreach Experience Manager?

Bloomreach Experience Manager (brXM) is an open and flexible CMS designed for developers and marketers. As the original headless CMS, brXM allows developers to build quickly and integrate with the systems. While it’s built for speed, it also provides top-notch personalization and channel management capabilities for marketers to drive results.

Features

  • brXM Page Angular component;
  • brXM Components Angular directive;
  • Manage Content Button;
  • Manage Menu Button;
  • TransferState support;
  • Angular Universal support.

Get Started

Installation

To get the SDK into your project with NPM:

npm install @bloomreach/ng-sdk

And with Yarn:

yarn add @bloomreach/ng-sdk

Usage

The following code snippets render a simple page with a Banner component.

src/app/app.module.ts

In the NgModule metadata, it needs to import BrSdkModule and specify all the brXM components as module entry components.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrSdkModule } from '@bloomreach/ng-sdk';

import { AppComponent } from './app.component';
import { BannerComponent } from './banner/banner.component';

@NgModule({
  bootstrap: [AppComponent],
  declarations: [
    AppComponent,
    BannerComponent,
  ],
  entryComponents: [
    BannerComponent,
  ],
  imports: [
    BrowserModule,
    BrSdkModule,
  ],
})
export class AppModule {}

src/app/app.component.ts

In the app-root component, it needs to pass the configuration and brXM components mapping into the br-page component inputs.

import { Component } from '@angular/core';
import { Location } from '@angular/common';
import { BrPageComponent } from '@bloomreach/ng-sdk';

import { BannerComponent } from './banner/banner.component';

@Component({
  selector: 'app-root',
  template: `
    <br-page [configuration]="configuration" [mapping]="mapping">
      <ng-template let-page="page">
        <header>
          <a [href]="page.getUrl('/')">Home</a>
          <ng-container brComponent="menu"></ng-container>
        </header>
        <section>
          <ng-container brComponent="main"></ng-container>
        </section>
        <footer>
          <ng-container brComponent="footer"></ng-container>
        </footer>
      </ng-template>
    </br-page>
  `,
})
export class AppComponent {
  configuration: BrPageComponent['configuration'];

  mapping = {
    Banner: BannerComponent,
  };

  constructor(location: Location) {
    this.configuration = {
      cmsBaseUrl: 'http://localhost:8080/site',
      request: { path: location.path() },
    };
  }
}

src/app/banner/banner.component.ts

Finally, in the app-banner component, it can consume the component data via the component input.

import { Component, Input } from '@angular/core';
import { Component as BrComponent } from '@bloomreach/spa-sdk';

@Component({
  selector: 'app-banner',
  template: 'Banner: {{ component.getName() }}',
})
export class BannerComponent {
  @Input() component!: BrComponent;
}

Configuration

The br-page component supports several options you may use to customize page initialization. These options will be passed to the initialize function from @bloomreach/spa-sdk. See here for the full configuration documentation.

Mapping

The br-page component provides a way to link Angular components with the brXM ones. It requires to pass the mapping property that maps the component type with its representation.

  • The Container Items can be mapped by their labels.

    import { NewsListComponent } from './news-list/news-list.component';
    
    export class AppComponent {
      mapping = {
        'News List': NewsListComponent,
      };
    }
    
  • The Containers can be only mapped by their type, so you need to use constants from @bloomreach/spa-sdk. By default, the Angular SDK provides an implementation for all the container types as it is defined in the documentation.

    import { TYPE_CONTAINER_INLINE } from '@bloomreach/spa-sdk';
    import { InlineContainerComponent } from './inline-container/inline-container.component';
    
    export class AppComponent {
      mapping = {
        [TYPE_CONTAINER_INLINE]: InlineContainerComponent,
      };
    }
    

    From within the Container component, the Container Items can be accessed via the getChildren method. This can be used to reorder or wrap child elements.

    import { Component, Input } from '@angular/core';
    import { Component as BrComponent } from '@bloomreach/spa-sdk';
    
    @Component({
      selector: 'app-inline-container',
      template: `
        <div>
          <span *ngFor="let child of component.getChildren()">
            <ng-container [brComponent]="child"></ng-container>
          </span>
        </div>
      `,
    })
    export class InlineContainerComponent() {
      @Input() component!: BrComponent;
    }
    
  • The Components can be mapped by their names. It is useful for a menu component mapping.

    import { MenuComponent } from './menu/menu.component';
    
    export class AppComponent {
      mapping = {
        menu: MenuComponent,
      };
    }
    

Inline Mapping

There is also another way to render a component. In case you need to show a static component or a component from the abstract page, you can use inline component mapping.

@Component({
  selector: 'app-root',
  template: `
    <br-page>
      <ng-template>
        <app-menu *brComponent="'menu'" let-component [component]="component">
        </app-menu>
      </ng-template>
    </br-page>
  `,
})
export class AppComponent {}

In a brXM component, it is also possible to point where the component's children are going to be placed.

@Component({
  selector: 'app-footer',
  template: `
    <div>
      @copy; Bloomreach
      <ng-container brComponent></ng-container>
    </div>
  `,
})
export class FooterComponent {}

The component data in case of inline mapping can be accessed via the template context.

@Component({
  selector: 'app-root',
  template: `
    <br-page>
      <ng-template>
        <ul *brComponent="'menu'" let-component let-page="page">
          <li><a [href]="page.getUrl('/')">Home</a></li>
          <li *ngFor="let item of component.getModels()">...</li>
        </ul>
      </ng-template>
    </br-page>
  `,
})
export class AppComponent {}

State Transfering

The br-page component supports TransferState without any extra configuration. To use it in Angular Universal applications, import ServerTransferStateModule on the server and BrowserTransferStateModule on the client. If you would like to disable the feature, just pass false into the stateKey input.

<br-page [stateKey]="false"></br-page>

Reference

The Angular SDK is using Bloomreach SPA SDK to interact with the brXM. The complete reference of the exposed JavaScript objects can be found here.

br-page

This is the entry point to the page model. This component requests and initializes the page model, and then renders the page root component recursively.

TypePropertyRequiredDescription
inputconfigurationyesThe configuration of the SPA SDK.
inputmappingyesThe brXM and Angular components mapping.
inputpagenoPreinitialized page instance or prefetched page model. Mostly that should be used to transfer state from the server-side to the client-side.
inputstateKeynoThe TransferState key is used to transfer the state from the server-side to the client-side. By default, it equals to brPage. If false is passed then the state transferring feature will be disabled.
outputstatenoThe current state of the page component.

This component also supports a template transclusion. <ng-template> from the component contents will be rendered in the root component context.

VariableDescription
$implicitThe root component.
componentThe root component.
pageThe current page instance.

brComponent

This directive points to where children or some component should be placed. brComponent can be used inside br-page or mapped components only. If it is being used as a structural directive, then the template will be rendered in the context of every matched component. Otherwise, it will try to render all children components recursively.

PropertyRequiredDescription
brComponentnoThe component instance or a path to a component. The path is defined as a slash-separated components name chain relative to the current component (e.g. main/container). If it is omitted, all the children will be rendered.

The template context holds references to the current component and the current page instance.

VariableDescription
$implicitThe current component.
componentThe current component.
pageThe current page instance.

brManageContentButton

This directive places a button on the page that opens the linked content in the document editor. The button will only be shown in preview mode.

PropertyRequiredDescription
brManageContentButtonyesThe content entity to open for editing.

brManageMenuButton

This directive places a button on the page that opens the linked menu in the menu editor. The button will only be shown in preview mode.

PropertyRequiredDescription
brManageContentButtonyesThe related menu model.

Links

FAQ

License

Published under Apache 2.0 license.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free