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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:28:02+00:00 2026-05-27T01:28:02+00:00

Is there a Javascript templating library that automatically infers the variables used in the

  • 0

Is there a Javascript templating library that automatically infers the variables used in the template and the possible dependencies among them? If I for example have a template that looks like this (Handlebars syntax):

<script id="entry-template" type="text/x-handlebars-template">
<div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
</div>
</script>

I would like to be able to do something like this:

var source   = $("#entry-template").html();
var template = Handlebars.compile(source);
template.vars() => {title: "", body: ""}

The reason I would like this functionality is that I would like to be able to generate a form with fields reflecting the variables needed to complete the 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-05-27T01:28:03+00:00Added an answer on May 27, 2026 at 1:28 am

    This snippet (edit at http://jsfiddle.net/CfaAW/2/) uses a Regex to find the simple {{var}} syntax, and will also look for . to handle Handlebars Paths.

    function extractObjectFromTemplate(source) {
        var handlebarsRegex = /\{\{([\s\S]+?)\}\}/g;
        var varObject = {};
        var varArray, ns, o, i, len;
        while ((varArray = handlebarsRegex.exec(source)) !== null) {
            ns = varArray[1].split('.');
            o = varObject;
            for (i = 0, len = ns.length; i < len; i++) {
                o = o[ns[i]] = o[ns[i]] || (i == len - 1 ? "" : {});
            }
        }
    
        return varObject;
    }
    

    On your template, a JSON.stringify(extractObjectFromTemplate(source)) gives you this:

    {"title":"","body":""}
    

    and with Handlebars Paths in your template

    <h1>{{title.h1}}</h1>
    <h2>{{title.h2}}</h2>
    <div class="body">
      {{body}}
    </div>
    

    you will get the nested properties

    {"title":{"h1":"","h2":""},"body":""}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a templating library/system/engine/technique written in javascript that: Doesn't screw up plain-text (non-html)
is there any javascript library that provides me with a custom event as soon
Is there any javascript algo that i can used to check for a website
Is there any javascript function that can encrypt data: For example i want to
I'm wondering is there a JavaScript library available that would allow me to generate
Is there a JavaScript library that models 3D polyhedra using the canvas tag or
Is there a JavaScript library which allow me to perform all the REST operation
In the wikipedia entry Unobtrusive JavaScript there is an example of obtrusive JavaScript: <input
Is there a templating language that has both server-side ruby (pref. rails) and JS
Situation: A PHP Templating system that is responsible for building a pages HTML. Javascript

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.