Copied to clipboard

Flag this post as spam?

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


  • Niels Kristiansen 166 posts 382 karma points
    Sep 30, 2013 @ 10:51
    Niels Kristiansen
    0

    angularjs + XSLT in Umbraco

    Hi,

    I'm trying to get the AngularJS to work together with XSLT, but having issues with such a simple thing as showing a hello world instead of {{data binding}}.

    Is it even possible to get AngularJS to work with XSLT?

    Here is the super simple example:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet 
        version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:ucomponents.cms="urn:ucomponents.cms" xmlns:ucomponents.dates="urn:ucomponents.dates" xmlns:ucomponents.email="urn:ucomponents.email" xmlns:ucomponents.io="urn:ucomponents.io" xmlns:ucomponents.media="urn:ucomponents.media" xmlns:ucomponents.members="urn:ucomponents.members" xmlns:ucomponents.nodes="urn:ucomponents.nodes" xmlns:ucomponents.random="urn:ucomponents.random" xmlns:ucomponents.request="urn:ucomponents.request" xmlns:ucomponents.search="urn:ucomponents.search" xmlns:ucomponents.strings="urn:ucomponents.strings" xmlns:ucomponents.urls="urn:ucomponents.urls" xmlns:ucomponents.xml="urn:ucomponents.xml" xmlns:umbraco.contour="urn:umbraco.contour" xmlns:uTwit="urn:uTwit" 
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ucomponents.cms ucomponents.dates ucomponents.email ucomponents.io ucomponents.media ucomponents.members ucomponents.nodes ucomponents.random ucomponents.request ucomponents.search ucomponents.strings ucomponents.urls ucomponents.xml umbraco.contour uTwit ">
    
    
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    
    <xsl:param name="currentPage"/>
    
    <xsl:template match="/">
    
    
    <script type='text/javascript' src="//code.angularjs.org/1.1.5/angular.min.js"></script>
    
    <script type='text/javascript'>//<![CDATA[ 
    
    function Controller($scope) {
        $scope.outputs = {};
        $scope.inputs = [{
            type: "text",
            name: "first_field",
        }, {
            type: "text",
            name: "second_field",
        }, {
            type: "checkbox",
            name: "third_field",
        }];
    
    }
    //]]>  
    
    </script>
    
      <h3 >Dynamic form fields AngularJS</h3>
    <div ng-app="" ng-controller="Controller" class="container">
         <h4>Inputs</h4>
        <ul ng-repeat="input in inputs">
            <li><span>{{input.name}} : </span>
                <div ng-switch="input.type">
                    <div  ng-switch-when="text" > 
                        <input  type="text" 
                                ng-model="outputs[input.name]">
                        </input>
                    </div>
                    <div ng-switch-when="checkbox">
                        <input  type="checkbox" 
                                ng-model="outputs[input.name]"                                 
                                ng-checked="outputs[input.name]"                                 
                                value="outputs[input.name]" >                               
                        </input>
                    </div>
                </div>
            </li>
        </ul>    
         <h4>Outputs</h4>
        <ul ng-repeat="input in inputs">
            <li>
                {{input.name}} : {{outputs[input.name]}}
            </li>
        </ul>
    </div>
    
    </xsl:template>
    
    </xsl:stylesheet>
    

    Hope someone can help me getting up running basic on the AngularJS + XSLT :)

    Thanks a million,

    Niels

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Sep 30, 2013 @ 11:03
    Ismail Mayat
    1

    Niels,

    In xslt the curly brackets have meaning so lets say you want to set an attribute on an element you can do it as

    <MyElement>
        <xsl:attribute name="whatever" value="something"/>
     </MyElement>
    

    or

    <MyElement whatver="{$something}"></MyElement>
    

    So I guess in your xslt you would need to encode the curly brakcet.

    Regards

    Ismail

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Sep 30, 2013 @ 11:05
    Chriztian Steinmeier
    0

    Hi Niels,

    There shouldn't really be a lot of hurdles to overcome - what one needs to remember is that there's a couple of things you need to think of when generating the output:

    • Switch to HTML output mode to make sure you're not getting empty <script> or <div> tags in the output

    You do that at the top of the file:

    <xsl:output method="html" ... />
    
    • Because curlies inside attributes (and only in attributes) are already used for something similar in XSLT, you need to escape them by doubling them to generate them in the output

    so to generate this:

    <div class="{{$scope.className}}"></div>
    

    Do this in XSLT:

    <div class="{{{{$scope.className}}}}"></div>
    

    Hope that gets you going - otherwise shout! :-)

    /Chriztian

  • Mads Jørgensen 74 posts 226 karma points
    Sep 30, 2013 @ 11:05
    Mads Jørgensen
    1

    Hey,

    or simply {{{{data.binding}}}} wich escapes the curly braces :-)

    {{ = { in XSLT!

     

    But Ismail's solution by using the <xsl:attribute /> stuff would work as well.

  • Niels Kristiansen 166 posts 382 karma points
    Sep 30, 2013 @ 21:01
    Niels Kristiansen
    0

    Hi everybody,

    Tried to get this to work, but no succes :S

    The {{{{data.binding}}}} didn't do the trick.

    I have now made it even less fancy, with the following example:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet 
        version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:ucomponents.cms="urn:ucomponents.cms" xmlns:ucomponents.dates="urn:ucomponents.dates" xmlns:ucomponents.email="urn:ucomponents.email" xmlns:ucomponents.io="urn:ucomponents.io" xmlns:ucomponents.media="urn:ucomponents.media" xmlns:ucomponents.members="urn:ucomponents.members" xmlns:ucomponents.nodes="urn:ucomponents.nodes" xmlns:ucomponents.random="urn:ucomponents.random" xmlns:ucomponents.request="urn:ucomponents.request" xmlns:ucomponents.search="urn:ucomponents.search" xmlns:ucomponents.strings="urn:ucomponents.strings" xmlns:ucomponents.urls="urn:ucomponents.urls" xmlns:ucomponents.xml="urn:ucomponents.xml" xmlns:umbraco.contour="urn:umbraco.contour" xmlns:uTwit="urn:uTwit" 
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ucomponents.cms ucomponents.dates ucomponents.email ucomponents.io ucomponents.media ucomponents.members ucomponents.nodes ucomponents.random ucomponents.request ucomponents.search ucomponents.strings ucomponents.urls ucomponents.xml umbraco.contour uTwit ">
    
    <xsl:output method="html" omit-xml-declaration="yes"/>
    
    <xsl:param name="currentPage"/>
    
    <xsl:template match="/">
    
    <script type='text/javascript' src="//code.angularjs.org/1.1.5/angular.min.js"></script>
    <script type='text/javascript'>//<![CDATA[ 
    
    function MyCtrl($scope) {
      $scope.hi = 'Man, I wish this would work';
    }
    
    //]]>  
    
    </script>
    
      <div ng-app="">
      <div ng-controller="MyCtrl">
          {{{{hi}}}}
      </div>
    </div>
    
    </xsl:template>
    
    </xsl:stylesheet>
    

     

    Could I ask for help on getting the above to work, if so - I think I'm ready for the beautiful world of AngularJS - I hope :)

    Niels

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Sep 30, 2013 @ 21:48
    Chriztian Steinmeier
    1

    Hi Niels,

    It's only when used inside of an attribute that you need to escape the curlies - I didn't make that obvious enough, I see.

    (So this one only needs the regular set of double-curlies to work - the previous probably failed because of the critical angular.js <script> tag being closed as <script /> which is known to fail in atleast a couple of browsers...)

    EDIT: Oh, and you obviously need to run this from a webserver, because of the use of the protocol-less URL (//) for the AngularJS file...

    /Chriztian

  • Niels Kristiansen 166 posts 382 karma points
    Sep 30, 2013 @ 22:06
    Niels Kristiansen
    100

    Hi Chriztian,

    I found the issue has nothing to do with AngularJS and XSLT at all, but the order of AngularJS and jQuery. I was calling jQuery at first and thereafter AngularJS. When I changed the order of these everything worked just fine.

    Thanks to all the help you, Mads and Ismail provided.

    Looking so forward to work with AngularJS. Per has really sold the idea of the magic in this Framework :)

    Kind regards,

    Niels

Please Sign in or register to post replies

Write your reply to:

Draft