Call Handle Bar Helper Function From Another Helper Function





Hi all, This is my new post after long time :P
                               I am Learning Handlebar now :)

I am not going to explain you the basics of handlebar and make my post as bored one .Instead let me explain the feature which most of us don't know.
If you are new to handlebar here is the link you can refer it and learn basics

                                http://handlebarsjs.com/


From the above link you will get to know there is  a feature called helper function in handlebars .
Here i am going to explain how to call a helper function from another helper function.
some times  you may get into a situation where you want to perform some operation as the result of another in that situation you can use this logic.

 Template     

This is my template ,

{{#checkAndFormat "India"}}{{/checkAndFormat}}             

Helper 1:

                     
    Handlebars.registerHelper('checkAndFormat', function (value, options) {
    var source = '{{checkMyName x y}}';
    var context = {
        x: value,
        y: options
    };
    var test = Handlebars.compile(source)(context);

    var html=value;
    html=html+" is "+test;
    return new Handlebars.SafeString(html);
 });

checkAndFormat is my first helper function which i am going to call with the param in html content.
look at the code which is highlighted in blue that is the actual syntax .
checkMyName is the another helper function which i am going to call from first one .
I am passing the param from first helper to second one.This can be specified as 

  var source = '{{checkMyName x y}}';

x,y is the params ,that can be defined like this and assigned to context

        var context = {   x: value,     y: option  }; 

This is the code which calls our helper , it is like compiling our  handlebar template here i am adding context(params) also.
var test = Handlebars.compile(source)(context); 

 compiling  template is done on document ready so as soon as the template is compiled first helper     function will get called and "india" will be passed to helper 2

$(document).ready(function () {
    var source = $('#tmp').html();
    var template = Handlebars.compile(source);
    var html = template();
    $('#main').html(html);
});

  Helper 2:

Here i am checking that the param  value is equal to "India" if yes i am returning a string as"my country" else "not my country" will be returned 
Based on this result first helper will return html that will be getting displayed.


Handlebars.registerHelper('checkMyName', function (value, options) {
    if (value == "India") {
        return new Handlebars.SafeString("my country");
    } else {
        return new Handlebars.SafeString("not my country");
    }

});

This is my entire code ,



<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.0.beta.6.js" type="text/javascript" charset="utf-8"></script>

<body>
    <div id="main">
    </div>
    <script id="tmp" type="text/x-handlebars-template">
        {{#checkAndFormat "India"}}{{/checkAndFormat}}
    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            var source = $('#tmp').html();
            var template = Handlebars.compile(source);
            var html = template();
            $('#main').html(html);
        });
        Handlebars.registerHelper('checkAndFormat', function (value, options) {
            var source = '{{checkMyName x y}}';
            var context = {
                x: value,
                y: options
            };
            var test = Handlebars.compile(source)(context);
            var html = value;
            html = html + " is " + test;
            return new Handlebars.SafeString(html);
        });
        Handlebars.registerHelper('checkMyName', function (value, options) {
            if (value == "India") {
                return new Handlebars.SafeString("my country");
            } else {
                return new Handlebars.SafeString("not my country");
            }

        });
    </script>
</body>
</html>

Hope you also learnt some thing new today :)





Comments

Popular posts from this blog

Get rid of boring for loop and try using "range" and "rangeClosed"

HOW TO USE NOTE PAD AS YOUR PERSONAL DAIRY!!!!!

Create MultiSelect Dropdown using vue-multiselect