diff --git a/src/Umbraco.Web.UI.Client/src/packages/templating/templates/workspace/template-workspace-editor.element.ts b/src/Umbraco.Web.UI.Client/src/packages/templating/templates/workspace/template-workspace-editor.element.ts index a81f4c0f54f4..a40e9c4b06a8 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/templating/templates/workspace/template-workspace-editor.element.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/templating/templates/workspace/template-workspace-editor.element.ts @@ -102,7 +102,7 @@ export class UmbTemplateWorkspaceEditorElement extends UmbLitElement { } #resetMasterTemplate() { - this.#templateWorkspaceContext?.setMasterTemplate(null); + this.#templateWorkspaceContext?.setMasterTemplate(null, true); } #openMasterTemplatePicker() { @@ -121,7 +121,7 @@ export class UmbTemplateWorkspaceEditorElement extends UmbLitElement { ?.onSubmit() .then((value) => { if (!value?.selection) return; - this.#templateWorkspaceContext?.setMasterTemplate(value.selection[0] ?? null); + this.#templateWorkspaceContext?.setMasterTemplate(value.selection[0] ?? null, true); }) .catch(() => undefined); } diff --git a/src/Umbraco.Web.UI.Client/src/packages/templating/templates/workspace/template-workspace.context.ts b/src/Umbraco.Web.UI.Client/src/packages/templating/templates/workspace/template-workspace.context.ts index 35ff79bb43d8..0ea8f97d8cdd 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/templating/templates/workspace/template-workspace.context.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/templating/templates/workspace/template-workspace.context.ts @@ -67,7 +67,15 @@ export class UmbTemplateWorkspaceContext override async load(unique: string) { const response = await super.load(unique); - await this.setMasterTemplate(response.data?.masterTemplate?.unique ?? null); + + // On load we want to set the master template details but not update the layout block in the Razor file. + // This is because you can still set a layout in code by setting `Layout = "_Layout.cshtml";` in the template file. + // This gets set automatically if you create a template under a parent, but you don't have to do that, you can + // just set the `Layout` property in the Razor template file itself. + // So even if there's no master template set by there being a parent, there may still be one set in the Razor + // code, and we shouldn't overwrite it. + await this.setMasterTemplate(response.data?.masterTemplate?.unique ?? null, false); + return response; } @@ -79,9 +87,9 @@ export class UmbTemplateWorkspaceContext }, }); - // Set or reset the master template - // This is important to reset when a new template is created so the UI reflects the correct state - await this.setMasterTemplate(parent.unique); + // On create set or reset the master template depending on whether the template is being created under a parent. + // This is important to reset when a new template is created so the UI reflects the correct state. + await this.setMasterTemplate(parent.unique, true); return data; } @@ -102,7 +110,7 @@ export class UmbTemplateWorkspaceContext return this.getData()?.content ? this.getLayoutBlockRegexPattern().test(this.getData()?.content as string) : false; } - async setMasterTemplate(unique: string | null) { + async setMasterTemplate(unique: string | null, updateLayoutBlock: boolean) { if (unique === null) { this.#masterTemplate.setValue(null); } else { @@ -113,7 +121,10 @@ export class UmbTemplateWorkspaceContext } } - this.#updateMasterTemplateLayoutBlock(); + if (updateLayoutBlock) { + this.#updateMasterTemplateLayoutBlock(); + } + this._data.updateCurrent({ masterTemplate: unique ? { unique } : null }); return unique;