Custom Layout
The default layouts that are normally used with a content item will rarely be
sufficient for all of the App scenarios. For example, the default Card
layout
that is available out of the box is a good start but may not be applicable for
all pages of the app. In some cases, you would want an enlarged view of the same
card and in some cases a mini-view.
To cater to all of these scenarios, the Vyuh Framework allows extending a Content Item with additional layouts. In fact, any feature can provide these layouts, allowing even unrelated features to contribute to the overall list of layouts for a content item.
In this guide, let’s add a custom layout for the ProductCard
item we created
in another article. If you haven’t seen the article yet, take a quick read and
come back here.
The default layout for the ProductCard
item, that we created earlier, looks
like below.
We will change things up a bit and make the image come on the left and show just
the title
, price
and category
. This will be our mini-view of the
ProductCard. Let’s get started.
1. Creating a Mini-view layout schema
The schema that we will use to represent the mini-view layout configuration will
be a simple one. We will have a simple boolean to control whether the category
should be shown or not. Thus our schema looks like so:
There are couple of things to note in this schema. Firstly, our schema is no
longer a simple string but a template string that uses the
ProductCardDescriptor.schemaType
as its prefix. The reason why we have
switched to using a ProductCardDescriptor
is partly for convention and also
because we need a custom ContentDescriptor
to allow adding more layouts.
Creating the ProductCardDescriptor
and ProductCardContentBuilder
The descriptor and builder for the Product content item are fairly simple as
there is no custom configuration being created. We rely on the built-in
capability of the base ContentSchemaBuilder
to do all the work of assembling
layouts.
Notice that the content builder associates itself with the ProductCard
using
the ProductCardDescriptor.schemaType
.
2. Exporting in the FeatureDescriptor
Now that we have our core elements ready, its time to export them in the
FeatureDescriptor
. We do that like so:
The main lines to notice are from 22-25
, where we export the default layout
and the mini-view layout. Now we have an option to chose this layout when
creating the Product item in the CMS, as seen below.
3. Creating the Dart LayoutConfiguration
The Dart counterpart for the layout is just a type-safe version of the JSON
schema we created earlier. This can be seen below. We also see a simplified
layout of the ProductCard
in the build()
method.
4. Exporting it in the feature
By including the layout in the FeatureDescriptor
, via the
ProductCardDescriptor
(line 10), we can now be sure that it will be picked
up and rendered correctly.
5. Seeing it in action
Below you can see two product cards, each rendered with the mini and default layouts.
Summary
This article showed you the way to create multiple layouts for a ContentItem
.
By simply changing it on the CMS, we are able to affect the change on the App
without any special deployment. This is a hidden super power of the framework,
where you can try out different layouts without changing much of your original
code.
Under different contexts, different layouts are useful and this capability is now available to you as you build more complex applications.