Skip to content

Commit a3a8be4

Browse files
AndyButlandiOvergaard
authored andcommitted
Templates: Retain layout from file when loading template (closes #20524) (#20529)
Retain layout from file when loading template.
1 parent d17ba80 commit a3a8be4

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/Umbraco.Web.UI.Client/src/packages/templating/templates/workspace/template-workspace-editor.element.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class UmbTemplateWorkspaceEditorElement extends UmbLitElement {
102102
}
103103

104104
#resetMasterTemplate() {
105-
this.#templateWorkspaceContext?.setMasterTemplate(null);
105+
this.#templateWorkspaceContext?.setMasterTemplate(null, true);
106106
}
107107

108108
#openMasterTemplatePicker() {
@@ -121,7 +121,7 @@ export class UmbTemplateWorkspaceEditorElement extends UmbLitElement {
121121
?.onSubmit()
122122
.then((value) => {
123123
if (!value?.selection) return;
124-
this.#templateWorkspaceContext?.setMasterTemplate(value.selection[0] ?? null);
124+
this.#templateWorkspaceContext?.setMasterTemplate(value.selection[0] ?? null, true);
125125
})
126126
.catch(() => undefined);
127127
}

src/Umbraco.Web.UI.Client/src/packages/templating/templates/workspace/template-workspace.context.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ export class UmbTemplateWorkspaceContext
6767

6868
override async load(unique: string) {
6969
const response = await super.load(unique);
70-
await this.setMasterTemplate(response.data?.masterTemplate?.unique ?? null);
70+
71+
// On load we want to set the master template details but not update the layout block in the Razor file.
72+
// This is because you can still set a layout in code by setting `Layout = "_Layout.cshtml";` in the template file.
73+
// This gets set automatically if you create a template under a parent, but you don't have to do that, you can
74+
// just set the `Layout` property in the Razor template file itself.
75+
// So even if there's no master template set by there being a parent, there may still be one set in the Razor
76+
// code, and we shouldn't overwrite it.
77+
await this.setMasterTemplate(response.data?.masterTemplate?.unique ?? null, false);
78+
7179
return response;
7280
}
7381

@@ -79,9 +87,9 @@ export class UmbTemplateWorkspaceContext
7987
},
8088
});
8189

82-
// Set or reset the master template
83-
// This is important to reset when a new template is created so the UI reflects the correct state
84-
await this.setMasterTemplate(parent.unique);
90+
// On create set or reset the master template depending on whether the template is being created under a parent.
91+
// This is important to reset when a new template is created so the UI reflects the correct state.
92+
await this.setMasterTemplate(parent.unique, true);
8593

8694
return data;
8795
}
@@ -102,7 +110,7 @@ export class UmbTemplateWorkspaceContext
102110
return this.getData()?.content ? this.getLayoutBlockRegexPattern().test(this.getData()?.content as string) : false;
103111
}
104112

105-
async setMasterTemplate(unique: string | null) {
113+
async setMasterTemplate(unique: string | null, updateLayoutBlock: boolean) {
106114
if (unique === null) {
107115
this.#masterTemplate.setValue(null);
108116
} else {
@@ -113,7 +121,10 @@ export class UmbTemplateWorkspaceContext
113121
}
114122
}
115123

116-
this.#updateMasterTemplateLayoutBlock();
124+
if (updateLayoutBlock) {
125+
this.#updateMasterTemplateLayoutBlock();
126+
}
127+
117128
this._data.updateCurrent({ masterTemplate: unique ? { unique } : null });
118129

119130
return unique;

0 commit comments

Comments
 (0)