3

I'm developing two custom display templates (Control and Item template) and I'm wondering if it is possible to share script members between these two templates, e.g. if I add something like var bar = 'bar'; in Control_MyTemplate.html can I reference it in Item_MyTemplate.html with var fooBar = 'foo' + bar;? Or do I need to extend the ResultScriptWebPart and add the property on the dataprovider? So far I tried adding the variable in a <script> container and in one of the scriptblocks with no luck.

1 Answer 1

6

Sure you can.

It's JS code after all and you could either use a global variable to store your information or the provided ctx object.

Here' a example for both possibilities:

ControlTemplate:

    <!--#_
    window.ThisIsGlobal =  "this works !";
    ctx.MyVar = "THIS WORKS !";
    _#-->

ItemTemplate:

    <!--#_
    alert(window.ThisIsGlobal);
    alert(ctx.MyVar);
     _#-->
1
  • ah...I forgot to attach the member to the ctx object. Now it works like a charm, thanks!
    – Stig Perez
    Commented Aug 21, 2014 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.