Using the Fragments Toolkit

Using the Fragments Toolkit

The Fragments Toolkit helps you develop and manage Fragments locally, using your favorite tools. Here you’ll use the toolkit to generate a Fragments Project with sample Fragments, deploy it, and add your own Fragments Set to it.

note

For Liferay DXP 7.4+, Fragment Collections are called Fragment Sets in the Liferay UI.

Setting Up the Toolkit

The toolkit requires this software:

NPM and Yarn are package managers that you’ll use to install the the toolkit and modules it depends on.

  1. Start with installing an NPM and Node.js via a Node.js LTS.

  2. Download and unzip the example JavaScript project ZIP file.

    curl https://resources.learn.liferay.com/dxp/latest/en/site-building/developer-guide/developing-page-fragments/liferay-x2y6.zip -O
    
    unzip liferay-x2y6.zip
    
  3. Set up the Fragments Toolkit and its dependencies using the setup_tutorial.sh script.

    cd liferay-x2y6
    
    ./setup_tutorial.sh
    
    note

    The setup_tutorial.sh script provides commands for setting up Yeoman, Yarn, and the toolkit. The script is available in the example project’s ZIP file.

Resolve all unmet requirements reported by the script and rerun the script until it reports that your environment is ready.

Create a Fragments Project

The Fragments Toolkit’s yo liferay-fragments command launches an interface for generating a Fragments Project.

warning

Don’t nest Fragments projects. Make sure to create new Fragments projects in their own location, outside of any existing Fragments projects.

If you’re in the liferay-x2y6 project folder, exit it (e.g., cd ..).

Here’s how to generate a Fragments Project:

  1. Launch the project generator by executing the yo liferay-fragments command:

    yo liferay-fragments
    
  2. Name your project. For this example, accept the default project name by clicking enter.

    ? Project name (Sample Liferay Fragments)
    
  3. Indicate whether to add sample content. For this example, enter Yes.

    ? Add sample content? Yes
    Creating directory
       force .yo-rc.json
      create src/.gitkeep
      create .editorconfig
      create .gitignore
      create liferay-npm-bundler.config.js
      create package.json
      create README.md
    Adding sample content...
    Warning: some of these fragments are not compatible all
    portal versions, please check the generator documentation before using them:
    https://www.npmjs.com/package/generator-liferay-fragments#creating-new-fragments
    Running yarn...
    Done!
    You're ready to create fragments.
      create src/sample-collection/collection.json
      create src/sample-collection/sample-fragment/index.html
      create src/sample-collection/sample-fragment/main.js
      create src/sample-collection/sample-fragment/styles.css
      create src/sample-collection/sample-fragment/fragment.json
      create src/sample-collection/sample-fragment/configuration.json
      create src/sample-collection/sample-fragment-with-new-editables/index.html
      create src/sample-collection/sample-fragment-with-new-editables/main.js
      create src/sample-collection/sample-fragment-with-new-editables/styles.css
      create src/sample-collection/sample-fragment-with-new-editables/fragment.json
      create src/sample-collection/sample-fragment-with-new-editables/configuration.json
      create src/sample-collection/sample-fragment-with-react/index.html
      create src/sample-collection/sample-fragment-with-react/main.js
      create src/sample-collection/sample-fragment-with-react/styles.css
      create src/sample-collection/sample-fragment-with-react/fragment.json
      create src/sample-collection/sample-fragment-with-react/configuration.json
    

Congratulations on generating your Fragments Project!

The generator creates each project in a folder derived from the project name. For example, since you named the project Sample Liferay Fragments, the generator created a project folder called sample-liferay-fragments.

Import the Sample Fragment Set

Start a new Liferay instance by running

docker run -it -m 8g -p 8080:8080 liferay/portal:7.4.3.75-ga75

Sign in to Liferay at http://localhost:8080. Use the email address [email protected] and the password test. When prompted, change the password to learn.

