Accessing MacroLibraryScript functions from your TemplateScript

Context

This is something that is definitely part of Aria Templates tips & tricks. This feature has always been available since we delivered Macro Libraries, which means it’s available from quite a while. But surprisingly, it’s something you don’t think you can use in the first place.

Let’s assume you have a Template file that defines in its configuration JSON descriptor to have access a a given Macro Library that itself is associated to a Script file.

Then if the same Template file is also configured to have itself a Script file associated, you can freely access functions defined in the Macro Library Script from it.

Simply execute the methods accessing them from the alias you defined in your template, and that’s it !!

Some code

Here is a small example describing this nice feature:

Template

{Template {
  $classpath: "path.to.my.Template",
  $hasScript: true,
  $macrolibs: {
    "myLib": "path.to.my.Lib"
  }
}}

/* Your macros go there */

{/Template}

Template Script

Aria.tplScriptDefinition({
  $classpath: "path.to.my.TemplateScript",
  $prototype: {
   
    /* Helper method that is using a function defined in the MacroLib Script
     * This method should capitalize any text input
     * "some text input" -> "Some Text Input"
     */

    capitalize: function(text) {
      return this.myLib.capitalize(text);
    }
  }
});

Macro Library

{Library {
  $classpath: "path.to.my.Lib",
  $hasScript: true
}}

/* Your shared macros go there */

{/Library}

Macro Library Script

Aria.tplScriptDefinition({
  $classpath: "path.to.my.LibScript",
  $prototype: {
    capitalize: function(text) {
      return text.toLowerCase().replace(/(?:^|\s)\S/g, function ($1) {
        return $1.toUpperCase();
      }
    }
  }
});

Conclusion

How neat is that ? Pretty awesome no ? So, feel free to use this tips, and also tell us if you already knew it was usable !

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>