Content and content types
In this chapter, we’ll start working with sites and content types in XP. We’ll look at how we create content and how it’s stored, as well as how to create and publish sites. To achieve this, we’ll start working with and getting familiar with Content Studio, XP’s content editing suite.
This chapter gives a brief introduction to Content Studio and how to use it. For a more comprehensive overview, watch this short video on Content Studio basics or head to the Content Studio reference documentation.
|The tasks and examples in this chapter are based on work done in previous chapters (starting with the sandboxes chapter). If you want to follow along with the examples, make sure you’re all caught up.|
Content types are schemas that tell XP what properties a piece of content of a particular type can have. Think of a piece of content as a collection of data. Its content type describes what data that piece of content can and must have.
If you’re used to working with databases, you can think of content types as table schemas and contents as the rows of a table. The properties are the columns specified by the schema. Similarly, a content type is much like a (data) type or a class in many programming languages: something that describes the shape of your data. A piece of content is a manifestation/instance of that data type. The properties of the content type specify what that data type contains.
|Content types belong to XP applications. If you stop or remove an application that contains a content type, you’ll no longer be able to create content of that type, and rendering and previewing content of that type will not work.|
Now that we know what a content type is, let’s look at how we create them and how we make XP recognize them. We’ll create a content type that contains two input fields. When shown in Content Studio, it will look like this:
To get started, make sure you’re in the project folder (
myproject) that you created in a previous chapter. All paths will be relative to this root folder.
Create a directory for content types:
src/main/resources/site/content-types. XP is quite particular about project folder structure, so you need to put content types right where it expects to find them.
Create a directory called
artistin the newly created
content-typesdirectory. XP expects all content types to have their own directories.
artistdirectory, create a file called
artist.xml. The file name must match the directory name (except for the file extension). The full path to the content type from the project root should thus be
artist.xmlfile, place the following contents:The 'Artist' content type
<content-type> <display-name>Artist</display-name> (1) <description>Information about an artist</description> (2) <super-type>base:structured</super-type> <form> (3) <input name="name" type="TextLine"> (4) <label>Name</label> <help-text> The artist's name (if different from their professional moniker). </help-text> </input> <input type="TextArea" name="about"> <label>About the artist</label> </input> </form> </content-type>
1 This is the name of the content type and is what will be displayed in Content Studio when you create a piece of content of this type. 2 This is a short description of what the content type is. It’s not strictly required, but it’s highly recommended to add a description to all your content types. 3 This is where we specify what properties this content type contains. The contents of this tag control what we see and are able to edit in Content Studio. 4 This is a property of the content type. The
typeattribute specifies what kind of input field the user will see in Content Studio, and the
labelspecifies how the input will be labeled.
Content types also have a large number of other, optional fields that we won’t get into here. If you’d like to learn more about this, consult the reference documentation. XP’s
inputelements are not the same as HTML
inputelements. To read more about it, see the chapter on input types. .
enonic project deploy
If everything goes well, you should see your sandbox logs update now. And that’s the basics of creating new content types. Next, let’s turn our eyes to Content Studio and create some actual content!
As mentioned in the introduction, Content Studio is XP’s content management interface. Use it to navigate, create, edit, and publish content and sites. Content Studio also lets you preview pages and other content types that have a visual component.
To access Content Studio from the XP landing page (or anywhere in XP), use the admin menu and select "Content Studio".
Wanna get a quick overview over Content Studio? Watch this short video on Content Studio basics for a quick overview of how Content Studio works and what you can do with it. If you’d rather just keep reading: we’ll introduce everything as we go along, so you should be able to follow anyway.
Now we’re finally ready to create some actual content! With your sandbox up and running, navigate to Content Studio. You should be met with an empty studio that tells you are "wasting this space":
Before we can create our content, we need a site to hold it. Sites are a special content type that ships with Content Studio. It’s one of a very small number of content types that we can place at the very root of the content hierarchy. A site is like a root node that we can nest other content under. When we create a new site, we can also specify which applications it uses, and that’s where our application comes into play.
To get going, press the "New…" button in the toolbar. This will bring up a "Create Content" dialog that looks something like this:
You can also use the keyboard shortcut Alt+n to bring up the "Create Content" dialog.Figure 3. The Content Studio create content dialog
Select "Site" either by clicking on it, or by navigating to it with your keyboard. You can start typing as soon as the dialog opens to filter the options too!
When you select it, your browser will take you to a new tab (or window, depending on your browser settings). This is the content creation form (or wizard).Figure 4. Creating a new site in Content Studio.
Add the site name in the first field (with the
<Display Name>placeholder). It’s our first site, so let’s go with "My First Site". Notice that when you do this, the path below the input field gets automatically updated to match what you input. You can manually override the path if you wish, but we’ll leave it as is for now.
The description field is used to describe what the site is about. It’s an optional field, so you can either leave it blank or add a description of your liking.
The next field, "Applications", is important. This is where we tell Content Studio what sort of applications the site uses and thus what content types we can create within it. Select the "Headless Starter" app.
Your site form should now look a little something like this.
To confirm and return to the Content Studio hierarchy, press "Save" in the menu bar. Feel free to close the tab (you won’t be needing it again).
|You can also use the keyboard shortcut ctrl+s on Linux and Windows and ⌘+s on macOS to save your content. Additionally ctrl+enter/⌘+enter saves the content for you and then closes the tab for you.|
When you return to the Content Studio hierarchy view, you should now see your site listed. If you expand it, you’ll find that there is also a node called "Templates" underneath it. We can ignore this for now, but we’ll learn more about it later.
With the "My First Site" site highlighted, press "New …" to open the "Create Content" menu again. This time around, you should have some more choices than what you did previously, including our very own content type "Artist"!
However, before we start creating artists, let’s create a folder. Because we’ll be using this site in the coming chapters with more content types, having some hierarchy will make things easier. Choose a new folder and call it "artists".
Next, let’s create a few artists to get used to the workflow. We’ll start with Cardi B.
Select the artists folder (make sure it’s highlighted).
Create a new artist.
Give it the display name "Cardi B".
Use these details to fill in the form:Artist details (Cardi B on Wikipedia)
Figure 7. The content form filled out for the artist "Cardi B"
Belcalis Marlenis Almánzar
Belcalis Marlenis Almánzar (born October 11, 1992), known professionally as Cardi B, is an American rapper and songwriter.
Again, save and close the tab. Now repeat this process for these next two entries. Be aware that Content Studio lets you nest content, so if you try and create new content when "Cardi B" is selected in the hierarchy, it’ll nest the new content under "Cardi B". In our case, though, we want the artists to be nested directly under the current folder, so make sure "artists" folder is selected when you create new content.Missy Elliott (Missy Elliott on Wikipedia)
P!nk (P!nk on Wikipedia)
Melissa Arnette Elliott
Melissa Arnette Elliott (born July 1, 1971) is an American rapper, singer, songwriter, and record producer.
Alecia Beth Moore
Alecia Beth Moore (born September 8, 1979), known professionally as Pink (stylized as P!nk), is an American singer and songwriter.
With these artists created, your content hierarchy should now list the site, the artists folder, and the three artists.
Enonic provides XML Schema Definitions (XSDs) for text editor integration and validation of your XML schemas. To use it, add the attribute
xsi:schemaLocation to your content types as shown below:
<?xml version="1.0" encoding="utf-8"?> <content-type xmlns="urn:enonic:xp:model:1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:enonic:xp:model:1.0 https://raw.githubusercontent.com/enonic/xp/master/modules/core/core-api/src/main/resources/META-INF/xsd/model.xsd"> <display-name>Content type display name</display-name> <!-- rest of content elided for brevity --> </content-type>
Note that in-editor schema validation depends on whether your text editor supports validating against XSD schemas or not. If your editor doesn’t support it out of the box, it may be available via a Language Server Protocol (LSP) client.
You might have noticed the yellow circles with exclamation marks displayed on all the content we’ve been working on, both in the wizard and in the content grid. This is how Content Studio indicates whether something is published or not and whether there are any errors with it. There are four states:
- Error (red circle with a cross)
This indicates that there is something wrong with your content. Because XP uses structured content it can also validate content as it is created. If something doesn’t conform to the specified schema (for example: a required field has no value), it will fail to validate and be marked with this symbol.
- Work in progress (yellow circle with exclamation mark)
This symbol indicates that this piece of content has unpublished changes. That could be either because this is new content that has never been published, or it could be because there have been changes to this piece of content since it was last published.
- Ready for publishing (green circle with a check mark)
This piece of content is marked as ready to be published.
- Published (no icon)
This piece of content is published and has not been modified since.
So what does it mean for content to be published? It doesn’t make much of a difference to us at this stage of the tutorial, but for when you’re working on production systems, it’s very important. So let’s talk briefly about Content Studio’s branching and publishing system.
Content Studio operates with two branches: a draft branch where you work and edit, and a branch that holds the published content, known as the master branch. When you work on something in Content Studio, you’re working on the draft branch. When you publish something, the master branch gets updated with the new content. When you edit that same piece of content in Content Studio again, you’re back to working on the draft, and you have to publish it to see changes to the published content.
To publish a piece of content, right click it and select "Publish".
|You can also publish a piece of content by using the keyboard shortcut ctrl+Alt+P/⌘+Alt+P.|
Let’s give this publishing thing a try! We’ll publish the whole site at once. Select your site and publish it. You’ll be greeted by the Content Studio publishing wizard.
You’ll find that the "Publish now" button isn’t available just yet. For content to be published, you must first mark it as ready. But before we do that, we should also make sure that we select all the content that belongs under the site too. By default, publishing a piece of content doesn’t publish items nested under it. To include nested content, press the "include child items" icon to the left of the content icon (the one that looks sort of like a fork or a three-legged octopus). Next, select the "mark as ready" option from the button dropdown. It’ll mark your content as ready to publish. Now press "publish now".
Select your site. Publish.
Include child items.
Mark as ready.
One of the applications we installed previously is Data Toolbox. Data Toolbox provides you with a web interface to visualize and manipulate your XP data. We’ll now see how to inspect our content using Data Toolbox.
Open the XP admin menu and select Data Toolbox. You should have this app installed from when you took the welcome tour.Figure 11. Data Toolbox
Navigate to the "Data Tree" menu. In this menu, the toolbox lists repositories within your XP instance. There are three repositories listed:
This is the repository that holds your content.
This is where the audit log is stored. The audit log allows you to see a log of everything that’s been done in your XP instance, such as content creation, user administration, etc., and also tells you when it was done and by whom.
This is where XP stores data about your system, such as external applications and identity data.
Navigate through to
Next up is a list of branches (as discussed above). As long as you’ve published your content, you can choose either one of these.
Keep navigating further down the tree:
When you’ve reached
my-first-siteyou should see all the content of your site listed. Navigate to
artiststo find actual pieces of content.
Choose one of your artists and check out both the info view and the JSON view, available via the buttons in the table row.Figure 12. The Data Toolbox node info view
To inspect the data associated with the artist, you can select "Display properties", which will bring up data about the node, such as its display name, its owner, when it was created, etc.Figure 13. The display properties view
Finally, you can navigate to the
dataproperty, where you’ll find the data stored on the artist.Figure 14. The properties.data view
Inspecting content is only a fraction of what Data Toolbox can do: it’s a versatile and powerful administrative tool, and we’ll see more of it in a later chapter.