Copied to clipboard

Flag this post as spam?

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


  • Miran 1 post 71 karma points
    May 08, 2019 @ 13:40
    Miran
    0

    Using (through inheritance) custom variables in Partial Views

    Hi everyone reading. I come from design side of things (print, front-end, ux-ui, ... etc.) and I'm currently learning the basics of Umbraco. And already have a question, go figure that :D Bare with me. And thank You.

    I have a document type defined for a page, with an entry for a phone number. And I want to call that variable and transform it into a string and use that new variable in a PartialView in a master template inside HTML. I have a working solution pasted below, using PartialViewMacro.

    My current solution is this ...

    In PartialViewMacro (_pvm-data-contact-callnumber.cshtml) I have this code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var contactNumberCallString = (string)Model.Content.Value("mainContactNumber", fallback: Fallback.ToAncestors);
        contactNumberCallString = contactNumberCallString.Replace("+", "00").Replace("(", "").Replace(")", "").Replace(" ", "").Replace("3850", "385");
        }
    @contactNumberCallString
    

    And in my PartialView (_pv-info-contact-header.cshtml) I call it where needed:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    html bla bla
    ...
    <a href="tel:@Umbraco.RenderMacro("pvmDataContactCallnumber")">
        @Model.Value("mainContactNumber", fallback: Fallback.ToAncestors)
    </a>
    ... 
    end bla
    

    And in master template I call partialView and it works like I want it to :) In a content section I write the phone number in a way +385 (0)21 1234 567 and get it the way I want for calling "00385211234567".

    My question is this ... Is there a better way of doing this, a more "elegant solution" from programmers viewpoint? Or this is how to do it in Umbraco.

    I tried several ways, non of them working, until I've come up with this and it seems too simple :D But I'm not sure is this the best way to do it, with Macro. I tried inserting the code in master template ( @{ ... code for transformation ... } ) and calling variables in children templates but that doesn't work. I don't know how to make global variables, inherit custom variables from parents (not from docum. types but " var name = something ") and then using it in a child with @name syntax. I don't know enough about Razor, MVC, ... to know if something like this is possible or is a big NO NO :)

    My logic was, transform the string once, use it on multiple pages. With this working solution I have a feeling that it retransforms the number for every page the user opens. And that seems to me like a bad thing to do. But my knowledge about backworkings of Umbraco is limited and maybe I am "feeling" it wrong.

    I would like to learn the "correct" way from the start.

Please Sign in or register to post replies

Write your reply to:

Draft