【发布时间】:2020-12-18 01:06:30
【问题描述】:
我有两个模块,Blog 和 Authors。对于每篇博客文章,我想从 Authors 模块中选择作者并显示他的姓名和照片:
blog/index.js:
module.exports = {
extend: 'apostrophe-pieces',
name: 'blog',
label: 'Article',
pluralLabel: 'Articles',
contextual: true,
sort: { date: -1 },
arrangeFields: [
{
name: 'content',
label: 'Content',
fields: [ 'title', 'shortTitle', 'slug', 'published', 'tags', 'date', 'perex', 'shortPerex', '_author', '_thumbnail' ]
}
],
addColumns: [
{
name: 'date',
label: 'Date',
sort: {
date: -1
}
},
{
name: 'slug',
label: 'Slug',
sort: {
slug: 1
}
}
],
addFields: [
{
name: 'title',
label: 'Title',
type: 'string',
required: true
},
{
name: 'shortTitle',
label: 'Short title',
type: 'string',
required: false
},
{
name: 'shortPerex',
label: 'Short perex',
type: 'string',
textarea: true,
required: false
},
{
name: 'perex',
label: 'Long perex',
type: 'string',
textarea: true,
required: false
},
{
name: 'date',
label: 'Date',
type: 'date',
pikadayOptions: {
firstDay: 1
},
required: true,
sortify: true
},
{
name: '_author',
type: 'joinByOne',
withType: 'author',
label: 'Author',
idField: 'authorId',
},
{
name: '_thumbnail',
type: 'joinByOne',
withType: 'apostrophe-image',
label: 'Thumbnail',
required: true,
}
]
};
authors/index.js
module.exports = {
extend: 'apostrophe-pieces',
name: 'author',
label: 'Author',
pluralLabel: 'Authors',
searchable: false,
arrangeFields: [
{
name: 'basics',
label: 'Basics',
fields: [ 'title', 'nameCZ', 'nameEN', '_thumbnail', 'slug', 'published', 'tags' ]
}
],
addFields: [
{
name: 'name',
type: 'string',
label: 'Name',
required: true
},
{
name: '_thumbnail',
type: 'joinByOne',
idField: 'thumbnailId',
withType: 'apostrophe-image',
label: 'Picture',
required: true,
}
]
}
blog-pages/index.js:
module.exports = {
extend: 'apostrophe-pieces-pages',
perPage: 99,
sort: { date: -1 },
}
blog-pages/views/show.html:
{% extends 'layout.html' %}
{% block title %}{{ data.piece.title }} | Costlocker{% endblock %}
{% block main %}
{% set item = data.piece %}
<h5>Author:</h5>
<pre>{{ item._author | json}}</pre>
<hr>
<h5>Thumbnail:</h5>
<pre>{{ item._author._thumbnail | json}}</pre>
{% endblock %}
结果:
<h5>Author:</h5>
{
"_id": "ckiolmxue003kenrrqjdj20vz",
"published": true,
"trash": false,
"siteMapPriority": null,
"type": "author",
"title": "David Maralík",
"name": "David Maralik",
"thumbnailId": "ckiolmjqb0033enrr4ol0wl0u",
"slug": "david-maralík",
"tags": [
],
"openGraphTitle": "",
"openGraphDescription": "",
"openGraphType": "",
"openGraphImage": {
"items": [
],
"type": "area",
"_edit": true,
"_docId": "ckiolnuto0058enrr99bl4d4j",
"_dotPath": "_author.openGraphImage"
},
"workflowLocale": "en-draft",
"workflowGuid": "ckiolmxue003jenrric96iw8g",
"createdAt": "2020-12-14T13:34:54.422Z",
"titleSortified": "david maralík",
"updatedAt": "2020-12-14T13:34:54.424Z",
"highSearchText": "david maralík david maralík david maralík david maralík david maralik david maralík",
"highSearchWords": [
"david",
"maralík",
"maralik"
],
"lowSearchText": "david maralík david maralík david maralík david maralík david maralik david maralík",
"searchSummary": "",
"viewGroupsIds": [
],
"viewUsersIds": [
],
"editGroupsIds": [
],
"editUsersIds": [
],
"docPermissions": [
],
"editGroupsRelationships": {
},
"editUsersRelationships": {
},
"loginRequired": null,
"viewGroupsRelationships": {
},
"viewUsersRelationships": {
},
"workflowLastEditor": "admin",
"workflowLastEditorId": "ckioleo9j00068qrr59o3k4r4",
"workflowModified": false,
"workflowLastCommitted": {
"at": "2020-12-14T13:41:52.420Z",
"user": {
"username": "admin",
"title": "admin",
"_id": "ckioleo9j00068qrr59o3k4r4"
}
},
"_edit": true,
"_publish": true,
"_originalWidgets": {
}
}
<h5>Thumbnail:</h5>
null
如何获得_thumbnail.attachment的Author?
谢谢
【问题讨论】:
标签: apostrophe-cms