src/main/java/tinyrtpl/Rtpl.java
private static String process(Data data, String tpl, int begin) {
        int fragBegin = tpl.indexOf("{{", begin);
        if (fragBegin < 0) return tpl.substring(begin);
        int fragEnd = tpl.indexOf("}}", fragBegin + 2);
        if (fragEnd < 0) return tpl.substring(begin);

        String curFrag = tpl.substring(fragBegin + 2, fragEnd); // get the current frag
        int curFType = Rtpl.typeOfFrag(curFrag); // identify the type of this frag

        // "{{if ...}}..."
        if (curFType == 1) {
            int blockEnd = Rtpl.indexOfEndingOverPairs(tpl, fragEnd + 2, "{{/if}}", "{{if", "{{/if}}");
            if (blockEnd < 0) {
                return tpl.substring(fragBegin);
            }
            return tpl.substring(begin, fragBegin) + Rtpl.processIf(data, Rtpl.getBranchesOfIf(tpl.substring(fragBegin, blockEnd))) + Rtpl.process(data, tpl, blockEnd + 7);
        }

        // "{{each ...}}..."
        if (curFType == 5) {
            int blockEnd = tpl.indexOf("{{/each}}", fragEnd + 2);
            if (blockEnd < 0) {
                return tpl.substring(begin);
            }
            String[] paras = Rtpl.getParasOfEach(curFrag);
            return tpl.substring(begin, fragBegin) + Rtpl.processEach(data, tpl.substring(fragEnd + 2, blockEnd), paras[0], paras[1], paras[2]) + Rtpl.process(data, tpl, blockEnd + 9);
        }

        // "{{set ...}}"
        if (curFType == 7) {
            String[] paras = Rtpl.getParasOfSet(curFrag);
            return tpl.substring(begin, fragBegin) + Rtpl.processSet(data, paras[0], paras[1]) + Rtpl.process(data, tpl, fragEnd + 2);
        }

        // "{{include ...}}"
        if (curFType == 8) {
            String[] paras = Rtpl.getParasOfInclude(curFrag);
            return tpl.substring(begin, fragBegin) + Rtpl.processInclude(data, paras[0], paras[1]) + Rtpl.process(data, tpl, fragEnd + 2);
        }

        // "{{...}}"
        if (curFType == 9) {
            return tpl.substring(begin, fragBegin) + Rtpl.processGet(data, curFrag.trim()) + Rtpl.process(data, tpl, fragEnd + 2);
        }

        return tpl.substring(begin);
    }
/**
     * Process the data and template string forward, from a certain position.
     * @param data: data to render
     * @param tpl: template string to render
     * @param begin: begin position
     * @return a html string
     */
Process the data and template string forward, from a certain position.
@param data: data to render
@param tpl: template string to render
@param begin: begin position
@return a html string
Process the data and template string forward from a certain position . @param data : data to render @param tpl : template string to render @param begin : begin position @return a html string
private static String process(Data data, String tpl, int begin) {
        int fragBegin = tpl.indexOf("{{", begin);
        if (fragBegin < 0) return tpl.substring(begin);
        int fragEnd = tpl.indexOf("}}", fragBegin + 2);
        if (fragEnd < 0) return tpl.substring(begin);
        String curFrag = tpl.substring(fragBegin + 2, fragEnd); 
        int curFType = Rtpl.typeOfFrag(curFrag); 
        if (curFType == 1) {
            int blockEnd = Rtpl.indexOfEndingOverPairs(tpl, fragEnd + 2, "{{/if}}", "{{if", "{{/if}}");
            if (blockEnd < 0) {
                return tpl.substring(fragBegin);
            }
            return tpl.substring(begin, fragBegin) + Rtpl.processIf(data, Rtpl.getBranchesOfIf(tpl.substring(fragBegin, blockEnd))) + Rtpl.process(data, tpl, blockEnd + 7);
        }
        if (curFType == 5) {
            int blockEnd = tpl.indexOf("{{/each}}", fragEnd + 2);
            if (blockEnd < 0) {
                return tpl.substring(begin);
            }
            String[] paras = Rtpl.getParasOfEach(curFrag);
            return tpl.substring(begin, fragBegin) + Rtpl.processEach(data, tpl.substring(fragEnd + 2, blockEnd), paras[0], paras[1], paras[2]) + Rtpl.process(data, tpl, blockEnd + 9);
        }
        if (curFType == 7) {
            String[] paras = Rtpl.getParasOfSet(curFrag);
            return tpl.substring(begin, fragBegin) + Rtpl.processSet(data, paras[0], paras[1]) + Rtpl.process(data, tpl, fragEnd + 2);
        }
        if (curFType == 8) {
            String[] paras = Rtpl.getParasOfInclude(curFrag);
            return tpl.substring(begin, fragBegin) + Rtpl.processInclude(data, paras[0], paras[1]) + Rtpl.process(data, tpl, fragEnd + 2);
        }
        if (curFType == 9) {
            return tpl.substring(begin, fragBegin) + Rtpl.processGet(data, curFrag.trim()) + Rtpl.process(data, tpl, fragEnd + 2);
        }
        return tpl.substring(begin);
    }
