Search code examples
kentico

Is it possible to hardcode Kentico webparts into a layout?


Kentico 8.2.

I've got a layout "master" file - the creatively named Master.ascx.

I've got three web part zones in the layout: header, main and footer. Main just includes a content placeholder which is overridden by the next layout down - home, standard, whatever.

I could just drag and drop web parts onto the header and footer web part zones on to the "design" tab, but I don't want to for a few reasons:

  1. Which web parts go in which zone is stored in the database, which isn't versioned in Git. The layout file itself is versioned via the "store CMS Virtual Files on disk" option.
  2. The layout markup inside the header is complicated enough that I'll end up needing to have 4 or 5 web part zones for 4 or 5 web parts - or I'll need to write a layout web part or drag row layouts / column layouts in, etc - it seems much simpler to just hardcode web parts inside bespoke markup.
  3. I find editing a plain text file with Visual Studio much faster than dragging and dropping web parts into a zone, and the same applies for anyone else who'll ever work on this site.
  4. Which web parts go in which web part zone doesn't ever change - there's always a primary nav, share-on-facebook, etc - which web parts are included will never need to be in any way dynamic.

Is it possible to just hardcode a web part into my layout?


Solution

  • To answer your question, no you cannot hard code webparts in your layout. Simple reasoning is webparts are not part of a layout, they are part of a template . The webpart definition for each template is stored with the template in the database as XML. You can see this by going to a template and selecting the Webparts tab and it will display all the XML for each of the webparts on that page template.

    Understand where you're coming from with your 4 points. What you also need to consider is:

    • what happens when you want/need upgrade or apply a hotfix?
    • from the client, content editor and content administrators perspective, having everything in code, renders the CMS pretty useless.
    • if you code everything in a physical file, it's then pretty pointless to have a CMS if it requires a developer or a physical code change for every request

    The point of the CMS is to be dynamic and database driven. Not everything can reside in the file system, database or a code repository. Kentico has some very strong developer tools you can enable like workflow, versioning, and check-in/out.

    I'd also enable your DBA and ensure they have a valid backup and recovery plan in the event of some disaster.