Then, follow these steps to import the sample Fragment Set to Liferay:

  1. Import the Fragment Set to your Liferay instance invoking the yarn run import command in the project’s root folder (e.g., sample-liferay-fragments). Alternatively, you can import the Fragment Set manually.

    cd sample-liferay-fragments
    
    yarn run import
    
  2. Answer the prompts.

    $ yo liferay-fragments:import
    ? Liferay host & port http://localhost:8080
    ? Username [email protected]
    ? Password [hidden]
    Checking connection...
    Connection successful
    
    ? Company ID liferay.com
    ? Group ID Liferay DXP
    Building project...
    Importing project...
    Project imported
        force .yo-rc.json
        force ../../.yo-rc-global.json
    Done in 21.43s.
    
  3. Verify the Fragment Set is available. Open the Site Menu (Site Menu) and go to DesignFragments. The Set should appear in the list.

    The Set is available.

    note

    For Liferay DXP 7.1 and 7.2, instead navigate to SiteSite BuilderPage Fragments under the Product Menu to get to the Fragments page.

  4. Select a Fragment to view it in the Fragments Editor.

    Here is the sample Fragment with React.

Great! You successfully deployed a Fragment Set.

Fragment Set Structure

Each Fragment Set uses this structure:

  • collection.json: a text file that describes the Fragment Set.

    {
        "description": "Optional description",
        "name": "Fragment Set Name"
    }
    
  • language.properties (optional): language keys defined for the Fragment Set.

  • [fragment-name]/: a folder containing all of the files for a Fragment.

  • resources/ (optional): a folder containing any additional images or files the Fragments need. See Including Default Resources in Fragments for more information.

You can use the Fragments Toolkit to create projects quickly with this structure.

Modifying fragment.json

fragment.json contains fragment attributes that can be modified to enhance the functionality and usability of fragments.

Key Description
cssPath Specifies .css, .scss, or .sass files.
configurationPath Specifies the configuration source for the fragment.
htmlPath Specifies the HTML source for the fragment.
jsPath Specifies the JavaScript source for the fragment.
name The name of the fragment.
type Specifies a component or React fragment.
icon Adds a clay icon to imported fragments. See the Clay Icon List for available icons.
sass {loadPaths:[]}} Specifies external dependencies.
       {
           "cssPath": "styles.css",
           "configurationPath": "configuration.json",
           "htmlPath": "index.html",
           "jsPath": "main.js",
           "name": "Fragment name",
           "type": "[component|react]",
           "icon": "adjust"

           // This configuration is optional and can be removed
           "sass": {
               "loadPaths": [
                   "../../../node_modules"
               ]
           }
       }

Add a Fragment Set to the Project

Use the add-collection command to add a Fragment Set to your project.

  1. Navigate to your project’s root folder and run this command.

    yarn run add-collection
    
  2. When prompted, enter a name and description for the Fragment Set.

    > yo liferay-fragments:collection
    ? Collection name (required) My Set
    ? Collection description (optional) This is my new Fragment Set.
    create src/my-set/collection.json
    
  3. Verify your new Fragment Set was created successfully in the project’s src folder. It should only include a collection.json file:

    {
        "description": "This is my new Fragment Set.",
        "name": "My Set"
    }
    

Create a New Fragment

Use the add-fragment command to add a new Fragment to the Set.

  1. Navigate to your project’s root folder and run this command.

    yarn run add-fragment
    

    The CLI starts the process:

    > yo liferay-fragments:fragment
    
  2. Name your Fragment.

    ? Fragment name (required) My Jumbotron
    
  3. Choose whether to use React or another JavaScript framework. React requires Liferay 7.3+. For this tutorial, please decline using React.

    ? Use React (or other JS framework)? No
    
  4. Use the new editable element syntax for Liferay 7.3+.

    ? Use new data-lfr editable syntax? Yes
    
    note

    If you accepted using React in your fragment, the toolkit assumes you’re on Liferay 7.3+ and configures the new editable element syntax.

    note

    Please see Fragment-Specific Tags for more information on Liferay’s editable data syntax.

  5. Select the Fragment Set you just created (My Set).

    ? Choose a collection
        My Set
        Sample Set
        + New Collection
    
    note

    The fragment.json defines the paths to the Fragment’s CSS, HTML, and JavaScript. If you change any of these file names, update their paths in the fragment.json.

