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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:39:25+00:00 2026-05-25T11:39:25+00:00

I am using PHP-backend, Backbone.js and Handlebars.js. My javascript requests for data, and JSON

  • 0

I am using PHP-backend, Backbone.js and Handlebars.js. My javascript requests for data, and JSON data is returned successfully (json_encode).

When I give this JSON data to the handlebars template, it is not displaying. I realised the square brackets in front and at the back of my JSON object are ‘disliked’ by Handlebars.js and not being displayed. Take a look at the code below.

var ArticleListView = Backbone.View.extend(
{
  el: $('#main'),
  render: function()
  {
    var template = Handlebars.compile($("#articles_hb").html());
    $(this.el).html(template([{"articles":[{"title" : "1"}, {"title" : "2"}]}]));
    return this;    
  }
});

Now, if I take the brackets out, it works fine. What’s going on? Why are the square brackets there in the first place? How do I get rid of them?

  • 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-25T11:39:25+00:00Added an answer on May 25, 2026 at 11:39 am

    Handlebars wants an object for the context as it uses the context as a simple lookup table for template values. So you need to pass an object ({ ... }) to template(), not an array ([ ... ]).

    Someone is give you a one element array that contains the context object you need. Either fix the PHP that is producing the JSON to send a JSONified object (associative array in PHP terms) without the array wrapper or strip off the array in the client code with something like this:

    $(this.el).html(template(context[0]));
    

    If you have this literal code:

    $(this.el).html(template([{"articles":[{"title" : "1"}, {"title" : "2"}]}]));
    

    in your JavaScript file then you have to what is generating that code and fix it. If you do have literal data like that embedded in your Backbone view then you’re probably not using Backbone correctly, the data for your template should probably be coming from a Backbone model.

    If you’re getting that JSON from a Backbone model then I’d guess that you’re calling toJSON on a collection (which returns an array) rather than a single model where toJSON should give you a JavaScript object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm posting data using jquery/ajax and PHP at the backend. Problem being, when I
I have a webapp written in PHP using a MySQL database backend. This question
Is this correct? I create cookies using frontend javascript and sessions using backend language
I'm currently developing a PHP application that's using an Access database as a backend.
Using PHP, what's the fastest way to convert a string like this: 123 to
Using PHP, I can convert MySQL data or static table data to csv, Excel,
Here's my situation: using PHP and MySQL as my backend, jQuery and jsTree for
i am currently using php as backend language in webdevelopment. but im wondering what
I was thinking of implementing real time chat using a PHP backend, but I
I've got a php backend which delivers a time (e.g. '07:00:00'). This time is

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.