Troubles with connecting object passed from Express to Handlebars template

I'm currently facing an issue trying to chain an object rendered from Express in my Handlebars file. I can't seem to find a solution.

The object I'm working with is structured like this:

"generalContentOfferOne": {
  "subCopy": {
    "en-us": "Test Copy",
    "bahasa": "Bergabunglah dalam..."
  }
}

When I try to render

{{distributorJSON.generalContentOfferOne.subCopy}}
in my Handlebars file, it displays as Object object, which is expected.

In addition, I have a localization variable that determines whether the language should be en-us or bahasa based on the route. I thought I could use bracket notation to dynamically render the value from the object above using the localization value. For example:

{{ distributorJSON.generalContentOfferOne.subCopy[{{localization}}] }}

I also attempted:

{{ distributorJSON.generalContentOfferOne.subCopy.{{localization}} }}

Unfortunately, these methods are not successful. I suspect that Handlebars might have its own specific way of chaining dynamic values. Any guidance would be greatly appreciated. Thank you!

Answer №1

After searching high and low, I came up empty-handed with a ready-made solution. That's when inspiration struck, and I created a custom handlebars helper to tackle the issue.

Here's the handy helper function:

exports.getDynamicPropertyValue = function(object, key){
  return object[key];
}

And this is how you can use it in your Handlebars template:

{{ getDynamicPropertyValue productData.specifications.weight "metric" }}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Get the nearest offspring of the guardian

I have a main 'container' div with multiple subsections. Each subsection contains 1 or 2 sub-subsections. Let's say I have a variable that holds one of the subsections, specifically the 4th subsection. This is the structure:container > s ...

What is the best way to incorporate Javascript into jQuery tabs?

On my website, I have implemented a Jquery and CSS tab system similar to the one found here. Each tab contains a Facebook feed box, a Twitter widget, and a ranking widget for my blog. However, when these widgets are placed within the tab content area, they ...

Is it possible to create a button function in HTML that can alter the CSS image tag?

Is there a way I could create a function that can retrieve a value, update an image source, and then incrementally select another value? It would be incredibly helpful if you could provide a reference where I could learn how to do this or explain it in det ...

Having trouble with the Ajax load function not functioning in your HTML code?

The issue has been resolved. Thank you to everyone who helped! Initially, I was attempting to run a file offline instead of on a web server (XAMPP server). Once I uploaded the file to the web server, it started working properly. I had been trying to load ...

Querying MongoDB to locate books by the same author or those that are categorized in at least one similar category

Looking to discover books by the same author or with at least one matching category. This is how my Book Schema looks: const bookSchema = new Schema( { title: { type: String, required: true }, author:{ ...

Use the Facebook API to easily access any user's profile picture by providing their unique Facebook user ID

For my JavaScript function, I am taking the input "user_id" and expecting it to provide me with the profile photo data. Everything works fine when a logged in user's profile picture is being displayed. However, when I pass a logged out user_id, an err ...

Logic for iterating through blocks in JavaScript

I have a code that consists of an array containing multiple objects, in this case URLs. I am currently using a loop to iterate through this array and send a request for each URL. However, I am looking for a way to optimize this process by breaking it dow ...

While working with Ngrx/effects, an error with code TS2345 occurred. The error message stated that the argument is of type 'Product[]', which cannot be assigned to a parameter of type

When I compile my code, I encounter the following issue (despite not finding any errors in the browser console and the application functioning properly). An error occurs in src/app/services/product.service.ts(15,9): The type 'Observable<Product> ...

Using percentage values to fill a customized SVG image

I'm looking to incorporate a percentage value into a custom SVG image in Angular 6 using TypeScript or CSS. Are there any tools designed for this specific task? The custom image could be anything, such as a dollar sign, gear icon, or thumbs up symbo ...

How can you determine the distance of an SVG path?

I've been experimenting with Scrollmagic and I'm interested in implementing a similar effect to the one showcased here: To test it out, I created an svg with a squiggle path and now I need to insert the length of the path into stroke-dasharray: ...

How to Display Graph Horizontally using jqBarGraph

I have encountered a minor but significant issue while using jqBarGraph. Due to the limited space available for displaying the graph (550px width with unlimited height), and up to 50 items can be shown at once, the bars appear quite small (around 5px thi ...

What could be causing the malfunction of my Nextjs Route Interception Modal?

I'm currently exploring a different approach to integrating route interception into my Nextjs test application, loosely following this tutorial. Utilizing the Nextjs app router, I have successfully set up parallel routing and now aiming to incorporate ...

Sequelize - Leveraging Associations in Where Clauses

Within sequelize, my setup includes boards and users with a many-to-many association structured like this: User.hasMany(Board, {through: BoardUsers}); Board.hasMany(User, {through:BoardUsers}); I'm trying to figure out if there's a way to use a ...

Ways to schedule the execution of a script at a specific time using Node.js

Every time the readDirectory function is called, it checks if any file created date is more than 30 days ago and removes that file from the directory. While this functionality is working correctly, I am concerned about its efficiency. To address this issue ...

Most effective method for eliminating an item from an array with AngularJs

I have an array of objects that I want to modify by removing multiple objects from it. The following code is currently achieving this successfully, but I'm curious to know if there is a more efficient way to accomplish this task. This process involv ...

Unable to resolve 500 error on Vercel in Next.js despite successful local development

Here is the content of route.ts import fs from 'fs'; import path from 'path'; import PizZip from 'pizzip'; import Docxtemplater from 'docxtemplater'; import { NextRequest, NextResponse } from 'next/server'; ...

Next.js presents a challenge with micro frontend routing

In the process of developing a micro frontend framework, I have three Next.js projects - app1, app2, and base. The role of app1 and app2 is as remote applications while base serves as the host application. Configuration for app1 in next.config.js: const ...

Learn the simple steps for creating a sub collection within a document using Firebase Function Express

Looking at the code snippet below, you'll see how I tried to create a sub collection of likes under a document with PostID. However, I encountered a 404 error indicating that it was not found. Based on my understanding from the documentation, a collec ...

What steps can I take to prevent drawing on the HTML5 Canvas?

I am working on creating a Paint-like application using HTML 5 Canvas. In this project, I need to implement two types of rectangles: filled and unfilled. Here is the code snippet for achieving this functionality: function drawRectangle(isFilled = false) { ...

Background with borders full of parallax effects scrolling in sync

I'm completely new to the world of HTML and CSS, and I was hoping for some guidance. I've been trying to wrap my head around a tutorial on creating parallax scrolling websites, which you can find here: However, I noticed that the tutorial includ ...