Blocks
Are main building part of pages they are sections of your page that have their own styling and are indenpended on each other. They are ment to be used by end user to adjust pages to their needs.
Adding new Blocks
Before using block in FE code it must be defined in Directus.
Define block in Directus
Create collection named
block_NewTantalumBlock
withid
beingUUID
.DANGER
Follow naming scheme of
block_<name-of-block-in-upper-cammel-case>
eg.block_NewTantalumBlock
this way we can differentiate data model types as well as get name of Vue component in Frontend. If you do not follow this convention new block will not load and Vue will throw errors. Same goes for UUID. This is very important as you cannot change it later.In collection add following fields:
block_NewTantalumBlock - headline (Type: String, Interface: Input) - description (Type: Text, Interface: Textarea)
(Optional) You can add custom name for your block. By default it will be changed to:
Block New Tantalum Block
. If you wish to change it scroll to Collection Naming Translations and click Create new in fill all required fields in form. I will skip this part.For organization move created collection to
blocks
folder. This way we can also easly hide it.
Allow for selection in content type
- Navigate to settings of
pages_translations
collection. - Click
blocks
input so that you can see it's settings. - Go to Relationship tab.
- Click Related Collections and search for newly created block (
block_NewTantalumBlock
), add it to allowed blocks by checking checking checkbox. - Save input settings.
- (Optional) You can do the same to
posts_translations
if you have it.
Adjust security policy
- In settings go to Access Policies.
- Click on Public.
- Scroll to bottom and click on Add Collection at the bottom of the Permissions list.
- Search for newly created block (
block_NewTantalumBlock
) and add it to the list. - Allow All access to Read action.
Add block to page
- In directus open page that you would like to add your block too. I will use homepage.
- Scroll to block settings and click Create New
- From list select your block.
- Fill your content.
- Save block.
- Save page settings (Ctrl + S).
Adding block in Nuxt
After defining block in Directus it is time to add it to our FE code.
WARNING
Before we continue restart your Nuxt enviroment as this will generate Directus types needed for Vue. That way we can get type support.
Create Vue component
- Create Vue component named
NewTantalumBlock.vue
in your FE code under app > components > blocks - Add script setup snippet into component
<script setup lang="ts">
import type { BlockNewTantalumBlock } from '#build/directus-types';
defineProps<BlockNewTantalumBlock>();
</script>
- Add your template tag. Here is example one
<template>
<section class="w-full flex justify-center py-12">
<div class="container w-full max-w-screen-xl">
<h2 class="text-4xl font-bold text-center mb-8">{{ headline }}</h2>
<p class="text-center">{{ description }}</p>
</div>
</section>
</template>
Because of Directus SDK we are automatically getting all types for that block. This together with Typescript support in Vue allows us for easy block definition.
WARNING
For now you wont see anything on your page check next section to see block on page.
- Because of multilangual setup and improved type support we have to modify Vue component to tell directus witch fields to get. Modify your Vue component script tags like below.
<script lang="ts">
import type { Schema, BlockNewTantalumBlock } from '#build/directus-types';
import type { QueryFields } from '@directus/sdk';
export const block_NewTantalumBlock: QueryFields<
Schema,
BlockNewTantalumBlock
> = ['id', 'headline', 'description'];
</script>
<script setup lang="ts">
import type { BlockNewTantalumBlock } from '#build/directus-types';
defineProps<BlockNewTantalumBlock>();
</script>
Add new block to retrieve
- Navigate to app > > shared > availableBlocks.ts
- Import exported
block_NewTantalumBlock
from your Vue component. Then add it toavailableBlocks
// ..
import { block_NewTantalumBlock } from "~/components/Blocks/NewTantalumBlock.vue";
export const availableBlocks = {
// ..
block_NewTantalumBlock
};
// ..
- That's it 🎉. Now opening your homepage should display your new block. If you've not changed order of blocks it should be on the bottom, right above newsletter.
Editing existing blocks
- Open your block collection data model in directus, add/remove fields.
- Restart Nuxt enviroment.
- Open Vue component responsible for that block and delete/add fields that you changed.
- That's it 🎉.
DANGER
Deleting field will cause all data for that field to be deleted. This cannot be undone.
Removing blocks
- Open your block collection data model and delete it using trash icon in top left corner.
- Restart Nuxt enviroment.
- Remove Vue component.
- Remove block import from [[page_name.vue]].
- Remove block from
availableBlocks
. - Go to pages where you've used that block and remove it. It will be called "Invalid Item".
- That's it 🎉.
DANGER
Deleting block will remove all blocks from your pages. This cannot be undone. If block was not deleted from content type item it will show as Invalid.