Update YouTube component – Get data from component using Sightly

In this session, you will see how to update the YouTube component to capture the URL entered by an author in the dialog. (I have uploaded the sample code in GitHub.)

We have accomplished the following so far.

  1. Created a YouTube component that will add a specific YouTube video to the page.
  2. Created a dialog where the author can enter a different URL.

The requirement was to update the video using the URL entered by the author.

Let us start. We will create a JavaScript file named url.js to write our logic.

  1. Create a js file url.js.
  2. Copy and paste the following code:
use(function () {
var CONST = {
PROP_TITLE: "jcr:title",}
var url = {};
// The url entered in the dialog
url.text = granite.resource.properties[CONST.PROP_TITLE]
if (url.text ==null)
 {
 url.text = "https://www.youtube.com/embed/dI1yi2mmNuo";
 }
// Adding the constants to the exposed API
url.CONST = CONST;
return url;
});

Let us go through the code line-by-line. It creates a JavaScript function. It creates a constant PROP_TITLE and assigns jcr:title to it. It is a very important property. Let us take a look at the JCR properties after you add a value to the component’s dialog.

Go to content > aem-company > jcr: content > content > youtube_1.

Note that jcr:title property captures the URL entered in the dialog. We have entered the name of the dialog as ./title and that’s the reason its value is captured as jcr:title. The code creates a variable url. Added a text property to the URL and it captures granite.resource.properties[CONST.PROP_TITLE], basically the URL entered by the authors. Then the code checks if the url.text property is null. It’s null when there is no value entered in the dialog. In that case, we are assigning a default value. We are returning url as a constant.

Now let us change the code that we wrote in the YouTube.html to use the script.  Copy and paste the following code to YouTube.html.

<div data-sly-use.url="url.js" style="overflow: hidden;">
     <iframe width="600" height="510" src="${url.text}" frameborder="0" allowfullscreen></iframe>
</div>

The data-sly-use attribute assigns the return value of url.js to the url variable. We extract the url from the url property using ${url.text}. See how simple it is.

Now let me give you another URL to use. https://www.youtube.com/embed/kl3Hz-4x4L0 (This is another YouTube video that I shared some time back. It has a title, AEM Authors, Templates, Components.)

  1. Drag and drop the YouTube component to the web page.
  2. Select it and select Edit.
  3. Enter the URL that I gave above.
  4. Select OK.

Note that the video is updated.

There are lots of pitfalls in the component. At the first place, it does not check if the entered string is a URL or not. Also, no need to display a video at the first instance, and so on. The intention was to show you how to capture a user input from the dialog; and we have succeeded.
See you in the next session.

Creating AEM dialogs

In this short session, we will see how to create AEM dialogs. This is primarily an overview session about AEM dialogs.
I have shared the package in GitHub.

AEM dialogs are used to enter information to a component. For example, if you open a page that I showed here and double-click the Title element, a dialog appear. You can enter certain information, such as Title, Link, and so on. Just to demo, let me change the title to my name, Sunil, and see. You could see that the title of the page is changed.

Dialog you just saw is a touch-optimized dialog – it means this dialog is optimized to view in various devices, such as phones and tablets. AEM used to have a different dialog. Let us see it. Go to the Classic view. Double-click the title page. I hope you noticed the difference. This dialog is used in Desktops. Most of your users may want to use touch-optimized dialogs that enable them to work on hand-held devices. And of course, that’s the future.

I will first introduce you to the latter – the dialog that’s not touch-optimized – the Classic dialog. We will see how to create a dialog for the YouTube component. We have seen the component adds a YouTube video to the page. Author wants to change the URL of the YouTube video. So, the requirement is to provide them with a dialog that allows them to enter a URL.

Delete the dialog that you created earlier. Let us copy a dialog from an existing component. Then, we will refine it based on our requirement. This is always a good strategy, because we are starting with something tried and tested. We can use the dialog of a component named, title.

  1. Delete the dialog you already created.
  2. Copy the dialog from /libs/wcm/foundation/components/title/dialog.
  3. Paste dialog underneath youtube.
  4. Remove /youtube/dialog/items/title/type.
  5. Select title and update it.
    Field Label: YouTube URL
    Field Description: Enter YouTube URL.
    (This is an instruction for authors.)
  6. Don’t change the name.
    xtype defines what exactly the field is. It’s a text field.

