Copied to clipboard

Flag this post as spam?

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


  • Dante 4 posts 24 karma points
    Nov 18, 2014 @ 01:47
    Dante
    0

    generating page title with razor script relative to document type

     

    I am trying to create a script whereby depending on the document type of the page a certain pre-defined title tag format will appear, if there is nothing already written in an overwriting custom title input. I have inserted the macro within the title tag on my master template but keep on getting an Error loading Razor Script message.

    Any help would be very much apprecaited.

    Html

    <title> <umbraco:Macro Alias="NewPageTitle" runat="server"></umbraco:Macro </title>

    Script 

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines

    @{
     
      if(String.IsNullOrEmpty(@Model.tabName.ToString()== false )
        {
          @Model.tabName
        }
      
      else if(@Model.DescendantsOrSelf("Country"))
        {   
         <text
           Holidays to @Model.Name
         </text>
        }
        
      else 
        {
          @Model.Name;
        }

    }

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 18, 2014 @ 08:47
    Dennis Aaen
    0

    Hi Dante,

    Since the Razor was introduced in Umbraco there have been some iterations on this, so depending on the version of Umbraco that you are using can discover some adjustments if you found some code on the forum. 

    The first thing I would do is try to remove the @ in the if and if else statement. So your script would look like this. In Umbraco version 4.x. you can have an @ before Model in if statements, but in v6 it is not allowed because they use Razor 2.0.

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines

    @{
     
      if(String.IsNullOrEmpty(Model.tabName.ToString()) == false )
        {
          @Model.tabName
        }
     
      else if(Model.DescendantsOrSelf("Country"))
        {  
         <text>
           Holidays to @Model.Name
         </text>
        }
       
      else
        {
          @Model.Name;
        }
    }

    Another approach that I have use multiple times is also break your code down to some blocks like first check if the first if works, and then build the else on, and so the if else.

    Hope this helps,

    /Dennis

  • Dante 4 posts 24 karma points
    Nov 18, 2014 @ 22:18
    Dante
    0

    Thanks Dennis for you quick reply but this solution is not working for me.

    I am using Umbraco v4 but strongly considering upgrading to the latest version in the very nead future. I was not aware of the difference in Razor rules, would this mean i have to edit all my scripts?

    Also could you please expand on what you meant by breaking the code down into blocks?

    Many thanks, D

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 18, 2014 @ 23:07
    Dennis Aaen
    0

    Hi Dante,

    No if your razor files works just fine now it´s it just fine. But you considering upgrading to the latest version or a version 6.x.x you can maybe experience that some of your razor scripts will maybe fail, because since Umbraco uses razor for the first time there have been some interations over how to write the syntax.

    What I mean by breaking the code down into blocks, was if you have a big block of code, it is maybe easier to see how long the code works before it gives you the error, if you say ok does this small code works.

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines

    @{
     
     
    if(String.IsNullOrEmpty(Model.tabName.ToString())==false)
       
    {
         
    @Model.tabName
       
    }
    }

    If this works then you can build on the else statement, and so on, Hope this make sense. Another thing you could try to get some more information on why your code is goning wrong is by.

    Go to your webconfig file, and find umbracoDebugMode and set it to true like this.

    <add key="umbracoDebugMode" value="true"/>

    When it is true, then you can add this querystring ?umbDebugShowTrace=true  to the page where the macro script files is thought the error, and then you should get the strack trace for the page.

    e.g. http://www.domain.com/page/?umbDebugShowTrace=true

    But remember when you site is going live, then switch it back to false again. 

    The DynamicNode razor macros is the old first version of Razor in Umbraco, and this is deprecated, instead I think that you should have a look at partal views or partial views macros where you can use dynamic or strongly typed razor. Specifically if you consider to upgrade to a newer version.

    It´s not so hard to rewrite the old DynamicNode razor code to the dynamic razor or strongly typed razor.

    In the dynamic version instead of @Model for current page you are using @CurrentPage, and in strongly typed it is @Model.Content. Here is the documentation on how to convert old DynmicNode razor. http://our.umbraco.org/documentation/reference/templating/macros/Partial-View-Macros/ To see how to converting a Partial View Macro from a legacy Razor Macro Script take a look at the bottom of the page.

    Hope this helps, and make sense.

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft