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 954 karma points
    Jun 30, 2011 @ 06:01
    Tom
    0

    Model.something - something is what datatype?

    Just wondering.. I had the error in the other post to do with writing out an property from a razor macro.. what is the best way to check for null etc? i.e. what datatype is Model.someproperty by default? i ended up doing:

      @if((Model.pageTitle != null && !string.IsNullOrEmpty(Model.pageTitle.ToString())) || (Model.siteName != null && !string.IsNullOrEmpty(Model.siteName.ToString())))
      {
        <title>@Model.siteName</title>
      }

    but surely there's a cleaner way!?

  • Gareth Evans 141 posts 332 karma points c-trib
    Jul 01, 2011 @ 01:20
    Gareth Evans
    0

    In 4.7.1

    @Model.HasValue("pageTitle") or a variety of other approaches :)

  • Tom 713 posts 954 karma points
    Jul 01, 2011 @ 02:54
    Tom
    0

    Hi Gareth.. so what would i be doing to appropriately check these vars? where useAnalytics is a true false field..

     

    @inherits umbraco.MacroEngines.DynamicNodeContext
            
    @{ var useAnalytics = @Model.AncestorsOrSelf().googleTrackingEnabled; }
     
    if(!string.IsNullOrEmpty(@useAnalytics.ToString()) && @useAnalytics) 
    {
      @{ var googleTrackingAccountID = @Model.AncestorsOrSelf().googleTrackingAccountID;  }
      @if(!string.IsNullOrEmpty(@googleTrackingAccountID.ToString()))
      {
        <script type="text/javascript">
        //<![CDATA[
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', '@googleTrackingAccountID']);
        _gaq.push(['_trackPageview']);
     
        (function () {
          var ga = document.createElement('script'); ga.type = 'text/javascript';
          ga.async = true;
          ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        })();
        //]]>
        </script>
      }
    }
  • Gareth Evans 141 posts 332 karma points c-trib
    Jul 01, 2011 @ 02:59
    Gareth Evans
    0

    What you're doing is probably the cleanest way in 4.7.0, other than type checking to see if the property type is DynamicNull

    4.7.1 improves on this a lot, if you're interested in testing a prerelease version, let me know

Please Sign in or register to post replies

Write your reply to:

Draft