private static String process ( Data data , String tpl , int begin ) { int fragBegin = tpl . indexOf ( "{{" , begin ) ; if ( fragBegin < 0 ) return tpl . substring ( begin ) ; int fragEnd = tpl . indexOf ( "}}" , fragBegin + 2 ) ; if ( fragEnd < 0 ) return tpl . substring ( begin ) ; String curFrag = tpl . substring ( fragBegin + 2 , fragEnd ) ; int curFType = Rtpl . typeOfFrag ( curFrag ) ; if ( curFType == 1 ) { int blockEnd = Rtpl . indexOfEndingOverPairs ( tpl , fragEnd + 2 , "{{/if}}" , "{{if" , "{{/if}}" ) ; if ( blockEnd < 0 ) { return tpl . substring ( fragBegin ) ; } return tpl . substring ( begin , fragBegin ) + Rtpl . processIf ( data , Rtpl . getBranchesOfIf ( tpl . substring ( fragBegin , blockEnd ) ) ) + Rtpl . process ( data , tpl , blockEnd + 7 ) ; } if ( curFType == 5 ) { int blockEnd = tpl . indexOf ( "{{/each}}" , fragEnd + 2 ) ; if ( blockEnd < 0 ) { return tpl . substring ( begin ) ; } String [ ] paras = Rtpl . getParasOfEach ( curFrag ) ; return tpl . substring ( begin , fragBegin ) + Rtpl . processEach ( data , tpl . substring ( fragEnd + 2 , blockEnd ) , paras [ 0 ] , paras [ 1 ] , paras [ 2 ] ) + Rtpl . process ( data , tpl , blockEnd + 9 ) ; } if ( curFType == 7 ) { String [ ] paras = Rtpl . getParasOfSet ( curFrag ) ; return tpl . substring ( begin , fragBegin ) + Rtpl . processSet ( data , paras [ 0 ] , paras [ 1 ] ) + Rtpl . process ( data , tpl , fragEnd + 2 ) ; } if ( curFType == 8 ) { String [ ] paras = Rtpl . getParasOfInclude ( curFrag ) ; return tpl . substring ( begin , fragBegin ) + Rtpl . processInclude ( data , paras [ 0 ] , paras [ 1 ] ) + Rtpl . process ( data , tpl , fragEnd + 2 ) ; } if ( curFType == 9 ) { return tpl . substring ( begin , fragBegin ) + Rtpl . processGet ( data , curFrag . trim ( ) ) + Rtpl . process ( data , tpl , fragEnd + 2 ) ; } return tpl . substring ( begin ) ; }
Process the data and template string forward, from a certain position.
Process the data and template string forward from a certain position .
// get the current frag // identify the type of this frag // "{{if ...}}..." // "{{each ...}}..." // "{{set ...}}" // "{{include ...}}" // "{{...}}"