Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9208447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:37:13+00:00 2026-06-18T00:37:13+00:00

Is there a way to set the templateSettings for lodash when using RequireJS ?

  • 0

Is there a way to set the templateSettings for lodash when using RequireJS?

Right now in my main startup I have,

  require(['lodash', 'question/view'], function(_, QuestionView) {
    var questionView;
    _.templateSettings = {
      interpolate: /\{\{(.+?)\}\}/g,
      evaluate: /\{\%(.+?)\%\}/g
    };
    questionView = new QuestionView();
    return questionView.render();
  });

but it doesn’t seem to want to set the templateSettings globally because when I use _.template(...) in a module it wants to use the default templateSettings. The problem is that I don’t want to change this setting in every module that uses _.template(...).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:37:14+00:00Added an answer on June 18, 2026 at 12:37 am

    Based on @Tyson Phalp suggestion, that means this SO question.

    I adapted it to your question and I tested it using RequireJS 2.1.2 and SHIM configuration.
    This is the main.js file, that is where the requireJS config is:

    require.config({
    /*  The shim config allows us to configure dependencies for
        scripts that do not call define() to register a module */
    
        shim: {
          underscoreBase: {
            exports: '_'
          },
          underscore: {
            deps: ['underscoreBase'],
            exports: '_'
          }
    
        },
        paths: {
          underscoreBase: '../lib/underscore-min',
          underscore: '../lib/underscoreTplSettings',
        }
    });
    
    require(['app'],function(app){
      app.start();
    });
    

    Then you should create the underscoreTplSettings.js file with your templateSettings like so:

    define(['underscoreBase'], function(_) {
        _.templateSettings = {
            evaluate:    /\{\{(.+?)\}\}/g,
            interpolate: /\{\{=(.+?)\}\}/g,
            escape: /\{\{-(.+?)\}\}/g
        };
        return _;
    });
    

    So your module underscore will contain the underscore library and your template settings.
    From your application modules just require the underscore module, in this way:

    define(['underscore','otherModule1', 'otherModule2'], 
       function( _, module1, module2,) { 
          //Your code in here
       }
    );
    

    The only doubt I have is that I’m exporting the same symbol _ two times, even tough this work I’m not sure if this is considered a good practice.

    =========================

    ALTERNATIVE SOLUTION:
    This also works fine and I guess it’s a little bit more clean avoiding to create and requiring an extra module as the solution above. I’ve changed the ‘export’ in the Shim configuration using an initialization function. For further understanding see the Shim config reference.

    //shim config in main.js file
    shim: {     
      underscore: {
          exports: '_',
          init: function () {
            this._.templateSettings = {
              evaluate:/\{\{(.+?)\}\}/g,
              interpolate:/\{\{=(.+?)\}\}/g,
              escape:/\{\{-(.+?)\}\}/g
            };
            return _; //this is what will be actually exported! 
          }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to set the length of a data series using Google
Is there a way to set something as global in a class and have
Is there a way to set Java's XPath to have a default namespace prefix
Is there a way to set the UITableViewCell.image to display on the right hand
Is there a way to set a custom word spacing in CoreText? I have
Is there any way to set the view file name as an attribute on
Is there a way to set a custom DPI/PPI when creating an image using
Is there a way to set the checked radio button using a JavaScript variable?
Is there any way to set the view page modal during network connecting? I
Is there any way to set vertical alignment in css? I have a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.