Now let us see how the dialog appears in the web page.

  1. Open the web page.
  2. Double-click.
  3. See that the dialog appears.

Let us wind up the session here. Remember, we need to add the logic to capture URL entered here to update the web page. We will see how to accomplish that in the next session.

See you all soon!!

Create your first Sightly component

In this session, you will learn how to create an AEM component. You have learned that an AEM component is used to display an information in a certain way. You will use Sightly to create the component.

We will call the component as YouTube component. When you drag and drop the component to a web page, it will display a YouTube video – specifically, a YouTube video I uploaded as a part of this learning series.

So, let us start:

  1. Login to CRXDELite.
  2. Navigate to the directory stucure we have created earlier.
  3. Go inside the Component folder.
  4. Right-click and select Create Component.
  5. Enter the following:
    Label: youtube
    Title: DisplayYouTube
    Description: This component is used to display the YouTube video
    Group: AEM Company
  6. Click Next and Next.
  7. Enter Allowed Parent as */parsys
  8. Click Next and Click OK.
  9. Now, rename youtube.jsp to youtube.html. (By deafault, CRXDELite creates a JSP component. I have mentioned earlier that we will create the component using Sightly. That’s why we renamed the extension to html.)
  10. Add the following code:
    <div style="overflow: hidden;">
            <iframe width="600" height="510" src="https://www.youtube.com/embed/dI1yi2mmNuo" frameborder="0" allowfullscreen></iframe>
    </div>

    (I have taken the embedded code directly from YouTube. Then changed the width and height. I wrapped it around a div element and added the overflow attribute to remove extra space.)

  11. Create a dialog now. We are not going to do anything with the dialog. However, it’s needed for a newly created component to appear in the sidekick.
  12. Right click YouTube, select Create > Create Dialog.
  13. Enter title as YouTube and select OK.

The component is created now. Let us enable it.

  1. Open the web page you created.
  2. Go to the Design View.
  3. Enable the Component Group; that is, AEM Company.
  4. Go to the Edit view.
  5. From the component group, drag and drop the component to your page.
  6. See the way YouTube video appears.

That’s it. We will see how to customize component shortly.

As usual, I have uploaded the code to my GitHib. Please feel free to use it.

 

Summary: The first step of developing templates in AEM

This post provides a summary of what you learnt in the previous posts.

Before going to the next step, creating components, let us first take a quick recap of all that we learned so far.

AEM users generally belong to one of the following roles:

  • Authors: create web pages. They use templates which provides the layout of the pages; they use components to add information to be displayed on the pages.
  • Developers: create templates and components based on the various requirements of the organization.

We then learned the following web consoles:

  • SiteAdmin: http://localhost:4502/siteadmin : Used by authors to create web pages.
  • CRXDELite: http://localhost:4502/crx/de : Used by developers to explore CRX repository (Oak repository).  Used to create template and components.
  • Package Manager: http://localhost:4502/crx/packmgr/ : Used by developers to share the codes they develop.

Following are the default folders we should be aware of:

  • content  : All pages authors create go here.
  • app : Developers create their company pages here.
  • etc/design : Developers store the design pages.

Inside the app folder, developers can create their company folder. Inside the company folder, the folder for components and templates are placed.

Create template inside the templates folder. A template has the resourceType property which points to a component inside the components folder.  This component is called a page component, because it’s used to create the layout of the page. It has a resourceSuperType property which points to a page component inside libs/foundation/components/page. You inherit this page component, while overriding only the body.html. This is an important concept. I suggest you revisit my blog post on it.

We create the body.html in HTML format, because we want to use Sightly, which is much simpler than JSP. We pasted the HTML content; We added the data-sly-resource tag to include parsys component to a web page. The Parsys component helps to drag and drop components while authoring the web page. We placed the css and inside the etc/design folder. We added the design using the page property.

Authors need to go to the Design mode and add the required components to drag and drop to the web pages.

We are ready to go to the next phase – Creating components. If you have not yet downloaded and installed the packages, please do so. See you soon.

AEM Developers: Applying a design to the AEM Sightly template

In this session, you will learn about adding a design to the template. You will use Sightly, because it’s HTML5 and very easy to use.

This is in continuation of my previous blog posts. As usual, I have provided the video and transcripts below. Video can be watched in the Full screen mode. The sample design and package are in my GitHub repository. Let me know what you think.

