Templates: Retain layout from file when loading template (closes #20524) #20529
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Prerequisites
Fixes: #20524
Description
This was raised as a migration error, but it's not really - more a difference in how the 13 and 17 backoffice handles loading templates.
We have the concept of a "master template", i.e. a layout, which can be set via the UI when creating templates. If creating one at the root
Layout = null;will be set. If creating under another template, something likeLayout = "Layout.cshtml";will.The problem is that's not the only way to create a layout relationship with a template - you can just do it in the Razor code itself. So even if a template doesn't have a parent, it can still have something like
Layout = "Layout.cshtml";in the code.Currently when a template loads, whether or not a parent exists is used to amend the code to set the layout to
nullor the master template as appropriate. So in the situation where a layout is set in code in a template at the root,Layout = null;will be written into the markup. When this happens you can see the file on disk is still correct withLayout = "Layout.cshtml";, and the template editor is dirty as you'll see a "Discard changes" message pop up if you try to navigate away.This PR fixes this by not attempting to amend the Razor code on load - it'll now always keep whatever is on disk. Only when creating a template, or in setting a master template via the backoffice UI, will it continue to write the
Layoutsetting in the template file.Testing
Layout = null;to e.g.Layout = "MyLayout.cshtml";in the .cshtml file.Layout = null;presented in the backoffice UI, whilst the file on disk is unchanged.Layoutproperty.Release
Merging this will include it for 17.0, but we could likely cherry-pick back to
mainfor 16.4 too.