Copied to clipboard

Flag this post as spam?

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


  • Ted Jardine 98 posts 120 karma points
    Jan 19, 2011 @ 02:21
    Ted Jardine
    0

    IronRuby, Umbraco 4.6.1, and Library methods

    Using IronRuby for making a menu macro, but while it works fine in < 4.6, using with a new site running 4.6.1, I can't determine how to reference the Umbraco library any more (although the DynamicNode additions are nice!).

    Used to be able to use HTML Encode method in the Umbraco library by:

    umbraco = Object.const_get("umbraco")
    library = umbraco.const_get("library")

    ...

        puts %Q{
            <li><a href="#{library.nice_url(child.id)}"  class="#{'first' if i == 0} #{'selected'  if child.id ==  currentPage.id}" title="Go  to  #{child.name}">#{library.html_encode(child.name)}</a></li>
          }

     

    ...

    Now need to modify it for Nice URL via child.nice_url, but I can't determine the new library.html_encode(child.name) variation that will work with 4.6. Anyone know the answer?

    Thanks,

    Ted

     

     

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jan 19, 2011 @ 12:12
    Aaron Powell
    1

    I did a blog post on how to do a menu in IronRuby - http://www.aaron-powell.com/umbraco-menu-with-ironruby

    Hopefully it helps :)

  • Ted Jardine 98 posts 120 karma points
    Jan 20, 2011 @ 04:38
    Ted Jardine
    0

    Slace,

    I should have mentioned that your blog post is what got me started in the beginning (have used variations of it on several projects since with no issues - love getting away from xslt) ;-) Unfortunately, at least in my one-and-only 4.6.1 installation, it doesn't work (specifically any references to Umbraco.Library - I'm assuming because of the new DynamicNode stuff).

    Is it just me or have you used it with a 4.6.1 install? Specifically referencing Umbraco.Library? Again, no problems in versions prior to 4.6.x

    Thanks,

    Ted

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jan 20, 2011 @ 04:41
    Aaron Powell
    0

    What's the error message you're receiving?

    I *think* that there's a url property of the dynamic node that is used so you don't need library for that

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 20, 2011 @ 22:18
    Jonas Eriksson
    0

    Hm, the non-ruby-namestandard way works for me:

    umbraco = Object.const_get("umbraco")
    library = umbraco.const_get("library")

    puts library.HtmlEncode("åäö")
    puts library.NiceUrl(1070)

  • Ted Jardine 98 posts 120 karma points
    Jan 21, 2011 @ 19:49
    Ted Jardine
    1

    Jonas,

    This works:

    puts library.HtmlEncode("åäö")

    This however fails when adding the library.HtmlEncode(child.name) in the following:

    def print_link(child, i)
      puts %Q{
          <li><a href="#{child.nice_url}" class="#{'first' if i == 0} #{'selected'  if child.id ==  currentPage.id}" title="Go  to  #{child.name}">#{library.HtmlEncode(child.name)}</a>
        }
    end

    Remove library.HtmlEncode(child.name)...replaced with just child.name and works fine (but of course, does not HtmlEncode). Therefore, it appears it's only an issue when within $Q to help with " string delimiters.

    Making sure it's nothing else, the following also fails:

    def print_link(child, i)
      puts %Q{
          <li>#{library.HtmlEncode(child.name)}
        }
    end

    Specifically, "Error loading MacroEngine script (file: RubyMenu.rb)" (and yes, I know there's only an opening <li> above - it's a nested menu).

    Caveat: this is my only 4.6.1 installation, and it does not allow me to save even an empty Ruby script from the GUI without Skip testing (ignore errors) checked (see http://umbraco.codeplex.com/workitem/29913).

     

  • Ted Jardine 98 posts 120 karma points
    Jan 21, 2011 @ 20:08
    Ted Jardine
    1

     

    And in case it was a problem with string delimiting and needing the curly brace matching pairs, I tried the following:

    def print_link(child, i)
      encoded_name = child.name
      #puts %Q{
          #<li><a href="#{child.nice_url}" class="#{'first' if i == 0} #{'selected'  if child.id ==  currentPage.id}" title="Go  to  #{encoded_name}">#{encoded_name}</a>
        #}
    end

    which works. But the following does not:

    def print_link(child, i)
      encoded_name = library.HtmlEncode(child.name)
      #puts %Q{
          #<li><a href="#{child.nice_url}" class="#{'first' if i == 0} #{'selected'  if child.id ==  currentPage.id}" title="Go  to  #{encoded_name}">#{encoded_name}</a>
        #}
    end

    I'm it must be a simple issue that I'm just not seeing.

    ...further, this works!

    parent.children.find_all { |c| c.umbraco_navi_hide && c.umbraco_navi_hide != "1" && c.showInNavigation && c.showInNavigation == "Side"}.each_with_index do |child, i|   
            #print_link(child, i + 1)
            encoded_name = library.HtmlEncode(child.name)
            puts %Q{
                <li><a href="#{child.nice_url}" class="#{'first' if i == 0} #{'selected'  if child.id ==  currentPage.id}" title="Go  to  #{encoded_name}">#{encoded_name}</a>
            }
            #print_list(child)
            puts '</li>'
    end

    For some reason, putting the print_link logic in the calling method works. Putting library.HtmlEncode(...) anywhere in print_link and refactoring logic over to it always fails. Is this an IronRuby thing? Or an Umbraco issue? Or mine ;-)

    EDIT: editor butchering code so reposted without formatting.

     

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 21, 2011 @ 20:28
    Jonas Eriksson
    0

    Hi,

    wow, nice syntax! I didnt think this %Q thing was built in ironruby by default. Seriously cool. (Ruby noob as I am).

    To make library work inside functions it's necessary to declare it as global with a $:

    $umbraco = Object.const_get("umbraco")
    $library = $umbraco.const_get("library")
           
    def print_x(x)
      puts $library.NiceUrl(x.id)
    puts $library.HtmlEncode(x.name)
    end
           
    print_x(currentPage)
  • Ted Jardine 98 posts 120 karma points
    Jan 21, 2011 @ 20:36
    Ted Jardine
    0

    Jonas - awesome! Knew it had to be simple...and if you're a Ruby noob, then what does that make me ;-)

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 21, 2011 @ 20:43
    Jonas Eriksson
    0

    A noob I am, but some times good at guessing and googling ;) - and now much more eager to learn Ruby - Total eyeopener for me this, I thought one had to import at least erb.rb or whatever to create such strings with dictionary lookups and even built in function calls. I'm glad I joined this discussion :-) So thanks Ted, and I hope to see more ruby discussed in the umbraco forums.

    Cheers!

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 21, 2011 @ 20:55
    Jonas Eriksson
    0

    One more thing, to make code snippets be formatted nicely I usually start with an empty paragraph and format that to Code, then I paste the code snippet into the formatted empty paragraph.

  • Ted Jardine 98 posts 120 karma points
    Jan 21, 2011 @ 21:36
    Ted Jardine
    0

    Jonas,

    I started running with the IronRuby implementation in Umbraco before Razor came onto the scene. It apears that Razor is now getting a LOT of development love (especially because it's so central in v5) so it appears that would be the route to take going forward (number of blog posts on IronRuby integration versus Razor is a good indicator ;-)

    For IronRuby though, a good place to start (and unfortunately, one of the few blog posts on it...which I would have probably added to if I was planning sticking with IronRuby in Umbraco) is slace's post mentioned above.

    Re formatting: good suggestion, although when you go to edit, previous working pastes got mangled upon the second save.

    Thanks again for the answers above!

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 22, 2011 @ 08:32
    Jonas Eriksson
    0

    You are welcome :)

    Yeah, not much on Ruby on the forums, I know. There's two things that is really nice with Ruby in Umbraco over Python, one is this %Q which allows for a nice mix with tags and code - might be a way in Python also, but I never found it without importing some lib. And also Ruby has a slightly better implementation in Umbraco than Python - script path can be setup in a config file, so imports are made very nice. Speaking of imports - that is not (yet) possible with Razor (afaik) in Umbraco. Until it is I'd say Ruby is the best overall macro language for Umbraco. For now my only concerns is the class casing rules which requires the

    Umbraco = Object.const_get("umbraco")

    , and the somewhat strange (in my eyes) syntax in some cases.

    Cheers

Please Sign in or register to post replies

Write your reply to:

Draft