Copied to clipboard

Flag this post as spam?

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


  • Pickels 75 posts 108 karma points
    Jul 28, 2010 @ 18:32
    Pickels
    0

    XSLT vs Python

    Hello,

    I am new to Umbraco and I just started working with macros. I noticed you can write macros in XSLT or IronPython(or Ruby). Since I already know Python I thought I would just use that instead of XSLT.

    But now I notice that most examples I see are using XSLT. Is there something wrong with using Python? I read a few old posts that say the implementation isn't that great. Is that still the case or are most Umbraco users just used to XSLT?

    XSLT doens't really look hard but it sure looks ugly. If you compare the two: http://twitpic.com/29grzy (Python) vs http://twitpic.com/29grrb (XSLT). Python looks a lot more concise than XSLT. This got me suspicious that there might be something horribly wrong with Python in Umbraco.

  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Jul 28, 2010 @ 18:49
    Warren Buckley
    1

    Hi Pickels,
    For me personally I have being using Umbraco for quite a while now and Python was not around when I first started so it was .NET and XSLT, in addition to coming from a front end designer/developer background XSLT and XML was similar to XHTML in sense of closing and starting tags and was easier to understand coming from a non technical/developer background.

    In addition the content cache is stored in an XML file so it just made sense to work with XSLT.

    However there is nothing wrong in using Python, it is not as popular within the community but as far as I know there is nothing wrong with the Python implemetnation in Umbraco.

    I think what ever route you decide to use is down to personal preferance and the task at hand.

    Warren

  • Pickels 75 posts 108 karma points
    Jul 28, 2010 @ 18:59
    Pickels
    0

    That's good to hear. What's even better to hear is that we got all these options when working with Umbraco.

  • Geoff Lawson 5 posts 25 karma points
    Aug 02, 2010 @ 08:21
    Geoff Lawson
    0

    First off - I'm new and VERY impressed with Umbraco.  

    I love that Python is included, I'm pushing forward with using Python for all macros.  One problem I'm having is trying to figure out the API doc.  I want to look up the Node ID for based on a given name; id = getNodeId('somename').  Is there a method that does this?  Where is a good starting place for code examples?  What I'm finding in the docs is pretty light, I'm looking for more detail.  Any info would be welcome.

    Did I say how impressed I am with Umbraco?

    Thanks!

    Geoff

  • Pickels 75 posts 108 karma points
    Aug 02, 2010 @ 11:57
    Pickels
    0

    @Geoff Lawson: Why are you trying todo this? I was trying to do the exact same thing when I noticed there was a better way.

  • Geoff Lawson 5 posts 25 karma points
    Aug 02, 2010 @ 18:09
    Geoff Lawson
    0

    Hi Pickels - Your post is not complete, can you re-post.  I'm very interested in your response.

  • Pickels 75 posts 108 karma points
    Aug 02, 2010 @ 18:14
    Pickels
    0

    Woops that last I shouldnt have been there. Well tell me what you are trying to do and I might be able to help.

  • Geoff Lawson 5 posts 25 karma points
    Aug 02, 2010 @ 20:26
    Geoff Lawson
    0

    I'm new here so I'm sure there are better ways to doing this.  Right now I'm trying to get the node id based on the node name, i.e.

    nodeID = SomeGetMethod('theNodeName')

    what I'm using is

    nodeID = int(1111)

    but i'm assuming the node id might change, I just want to reference it by name. 

    In futher forum seaching, it seems like you can only directly reference the node by the id or from within $currentPage.  What I'm trying to do is build a 'right menu' that is consistant from all pages/templetes.

    Geoff

     

  • Pickels 75 posts 108 karma points
    Aug 02, 2010 @ 21:14
    Pickels
    1

    What you can do is give a macro parameter of the type contentSub. That way if you add your macro you can tell it which parent to use. To set the parameters go to macro > click on the macro > tab parameters.

    If you name your macro parameters parentNode you can acces it in python with parentNode. The macro parameters are strings so you need to convert them to int if you want to use them with the Node object.

    Node( int( parentNode ) )

    If you really want to find a node by it's name you can traverse the whole tree. 

    I made this little snippet yesterday: http://www.copypastecode.com/33441/

    You can remove the traverse.level if you don't need to know that.

    Here is an example that finds all the children of a specific type under a specific node: http://www.copypastecode.com/33485/

    I only have been working with Python for a month and Umbraco for 5 days so there might be even better ways to do this.

    I hope it helps if you have more Python questions feels free to ask them I can share what I discovered since Python examples are a little rare here.

  • Geoff Lawson 5 posts 25 karma points
    Aug 13, 2010 @ 22:08
    Geoff Lawson
    0

    Hi Pickels -

    I had to step away from this project for a couple of days... but I'm back.

    <!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} span.EmailStyle15 {mso-style-type:personal; mso-style-noshow:yes; mso-ansi-font-size:10.0pt; mso-bidi-font-size:10.0pt; font-family:Arial; mso-ascii-font-family:Arial; mso-hansi-font-family:Arial; mso-bidi-font-family:Arial; color:windowtext;} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->

    Thanks for the code snip.  I'm a bit perplexed with a very elementary piece of the puzzle – how do you retrieve the macro parameter in the python code?

     

    Sorry for the newbie question.

  • Pickels 75 posts 108 karma points
    Aug 13, 2010 @ 22:51
    Pickels
    0

    It's just the name of the parameter. So if you have nodeType as parameter then it's nodeType. If you want to know what's available just type 'print globals()'. An other handy function is dir( ). For example you can do print dir( currentPage ) will show you all the methods and variable on currentPage.

  • Geoff Lawson 5 posts 25 karma points
    Aug 13, 2010 @ 23:02
    Geoff Lawson
    0

    print globals() - doh!  didn't think about that.  Thanks again!

  • Jonas Eriksson 930 posts 1825 karma points
    Aug 25, 2010 @ 14:08
    Jonas Eriksson
    0

    About the nodes. Traversing them is fast as lightning since they are (as well as xslt ofcourse) kept in the app memory. A search for all nodes (tiny change from Pickles snippet) matching a property value does it's work in a blink even with a lot of nodes:

    def allChildrenFromNodeByPropertyValue(parent, propertyName, propertyValue):
    foundKids = []
    def traverse(list):
    for child in list:
    if child.GetProperty(propertyName) and str(child.GetProperty(propertyName).Value) == str(propertyValue):
    foundKids.append(child)
    traverse(child.Children)

    traverse(parent.Children)
    return foundKids

    # testing it:
    for c in allChildrenFromNodeByPropertyValue(Node(-1),"CompanyNo", companyNo):
    print c.Name + "\n\r"

    /just me playing with Python

  • Pickels 75 posts 108 karma points
    Aug 25, 2010 @ 15:51
    Pickels
    1
    """
         Module for traversing Umbraco node trees.
    """
    
    import clr
    clr.AddReference('cms')
    clr.AddReference('businesslogic')
    
    from umbraco.presentation.nodeFactory import Node
    from umbraco.cms.businesslogic.media import Media
    
    def findNodes(nodes, criteria):
        """
            Find nodes in a node collection by passing a filter function.
        """
    
    
        def traverse(ns):
            """
                Traverse through a collection.
            """
    
            for child in ns:
                if criteria(child): lostAndFound.append(child)
                traverse(child.Children)
    
        lostAndFound = []
        traverse(nodes)
    
        return lostAndFound

    Not sure if I posted this yet. It's a general function for traversing nodes and you can pass it a function/lambda. So for example to find all the nodes of a certain docttype you can do.

    findNodes(Node(-1).Children, lambda x: x.NodeTypeAlias == 'product')

    My function also works on media but it's not really recommended. If I understood correctly the businesslogic.media does a lot of database calls.

  • Jonas Eriksson 930 posts 1825 karma points
    Aug 25, 2010 @ 16:36
    Jonas Eriksson
    0

    Pure code beauty!

Please Sign in or register to post replies

Write your reply to:

Draft