Copied to clipboard

Flag this post as spam?

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


  • Tom 713 posts 952 karma points
    Apr 20, 2012 @ 08:50
    Tom
    0

    razor @functions

    Hi all try as I might I can't get the following scenario to work..

    What im trying to achieve is a loop through to the closes doctype which has a template and then a redirect..

     

    I'm getting a syntax error stating { expected:

    <%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true" %>

    <asp:content ContentPlaceHolderId="ContentPlaceHolderDefault" runat="server">
    <umbraco:Macro runat="server" language="cshtml">
    @inherits umbraco.MacroEngines.DynamicNodeContext;

    @{  
      if(Model.HasValue("umbracoRedirect"))
      {
        var nodeToRedirectTo = Library.NodeById(Model.umbracoRedirect);
        if(nodeToRedirectTo != null && nodeToRedirectTo.Visible) {
          Response.RedirectPermanent(nodeToRedirectTo.Url);
        }
      }
      
      var nodeForUrl = Model.Parent;
      while (nodeForUrl != null && !TemplateExists(nodeForUrl.Template))
      {
        nodeForUrl = nodeForUrl.Parent;
      }
      
      if(nodeForUrl.Visible) {
        Response.RedirectPermanent(nodeForUrl.Url);
      }
    }

    @functions {    
        public static bool TemplateExists(int templateId)
        {
          if (templateId <0) 
          {
            return false;
          }
          var template new umbraco.cms.businesslogic.Template(templateId);
          return template.Alias ="BasePage" || (template.MasterTemplate !&& TemplateExists(template.MasterTemplate));
        }
    }
    </umbraco:Macro>
    </asp:content>
  • Douglas Ludlow 210 posts 366 karma points
    Apr 20, 2012 @ 17:41
    Douglas Ludlow
    0

    This appears to save without errors:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{  
      if(Model.HasValue("umbracoRedirect"))
      {
        var nodeToRedirectTo = Library.NodeById(Model.umbracoRedirect);
        if(nodeToRedirectTo != null && nodeToRedirectTo.Visible)
        {
          Response.RedirectPermanent(nodeToRedirectTo.Url);
        }
      }
      
      var nodeForUrl = Model.Parent;
      while (nodeForUrl != null  && !TemplateExists(nodeForUrl.Template))
      {
        nodeForUrl = nodeForUrl.Parent;
      }
      
      if(nodeForUrl.Visible)
      {
        Response.RedirectPermanent(nodeForUrl.Url);
      }
    }

    @functions {    
      public static bool TemplateExists(int templateId)
      {
        if (templateId <= 0)
        {
          return false;
        }
        var template = new umbraco.cms.businesslogic.template.Template(templateId);
        return template.Alias == "BasePage" || (template.MasterTemplate != 0 && TemplateExists(template.MasterTemplate));
      }
    }

     I don't know where the error was exactly, but I did find that Template lives under the umbraco.cms.businesslogic.template namespace which is different from what you have.

  • Douglas Ludlow 210 posts 366 karma points
    Apr 20, 2012 @ 17:43
    Douglas Ludlow
    0

    Ah, okay, so the two changes I made to get the script to save without errors are:

    1. Remove the semicolon after:

      @inherits umbraco.MacroEngines.DynamicNodeContext
    2. Include template in the path namespace for:

      var template = newumbraco.cms.businesslogic.template.Template(templateId);
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies