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 8344397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:21:33+00:00 2026-06-09T06:21:33+00:00

I recently discovered Mustache, and after reading through all the documentation online, I am

  • 0

I recently discovered Mustache, and after reading through all the documentation online, I am still having trouble understanding how to use it. I am not sure how to separate my HTML from my template file, as I understand you include your tags within a variable in your javascript file.

I am creating a site where I would like to have a header/footer function. I turned to mustache to do this.

Here is my current template:

template = "
{{#head}}
<!DOCTYPE html>
<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <script src = "extras/icomoon/lte-ie7.js"></script><![endif]-->
<!--[if IE 7]>    <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<!--[if IE]>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/modernizr/2.5.3/modernizr.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.1.0/respond.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/selectivizr/1.0.2/selectivizr-min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/flexie/1.0.0/flexie.min.js"></script>
    <script src="js/vendor/move.js"</script>
<![endif]-->
<head>
    <meta charset="utf-8">

    <!-- Use the .htaccess and remove these lines to avoid edge case issues.
         More info: h5bp.com/i/378 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

     <title>{title}</title>
    <meta name="description" content="{meta-description}">

    <!-- Mobile viewport optimized: h5bp.com/viewport -->
    <meta name="viewport" content="width=device-width">

    <!-- Place favicon.ico and apple-touch-icon.png in the root directory: mathiasbynens.be/notes/touch-icons -->
    <link href='http://fonts.googleapis.com/css?family=Erica+One|Ubuntu+Condensed|Open+Sans|Maven+Pro|Sans+Pro:300,400' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="css/base.css">
    <!-- <link rel="stylesheet/less" href="less/style.less"> -->
    <link rel="stylesheet" href="css/animations.css">
    <link rel="stylesheet" href="css/typography.css">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/no-flexbox.css">

    <link rel="stylesheet" href="css/bootstrap.css" type="text/css">
   <!-- <link type="text/css" rel="stylesheet" href="css/1140.css"> -->
    <link type="text/css" rel="stylesheet" href="css/media-queries.css">
    <!-- todo: add font-awesome and icomoon-->
    <link rel="stylesheet" href="extras/icomoon/style.css">
    <script src="http://cdnjs.cloudflare.com/ajax/libs/modernizr/2.5.3/modernizr.min.js"></script>
    <script src="js/tests/tests.js"></script>
    <link rel="stylesheet" type="text/css" href="css/chosen.css">

    <!-- More ideas for your <head> here: h5bp.com/d/head-Tips -->

    <!-- All JavaScript at the bottom, except this Modernizr build.
         Modernizr enables HTML5 elements & feature detects for optimal performance.
         Create your own custom Modernizr build: www.modernizr.com/download/ -->

</head>
{{/head}}
<body>
    <!-- Prompt IE 6 users to install Chrome Frame. Remove this if you support IE 6.
         chromium.org/developers/how-tos/chrome-frame-getting-started -->
    <!--[if lt IE 7]><p class="chromeframe">Your browser is <em>ancient!</em> <a href="http://browsehappy.com/">Upgrade to a different browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to experience this site.</p><![endif]-->

    <!-- Add your site or application content here -->
    <header id="main" class="main-menu loading">
        {{#logo}}<a href = "/index.html"><img src="img/logo.png" class="flash-hover"></img></a>{{/logo}}
        {{#nav}}<nav id="nav-main" class="top-menu">
            <ul class = "top-nav">
                <li><a href="index.html">Home</a></li>
                <li><a href="docs/about.html">About</a></li>
                <li><a href="docs/features.html">Features</a></li>
                <li><a href="docs/installation.html">Installation</a></li>
                <li><a href="help-support/contact.html">Contact</a></li>
            </ul>
        </nav>{{/nav}}
    </header>
    <div id="ajaxloader1"></div>
    <div id="main-content" role = "main" class = "loading">
    {{#top_a}}
    <div id="top-a">
    {{content}}
    </div>{{/top_a}}
    {{#article}}
    <article>
    <hgroup>
        <h1 class="article-header">{{header}}</h1>
        <h2 class="subtitle">{{subtitle}}</h2>
        </hgroup>
        {{content}}
    </article>
    {{/article}}

    {{#footer}}
    <!-- JavaScript at the bottom for fast page loading: http://developer.yahoo.com/performance/rules.html#js_bottom -->

    <!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if offline -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.7.2.min.js"><\/script>')</script>

    <!-- scripts concatenated and minified via build script -->
    <!-- <script src="js/plugins.js"></script> -->
    <script src="js/loading.js"></script>

    <script src="http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/hashgrid/6/hashgrid.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/store.js/1.1.1/store.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/kerning.js/0.2/kerning.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/masonry/2.1.04/jquery.masonry.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.0.4/bootstrap.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.transit/0.1.3/jquery.transit.min.js"></script>
    <script src="extras/chosen/chosen.jquery.min.js"></script>
    <!-- <script src="js/vendor/can.jquery.min.js"></script> -->
    <script src="http://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.5.0-dev/mustache.min.js"></script>
    <!-- <script src="js/vendor/can.fixture.js"></script> -->
    <script src = "js/main.js"></script>
    <!-- <script src="http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.8.19/jquery-ui.min.js" media = "@screen"></script> -->
    <!-- end scripts -->

    <!-- Asynchronous Google Analytics snippet. Change UA-XXXXX-X to be your site's ID.
         mathiasbynens.be/notes/async-analytics-snippet -->
    <!-- <script>
        var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];
        (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
        g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
        s.parentNode.insertBefore(g,s)}(document,'script'));
    </script> -->
</body>
</html>
{{/footer}}

";

The problem I get is obviously that I need to escape all the quotation marks. So my question is, what’s the best way of separating all this markup from the template so that I can still get my syntax highlighting as if I was doing inline html in PHP, with standard header/footer functions?

Thanks in advance.

  • 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-09T06:21:35+00:00Added an answer on June 9, 2026 at 6:21 am

    There aren’t multi-line strings in JS, so you can’t just throw all of that in a JS string if that’s what that’s supposed to be. Ordinarily I’d suggest perhaps putting it in a <script> element like this:

    <script type="text/x-mustache-template">
    
    {{#head}}
    <!DOCTYPE html>
    
    ...
    
    </script>
    

    But since you have <script> tags in your template, that’s not so straightforward. Your best bet might be to put the template in it’s own file, and pull it into your page with PHP, something like:

    <?php
    
    $templates = array();
    
    $template[ 'whatever' ] = file_get_contents( "template.mustache" );
    
    ?>
    
    <script>
    
    var templates = <?php echo json_encode( $templates ); ?>;
    
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently discovered at work that it is the policy not to use compiler
I recently discovered longlines mode in Emacs (after having been a regular user for
I recently discovered the shorthand if statement and after searching online I couldn't find
I recently discovered that all Windows 7 machines have an handwriting recognition API within
I recently discovered that I can use lambdas to create simple event handlers. I
Having recently discovered design patterns, and having acquired the excellent Head First Design Patterns
I recently discovered the Typesafe Stack (and through it, SBT and g8) - loving
I recently discovered the PHP framework Kohana (which is awesome) and was reading a
I recently discovered, however, that all public functions are displayed in the Insert Function
I recently discovered that I could make use of user controls to reduce the

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.