Let us take a sample design. Assume that following is what your designer came up with.

design

Now, we need to update the AEM template, so that authors will be able to create pages that look similar to this. You can see some sample texts in the design. The sample text is what authors add in their pages.

For creating any design, you should have a CSS file. Let us see the CSS file first. It’s there in the assets/css location.

First step is to get this CSS in CRX repository (Oak repository). In this case, we have only one file to upload. We can upload it using the CRXDELite itself. In real-time, you may have lots of files to upload to CRX repository. There are many ways to do it. We will see them later.

  1. Go to http://localhost:4502/miscadmin.
    This is a new web console for you. You generally use it for creating pages for designs.
  2. Now go to Designs.
  3. Create a page AEM Company.
  4. Go to CRXDE Lite.
    The page you created is available under the designs folder.
  5. Go to designs > aem-company.
  6. Create an empty file static.css.
    Note that the css name should be static.css.
  7. Expand the file node that you created.
  8. Double-click jcr:data property.
  9. Browse to the CSS location and select it.
  10. Click OK.
    Note that static.css is updated with the css that your designer prepared.
  11. Go to the template we created earlier.
  12. Open body.html.
  13. Update it with the html pages with the html page that your designer provided.
  14. Remove the CSS link. (Assigning the CSS to a page is done at a later stage.)

This is the beauty of Sightly. In earlier versions of AEM, you had to convert the design into JSP, which could be time consuming. Adobe also provides a Brackets extension – a tool for designers. This tool allows designers to work directly with AEM templates. We will explore it later.

  1. Now open the Site Admin.
  2. Under AEM Company, create a new page with name home.
  3. Open the page, the page appears with the sample texts.
    Ideally, that should be removed from the sample page. It’s for authors to fill.
    We have seen how to do it earlier. You need to provide the parsys component for author to add component.
  4. Let us do it in the template.
  5. Remove the sample texts by adding the parsys component.
    <div data-sly-resource="${'content' @ resourceType='wcm/foundation/components/parsys'}"></div>
  6. Remove the sample text for the side content using iparsys.
    <div data-sly-resource="${'side-content' @ resourceType='wcm/foundation/components/iparsys'}"></div>
  7. Note that I changed the name to side-content. This is a mistake that I made in the video.
    The iparsys will not appear propery if you don’t change the name.
  8. Now open the page.

Enabling components in the Components tab

In the last session, we have added a space in the template that allows us to drag and drop a component to the web page. One of the shortcomings in the template was that it didn’t
have any components in the Components tab. In this session, we will add components to the Components tab.

  1. Open the web page we created.
  2. Click the Page Information icon and select Classic UI.
    The same page appears with a different look and feel.
    It’s called Classic UI. Classic UI is specifically designed for viewing in Desktops.
    (The UI you were using is designed for Touch Optimized devices.)
  3. From the sidekick, click the Design tab.
  4. Click Edit from the Design of Content.
    edit-design-par
    You can see various categories of components.
    select-component-category
  5. Select a category or go inside the category and select specific components.
  6. Open the page again and go to the Components tab.
    You will see all the components listed.
  7. Drag and drop the component to the web page and start editing.

Happy editing.

In the next session, we will update the template with a design.

Adding the Parsys component to template

In this session, let us try to modify the template we created earlier. We will add a parsys component in the template to provide a space in the web page to drag and drop components.

