Library Reference
JS Library
Template Engine
4min
the template engine allows easy access to rendering tools that are present in the klevu library core template functions setdata – sets the data variable of the template getdata – gets the data variable of the template gethelpers – get a full list of available template helpers sethelper – set new template helper or override existing one dependent on the name – sethelper function ( name , functionsource ) render – compile the js template based on name and return html in string format – render function ( name / , data , datalocal / ) list – get a full list of available template names settemplate – set a new template string as a template, decoded is used as a format for the templatestring if set to false it will decode the template sting from base64 encoding – settemplate function ( templatestring , name , decoded = false ) converttemplate – transform html string to dom tree – return is encapsulated in a div element inserttemplate – append as chield a dom node element to a parent don node – inserttemplate function ( parent , child ) template render compiles javascript templates into functions that can be evaluated for rendering useful for rendering complicated bits of html from json data sources template functions can both interpolate values, using <%= … %>, as well as execute arbitrary javascript code, with <% … %> if you wish to interpolate a value and have it be html escaped, use <% … %> global template variables inside the template, you have access to the following js variables data – contains the returned processed data and settings of the current request in the format settings – containing settings set as part of the global context example term, landingurl,termoriginal suggestions – containing processed response of the suggestion type request, refer to the api v2 docs for a complete format query – containing processed response of the query type request, refer to the api v2 docs for a complete format datalocal – contains the object send when using micro templates refer to the template helper render section helper – contains the list of functions defined as template functions core functions include translator – able to access the scope translation object in full translate – map to the scope traslator translate addtranslation – map to the scope traslator addtranslation gettranslation – map to the scope traslator gettranslation removetranslation – map to the scope traslator removetranslation settranslations – map to the scope traslator settranslations processcurrency – map to the scope currency processcurrency each – map to klevu each buildurl – accepts 3 arguments – example helper buildurl(data settings landingurl,”q” , “test”) uri – original url key – key that needs to be changed value – the value of the change striphtml – accepts 1 argument – strips any html tags from the input string render – accepts 4 arguments name – the name of the set template scope – the scope the template will be rendered in data – optional – will be taken from scope if not provided – data to be set in the data object of the rendered template datalocal – optional – will be defaulted to data – data to be set in the datalocal of the rendered template scope the scope of the template engine is dependent on what search object scope it is linked to each search object will contain as part of its scope a instance of the template engine all data and templates are executed as part of that scope if a user wants to use the same template in 2 or more search objects then the template has to be set for each of the template engines the template source can be any location where a full sting containing the template code can be extracted from , example a element contents in main dom , an ajax response , an encoded variable template engine accepts encoded templates in the base 64 encoding by parsing the third parameter to false(this is the default)