Archive for the ‘ Flash ’ Category

Avoiding the yellow focus rectangle

1
stage.stageFocusRect = false;

PureASTemplate an AS3 port of PureJSTemplate engine.

In the last post I used javascript to dinamically parse a template and post back the results to AS3. Now I have ported the PureJSTemplate to AS3 with the help of D.eval API.

This means no Javascript/Browser need. Now you can integrate it directly to your AS3/Flex projects.

Download PureASTemplate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package es.xperiments.utils
{
    import r1.deval.D;

    /**
     * @author xperiments
     */

    public class PureASTemplate
    {
        private static var tplMap : Array = new Array();
        private static var left : String = "<#";
        private static var right : String = "#>";
        //Delimiters can have regex special characters in them; the following two variables will hold escaped versions of them
        private static var escapedLeft : String = "<#";
        private static var escapedRight : String = "#>";
        private static var escapeRegex : RegExp = new RegExp( '(\' + [ ', '^', '?', '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\' ].join( '|\' ) + ')', 'g' );
        private static var replaceLeft : RegExp = new RegExp( "<#", "g" );
        private static var regexLeftRight : RegExp = new RegExp( "<#" + "|" + "#>", "g" );
        private static var replaceSingleQuote : RegExp = new RegExp( "'
", "g" );
        private static var replaceLineBreak : RegExp = new RegExp( "
\\r|\\n", "g" );

        public static function parseTemplate( id : String, tpl : String, data : Object ):String
        {
            var unreplaceLineBreak : RegExp = new RegExp( "
{lineBreak}", "g" );
            var tplID : String = id;
            var tplObj : Object = tplMap[tplID];
            if (!tplObj)
            {
                var leftjs : String = String.fromCharCode( 21 );
                var leftjsout : String = leftjs + "
=";
                tpl = tpl.replace( replaceLeft, left + leftjs );
                var tplSplit : Array = tpl.split( regexLeftRight );
                var js:String = "
function "+id+"( data ) { ";
                    js += "
var output=''; var ld='" + left + "'; var rd='" + right + "'; ";
                for (var i : uint = 0; i < tplSplit.length ; i++)
                {
                    var line : String = tplSplit[i];
                    if (PureASTemplate.stringStartsWith( line, leftjsout ))
                    {
                        js += "
output+=" + line.substring( leftjsout.length ) + "; ";
                    }
                    else if (PureASTemplate.stringStartsWith( line, leftjs ))
                    {
                        js += "
" + line.substring( leftjs.length ) + " ";
                    }
                    else
                    {
                        js += "
output+='" + line.replace( replaceSingleQuote, "\'" ).replace( replaceLineBreak, '{lineBreak}' ) + "'; ";
                    }
                }
                js += "
return output; }";
                tplObj = D.parseFunctions( js );
                tplMap[tplID] = tplObj;
            }
            return D.evalToString( id+'( data )', { data:data }, tplObj ).replace( unreplaceLineBreak, '\n' );
        }

        public static function setDelimiters(l : String, r : String) : void
        {
            if (l != r)
            {
                left = l;
                escapedLeft = left.replace( escapeRegex, '\\$1' );
                replaceLeft = new RegExp( escapedLeft, "
g" );

                right = r;
                escapedRight = right.replace( escapeRegex, '\\$1' );

                regexLeftRight = new RegExp( escapedLeft + "
|" + escapedRight, "g" );
            }
        }

        private static function stringStartsWith(str : String, startsWith : String):Boolean
        {
            return str.substring( 0, startsWith.length ) == startsWith;
        }
    }
}

AS3 Implementing a simple Templating System II

After some investigation on as3 templates engines, I write a simple class to manage key value pairs, but for a project I am now developing I have to control more things in the Template layout…

I have find a simple Javascript template System and “Ported” it to work width AS3.

It is not a “Direct Port” because I adapted it to work as a single javascript class that anyone can use from AS3.

Example Code: ( assumes classPaths are correct and have a Textfield in stage named output )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import es.xperiments.utils.PureJSTemplate;
var exampleTemplate:XML =
<code>
<!--[CDATA[
<$=data.welcome$>
<$for(var k=0; k<data.number; k++){
    data.k = k+200;
$>
    <$=data.k$>
<$}$>
]]-->
</code>;

PureJSTemplate.addEventListener( Event.COMPLETE, onJSTemplate );
PureJSTemplate.initialize( );

function onJSTemplate( e:Event ):void
{
    PureJSTemplate.setDelimiters( "<$","$>");
    output.htmlText = PureJSTemplate.parseTemplate( "TestTemplate", exampleTemplate,{welcome:"PureJSTemplate is great",number:30} );
}

Download: Purejstemplate