Here is the Fragment’s index.html:

<div class="my-jumbotron">
    <h1 data-lfr-editable-id="title" data-lfr-editable-type="text">
        My Jumbotron
    </h1>
</div>

The Fragment HTML uses the new data-lfr editable syntax.

Edit Your Fragment

Modify any part of your Fragment.

  • HTML (index.html)
  • CSS (styles.css)
  • JavaScript (main.js)
  • Fragment Attributes (fragment.json)
  • Configuration options (configuration.json).

For example, you can build off of the above HTML and use Clay’s Bootstrap-based components to create Fragment with an attention-getting headline and description, as demonstrated in this index.html:

<div class="component-my-jumbotron">
    <div class="jumbotron">
        <h1
            class="display-4"
            data-lfr-editable-id="01-title"
            data-lfr-editable-type="rich-text"
        >
            Editable Jumbotron Headline
        </h1>

        <p
            class="lead"
            data-lfr-editable-id="02-lead"
            data-lfr-editable-type="rich-text"
        >
            Edit this text to call extra attention to featured content or information.
        </p>

        <hr />

        <p
            data-lfr-editable-id="03-text"
            data-lfr-editable-type="rich-text"
        >
            Edit this text to provide more information.
        </p>

        <a
            class="btn btn-primary btn-lg"
            data-lfr-editable-id="04-label"
            data-lfr-editable-type="link"
            href="#"
        >
            Editable Link
        </a>
    </div>
</div>
note

If you are using Liferay 7.2 or below, remove the data-lfr-editable-[id|type] attributes and wrap the content elements in lfr-editable elements as described in Fragment-Specific Tags.

The first div element’s class="component-my-jumbotron" attribute attempts to uniquely identify this Fragment on a page.

tip

In your Fragment’s HTML file, use the main wrapper element (the <div> in the example above) to uniquely identify the Fragment so it doesn’t conflict with other components on a page.

Next the <div class="jumbotron"/> element wraps the content, applying Bootstrap’s jumbotron component. This component makes the content stand out. Here are the content elements:

  • <h1 class="display-4" ...>Editable Jumbotron ... creates the Fragment’s heading. It uses Bootstrap’s display-4 size style. The data-lfr-editable- attributes make the heading text editable. The data-lfr-editable-id="03-text" attribute identifies the element and the data-lfr-editable-type="rich-text" attribute declares the content type.
  • <p class="lead" ...>Edit this text ... is the lead body text distinguished by Clay’s lead style component. The data-lfr-editable- attributes make the paragraph editable.
  • The <hr/> and next <p ... elements produce a horizontal rule and another editable paragraph, respectively.
  • <a class="btn btn-primary btn-lg" ... specifies a modifiable link. The btn-primary class styles it as a main button and btn-lg makes it large. The href="#" attribute takes the user to the top of the page when the link is clicked. The data-lfr-editable-type="link" attribute makes the link editable.

You can also include resources in your Fragments. See Including Default Resources in Fragments for more information.

note

The Fragment and configuration object are passed in as arguments in JavaScript and are available as fragmentElement and configuration parameters, respectively.

Import Your New Fragment

You can import your new Fragment as you did the original example Fragment:

  1. Run the import command and provide your credentials:

    yarn run import
    
  2. Verify the Fragment Set is available. Open the Site Menu (Site Menu) and go to DesignFragments. The Set should appear in the list.

The Set is available.

Great! Now you know how to use the Fragments Toolkit to create and manage Fragments. See the Fragments Toolkit Command Reference for more toolkit command information.