Copied to clipboard

Flag this post as spam?

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


  • Mus'ab 158 posts 387 karma points notactivated
    Mar 04, 2020 @ 11:34
    Mus'ab
    0

    Global Variables in Razor View Engine

    is there a way for me to use a functionality similar to what are called Global Variables in the Spark View Engine, but for Razor.

    The point of it all lis to be able to define a variable in one section for the title and then being able to set or change the value of that variable later on in another section.

    In Spark you would create the variable in a section kind of like this (incomplete code for example purposes):

        <html>
      <head>
        <global type='string' Title='"Site Name"'/>
        <title>${Title}</title>
      </head>
      <body>
        <div><use content="view"/></div>
      </body>
    </html>
    

    And then you could set it in a different view or section or whatever:

    <set Title='product.Name + " - " + Title'/>
    

    How would I go about doing something like this in Razor or just solving a similar problem if I have the wrong approach ?

  • Richard Eyres 98 posts 580 karma points
    Mar 04, 2020 @ 14:12
    Richard Eyres
    0

    Hello

    I generally set 'global' items within a specific tab on the homepage node (such as title, telephone etc).

    If they need to be overridden further down the tree, then you add the same element to that document type as well. Then you can call the property and make it recursive.

    I personally, do not see the need to have global variables set in the templates.

  • Mus'ab 158 posts 387 karma points notactivated
    Mar 05, 2020 @ 11:14
    Mus'ab
    0

    Hello Richard

    when i want to overwrite title i am not bring the value from document type can you explain more please ?

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Mar 05, 2020 @ 12:17
    Dan Diplo
    0

    Generally, as Richard states, you would set these on a node in the CMS and then reference them. That way the values can be edited in Umbraco.

    However, you could create a static C# class that just contained static or constant properties and then reference these anywhere in your code (including views) if you really wanted.

    namespace MyNameSpace
    {
        public static class MyConstants
        {
            public const string Title = "This is a title";
    
            public const int SecretId = 1234;
    
            // etc
        }
    }
    

    Then in your view you'd just go:

    The title is @MyConstants.Title 
    

    You can also create Partial classes for more complex reusable data.

  • Mus'ab 158 posts 387 karma points notactivated
    Mar 05, 2020 @ 12:38
    Mus'ab
    0

    Hi Dan

    how can i change the title value when its a const ? i want to set the title value in a specific macro and i want the new value to be printed every ware i used before rendering dom

Please Sign in or register to post replies

Write your reply to:

Draft