Copied to clipboard

Flag this post as spam?

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


  • Kim Andersen 1447 posts 2197 karma points MVP
    Jan 12, 2012 @ 08:59
    Kim Andersen
    0

    Global variable in Umbraco v5/Razor

    Hi all

    I have a question regarding global variables in Razor/Umbraco v5.

    What I want to achieve is to have a variable with some content. That variable decides how the rest of the site shall be build.

    In XSLT what I did was to create a file called something like globalvaribles.xslt. I then included this file in the other XSLT-files where I wanted to have have access to the variables defined in the globalvariables.xslt-file.

    Now I could grab values from the global variables in all of the files where the globalvaribles.xslt was included. An example of a variable could be how wide the main layout should be, how many elements there was in the left side etc.

    Is it possible in some way to something similar in Razor/umbraco v5? I don't know if it's maybe possible to define the variable in some kind of master page and then inherit it to other files or something like that.

    Thanks in advance :)

    /Kim A

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jan 12, 2012 @ 09:08
    Dirk De Grave
    0

    Kim,

    I personally don't use "global variables" (actually I just hate that concept), but always use the content tree to store variables I need throughout the site. Can be changed quite easily. Even if I don't want content editors to see those (if that's a requirement), I use the excellent AttackMonkey's TabHider package to hide "content irrelevant" data. Another approach would be to store such data in config files and retrieve that info in code. May not be that flexible and requires coding, but works fine. Or, just write a class in c# and store that class in App_Data folder and use that class to store such data.

    Many options to choose from...

     

    Cheers,

    /Dirk

     

  • Kim Andersen 1447 posts 2197 karma points MVP
    Jan 12, 2012 @ 09:31
    Kim Andersen
    0

    Hi Dirk

    I agree with you about storing some standard settings in the content tree, on eg. a "Settings"-node. I do that as well :)

    But in this situation I can't, as the values are not static. Let me give an example of where I have used this earlier.

    Let's say we have a multi node tree picker to select content to the left and the right "column" of a site. The slected content can be inherited to the childpages and the childpages can cancel the inheritance from the parent pages. This means that on some of the pages there are content selected in the left side and in the right side. On some other pages there are only selected content in the right side and not in the left side etc.

    If there's not selected content in eg. the left side, then the main content should not be 500px wide, but maybe 700px instead. So the global variable in this example would tell us how wide the main content should be (actually it should tell us if there's content in the left side and in the right side of the page that we are currently on. Based on that we are giving the container different class'es). And therefor the variable are differet on each page, meaning that I can't just store it in the content tree or a config file.

    I hope the above explanation makes sense :)

    /Kim A

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jan 12, 2012 @ 10:04
    Jan Skovgaard
    1

    Hi Kim and Dirk

    I'd just like to add that basically Iit's a matter of how we can include razor files in other razor files so we don't need to write the same piece of code to give us the information about how wide the page should be etc.

    How can Razor code be reused in other files - is there a way to include razor files in other razor files?

    /Jan

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jan 12, 2012 @ 10:29
    Dirk De Grave
    2

    Check this post: http://joeriks.com/2011/03/11/better-structure-for-your-razor-scripts-with-renderpage-in-umbraco/ (Especially the part on helper files)

     

    Cheers,

    /Dirk

  • Kim Andersen 1447 posts 2197 karma points MVP
    Jan 12, 2012 @ 10:33
    Kim Andersen
    0

    Thanks Dirk, I'll check it out later today to see if this is can solve our "problem".

    /Kim A

Please Sign in or register to post replies

Write your reply to:

Draft