Let us once again see how a page created using this template looks like.

  1. Log into to SiteAdmin. (http://localhost:4502/siteadmin)
  2. Double-click AEM Company page that you created. (If you have not created it, download the package that I provided in the previous session. And, then install it.)

The page appears with the text we entered in the template. There are three issues associated with this page:

  •  There is no place where you can drag and drop a component. (If you are not really able to make out that, I suggest you to visit my previous post. You could also open one of the pages from the sample websites to compare.)
  • The Component tab doesn’t display any components.
  • The template is not associated with any design. (Our page is really blank. It doesn’t have anything like header, footer, and so on.)

Let us solve these issues one-by-one. First, we will provide a place in the web page to add components.

  1. Go to CRXDE Lite. (http://localhost:4504/crx/de)
  2. Open the template’s rendering script. (/apps/aem-company/components/homepage/body.html)
  3. Add the following code:
    <div data-sly-resource=”${‘content’ @ resourceType=’wcm/foundation/components/parsys’}”></div>

This is a Sightly code. data-sly-resource is a tag to specify the component that you need to add to the page. It has a resourceType parameter that points to a parsys component. You would had noticed it points to a location where we saw a page component, which we inherited while creating a template. Note that this is the standard procedure to provide a location to add components in a template.

Now refresh the web page. Notice that now it provides you with a space where you can add components.

parsys

I have provided a new package in GitHub. (TemplateWithParsys.zip) Download and use it if you want to.

Summary: Use the parsys component to add space for addition or deletion of components in a web page. You don’t need to create a parsys component. Always reuse the component that exists.

Summary – Creating templates and using packages

In this session, let us summarize what we learned in the previous session: Create the  first template. We created a template in three steps:

  • Step 1: Created the company folder inside apps. Created a folder for components and templates.
  • Step 2: Created a template inside the templates folder. Provided allowed paths as /content(/.*)?. And, we provided resourseType as aem-company/components/homepage. This was a component that didn’t exist at that time.
    resourceType
  • Step 3: We created a homepage component inside the components folder. This is the component that has the rendering script which decides the layout of the page. We named it as homepage, so that we can identify it as a page component (a component that decides the layout of the page, not the component you drag and drop to a web page.) We specified resourceSuperType of this component as wcm/foundation/components/page. Our intention was to inherit the page component. This is a must to get various functionalities in the webpage that authors create. We renamed the default script of component as body.html, so that we override only body.html that existed in wcm/foundation/components/page. Rest of the scripts we inherited.
    resourceSuperType1

This is the standard process to create  a template. I have created a package (FirstTemplate.zip) and shared in my GutHub account. Feel free to download, install, and experiment with it. You need to log into PackageManager to install a package. Packages in AEM helps you to share codes across AEM instances.

  1. Download the package from GitHub. Use FirstTemplate.zip.
  2. Log into PackageManager as admin/admin. (http://localhost:4502/crx/packmgr/index.jsp)
  3. Select Upload Package.
  4. Locate the package you downloaded. Click OK.
  5. All we have done in the previous session will be replicated to your AEM instance.

Create the first AEM template

In the last session, we have explored templates using CRXDE Lite. If you have not yet gone through the last session, go through it.  In this session, we will create a sample template using CRXDE Lite. Let us first get into the shoes of authors to see where exactly authors use a template in their content creation workflow.

  1. Open SiteAdmin. http://localhost:4502/siteadmin
  2. Log in with admin/admin credentials.
  3. In the left pane, go to Geometrixx Demo Site > English.
  4. From the right pane, select New > New Page.
    You see the Title and Name text boxes. (Title appears in the web page and Name becomes the URL.

What you see underneath, namely Geometrixx Content Page, Geometrixx Home Page, Geometrixx Form, are templates. They define the layout of your page.
In that sense, choosing the right template is a fundamental part of content creation.

  1. Now log into CRXDE Lite.
  2. Go to the Apps folder. We have seen earlier that apps is the folder where we create templates and components.
  3. Inside apps, create a new folder.
  4. Right-click apps and select Create Folder.
  5. Type aem-company. (This is our sample company.)
  6. After creating the company, click Save All to save the company. (Whenever you create a node, folder, or property, you need to save explicitly in CRXDE Lite.)
  7. In the similar way, inside aem-company, create two more folders: components and templates.

Now log into CRXDE Lite and create the first template.

  1. Now, right-click templates and select Create Template.
  2. Enter the following details:
  • Label: homepage
  • Title: AEM Company Home Page
  • Description: AEM Company’s Home Page Template
  • Recourse Type: aem-company/components/homepage
    This is the sling:resourseType property. We have seen that, it points to the rendering script. Right now, we have pointed it to a component that doesn’t exit. We will create that script shortly.
  • Ranking: You could leave it for the time being. It represents the order in which templates should appear.
  1. Click Next and enter Allowed Paths as /content(/.*)?. Means the template will apear in the content folder where all pages exist.
  2. Click Next till you see the Finish button.
    (We will learn about these properties at a later stage.)

Now, let us create the rendering script. We create the rendering script as a part of the component. We have already seen that, a component displays a specific information in a specific format. In this case, the component that we are going create, provides the layout of the page. It’s not a component that you can drag and drop to a page. We will name the component as homepage, so that we can identify it as a page component – or a component provides the layout of a page.

  1. Right-click the components folder and select Create > Create Component.
  2. Enter the following details.
  • Label: homepage
  •  Title: AEM Company Home Page Component
  • Description: AEM Company’s Home Page Component
  • ResourceSuperType: wcm/foundation/components/pageThis property decides the component from where you want to inherit properties that decide your page’s behavior.
    You have seen that the pages authors create have a sidekick, assets tab, and so on. We have pointed this to a page component that exists.
    Group: AEM Company

Note that, homepage component is created. It also adds a default rendering script: homepage.jsp. I have mentioned earlier that we used to create components using JSPs before. That’s the reason for the JSP extension. In this session, we will create the template using Sightly, because it’s HTML and easy to use.

Just rename the homepage.jsp to body.html. This is a very important step. The SuperType property points to wcm/foundation/components/page. Though we have not explicitly mentioned, this component exists in the libs folder. Let us locate it. Navigate to libs > wcm > foundation > components > page.

By providing this as the SuperType, we are inheriting the scripts the component has. Go to the page.html which is the rendering script. This script includes two pages: head.html and body.html. By renaming the homepage component’s rendering script’s name to body.html, we are overwriting the body.html. At the same time, we will inherit head.html. (I have explained in the simplest possible way. Let us have a detailed discussion about it later.)

Open the aem-company’s body.html and add some text. Now, go to siteadmin, create a new page. You will see the template you created. Create a page – AEM Company. Open the page and you will see the text you entered. We will modify the template further in the coming sessions.

Introduction to AEM developers

In this post, we will see the very basic responsibilities of developers. Developers create templates and components.

Let us first take a recap: a template defines the layout of a web page. Authors select a specific template before they create a page. A component allows authors to display a specific information in a certain format. For example, a bulleted list is a component. An image is a component. We have seen that you can drag and drop an image to an Image component and it will get displayed in the page.

Authors use SiteAdmin as a tool to create web pages. Similarly, there is a web console that developers use a lot – CRXDE Lite. In one of the previous posts, I have mentioned that the data in AEM is saved as Tar files. AEM uses Apache Oak for saving the data. These data is saved as nodes and properties. CRXDE Lite provides you with an interface where you can see these nodes and properties. It allows you to create and update the nodes and properties.

Let us start our exploration on CRXDE Lite: http://localhost:4502/crx/de
Log in as admin and admin as the credentials.

Let us first examine the web page that you created in the previous session. You created a web page – MyNewPage. In the left pane, move to the Content folder. Then navigate Geomtrixx > en.
You can see the web page you created there. Select the page and then select the jcr:content node. The properties associated with the node gets displayed in the right pane.
You can see a cq:template proptery which provides the location of the template you used. /apps/geometrixx/templates/contentpage
Scroll down to see the jcr:title property – the title of your page. Now select the par node. It represents a node inside which you can add other components. Inside this node, you will see the Text component you added.

In short, the pages an author creates goes to the Content folder. Normally, it’s created through SiteAdmin. Developers won’t play around with these pages.

Developers create templates and components. Where are they hidden? Go to the apps folder. This is the folder where you create templates and components associated with your project. Then go to Geometrixx. You will see various folders: components, config, install, src, and templates. We will learn more about components and templates in this session. Go inside the template folder. You will see all the templates associated Geometrixx site. These were the templates you saw while you were creating a new web page. Select the contentpage node – The template you used for creating a new page. Note that the jcr:primaryType property is cq:Template. It also got a Tile and Description properties. Now select jcr:content property. It has a sling:resourceType property. It points to geometrixx/components/contentpage.  It means that the node pointed, basically a script, provides the layout to render the content.

Let us see how this script looks like. Go to components and select contentpage. You will see a content.jsp. This is the script using which the basic layout is created. All of the existing templates are created using JSP. In AEM’s context, it’s called a templating language. In AEM 6.0 release, Adode introduced a new templating language called Sightly. Sightly is HTML 5 and is very easy to use.

Let us wind up this session here. In the next session, we will create a template using Sightly.
To give you a summary:

  • CRXDE Lite is developers web console: http://localhost:4502/crx/de
  • content is the folder where all author-generated pages exist.
  • apps is the folder where you create templates and componenets associated with your project.
  • cq:template is a property associated with all templates.
  • sling:resourceType property of a template points the script that is used to render the layout.
  • JSP is the templating language in which all templates are written.
  • AEM 6.0 provides a new templating language – Sightly.