Randomblue
Randomblue

Reputation: 116373

Text files in the path configuration file

I'm using the text plugin of RequireJS. Is it possible to reference a text file in the path configuration file? I've tried

require.config({
   paths: {
       'myTemplate': 'text!templates/myTemplate.html'
   }
});

but that didn't work.

Upvotes: 5

Views: 2195

Answers (2)

Jim Wooley
Jim Wooley

Reputation: 10408

RamenRecon's answer helped, but in my case I think it was slightly confusing by using myTemplate for the path and template name. The key I found is to only substitute the Path, but not the actual file name. As a result, to abstract the path to /subSystem/templates/myTemplate.htm using require and the path configuration, set the configuration as follows:

require.config({
   paths: {
      templatePath: 'subsystem/templates'
   }
});

And then in your module definition:

define(['text!templatePath/myTemplate.htm'],
   function(template) {}
);

Upvotes: 4

Matt S
Matt S

Reputation: 1882

The reason it isn't working is because RequireJS plugins are designed to be used as part of a require command, not in the config.

Try:

require.config({
   paths: {
       'myTemplate': 'templates/myTemplate.html'
   }
});

and in your module:

define(
    ['text!myTemplate'],

    function () {}
)

Upvotes: 7

Related Questions