Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • mauzilla 19 posts 90 karma points
    Mar 02, 2020 @ 10:19
    mauzilla
    0

    Using properties in MasterPage accross other pages

    Full disclosure: Brand new to Umbraco. I've been handed a project written in Umbraco 7 and busy "learning as I go" without any handover. I am a seasoned PHP developer with some c# experience, so it's a new world in a new world :)

    The project has a number of variables / properties used within the Views (such as username, email address etc). These are populated with a SAML library in production but for development I don't have access to the SAML server so we need to hard code these properties to make the application work.

    It appears that the current developers have "replicated" these properties in the MasterPage (main layout) and then the various other View pages as part of the ContentModels - This does not seem right as I believe I should be able to have a very basic conditional set in the MasterPage that checks if we have access to the SAML server (or possibly a variable in the web.config that checks the URL for the environment) and then have these properties set once in say the MasterPage (default "layout") and have them available for all sub pages.

    This however is not the case at present. I have to uncomment properties on each page making it a bit of a nightmare for management.

    What I have picked up is that in the MasterPage there seems to be a compile error which may be the cause of the problem:

    "The type or namespace 'MasterPage' does not exist in the namespace 'Umbraco.Web.PublishedContentModels' (are you missing an assebly reference?)"

    I have a feeling that this may be the reason I am unable to set view variables in the MasterPage and not have them available in the sub pages. If not, what is the correct way of handling such conditions prior to view rendering?

    Here is an example of the MasterPage method:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.MasterPage>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @{
    
    Layout = null;
    
    // The below should be commented out in production / staging where SAML is involved:
    var userId = '1234';
    var DisplayName = "MyName";
    var IsAdmin = true;
    var email "[email protected]";
     }
    

    The same is then replicated on sub pages (as example, Home):

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
    Layout = "MasterPage.cshtml";
    
    var userId = '1234';
    var DisplayName = "MyName";
    var IsAdmin = true;
    var email "[email protected]";
    }
    

    This obviously cant be right. Any suggestions would be greatly appreciated :)

  • Patrick van Kemenade 101 posts 339 karma points
    Mar 02, 2020 @ 11:25
    Patrick van Kemenade
    0

    Ok, first thing change the top line of the master page to:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    

    Second thing, I think you want to use the Model instead of local razor variables to access data and share things. Or if you want to access the user use the Umbraco functions for it. But for any other data do the following.

    Since the MasterPage will be used in a number of different DocumentTypes it has a generic inherist. In other word a MasterPage isn't a full page of it self, it's always part of a specific document type using the master page.

    Ok, but how about accesing data items which are used (in every) document type. For instance let say every page has an SEO Title and SEO MetaDescription, except for the Disclaimer page which is also using this masterpage.

    The way to set this up is to create a componsite DocumentType without templae and set this under permissions that this should be used as a child element.

    In your for instance Blog DataType include this composite (child) datatype.

    Since this child datatype uses an interface you can in the MasterPage make a distinction and know whether or not it's available, like this:

    if (Model is ISeoFields)
    {
        seoTitle = ((ISeoFields)Model).SeoTitle;
        seoMetaDescription = ((ISeoFields)Model).SeoMetaDescription;
    }
    

    Hope this helps.

Please Sign in or register to post replies

Write your reply to:

Draft