Is there a way to use a Handlebars helper to add spaces to a camelCased key name when formatting it?

{{#each data}}
    <div>
        {{@key}} : {{this}}
    </div>
{{/each}}

Here's a glimpse at the structure of the data:

data: {
    maxHealth: Number,
    maxMana: Number,
    chanceToCrit: Number,
},

I currently have a setup similar to this, showing the key name and its value as intended. However, the keys are displayed in camel case format like: maxHealth, maxMana, etc.

I am interested in having the keys displayed as: max health, max mana, chance to crit, for instance. The challenge is that I cannot predict the key names and values ahead of time since they will vary each time. How can I apply a handlebar helper to format the keys accordingly?

Appreciate your assistance.

Answer №1

Learn how to implement a custom helper in Handlebars.

The recommended approach to (-gives a nod-) deal with this situation is by creating and registering a helper function, such as the one below:

Handlebars.registerHelper('uncamelcase', function(str) {
    return doTheUncamelCasing(str);
});

In this example, doTheUncamelCasing represents a function that you define to convert a string from camelCaseStyle to camel case style. Afterwards, make use of your new helper within the template:

{{uncamelcase @key}}

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

Locate identical values in Vuejs

When working with an array of objects, I need to check if a newly inserted object is a duplicate or not. this.duplicate = false; for(let item of this.items){ if(item.id && this.item.name === item.name) { t ...

Struggling to accurately convert the string into a date object

I have an array of objects structured like this: const days = [ { _id: 12312323, date : '30/12/2021', dateStatus : 'presence' }, ... ] I am looking to convert the date property from a string to a Date object using the follo ...

Why does my computed property become undefined during unit testing of a head() method in Vue.js with Nuxt.js?

In my Vue.js + Nuxt.js component, I have implemented a head() method: <script> export default { name: 'my-page', head() { return { title: `${this.currentPage}` }; }, ... } </script> ...

Quasar Troubles with Touch Swipe Gestures

I'm facing two issues, the first being that the directives are not functioning as expected. For example, I've implemented a swipe only to the right: <div class="q-pa-md row justify-center"> <q-card v-touch-swipe. ...

Select a particular item and transfer its unique identifier to a different web page. Are there more efficient methods to accomplish this task without relying on href links and using the $_GET

Could there be a more efficient method for transferring the ID of a specific motorcycle from index.php to inventory.php when it is clicked on, aside from using href and $_GET? Perhaps utilizing hidden fields or submitting a form through JavaScript? What ...

Tips for postponing 'not found' error until the data has finished loading in Vue.js

I am accessing the following route: http://site.dev/person/1 The structure of my component is as follows: // PeopleComponent.vue <template> <div> <template v-if="person == null"> <b>Error, person does not exist.</b& ...

Dealing with JSON data retrieved from a Django QuerySet using AJAX

I am utilizing a Django QuerySet as a JSON response within a Django view. def loadSelectedTemplate(request): if request.is_ajax and request.method == "GET": templateID = request.GET.get("templateID", None) ...

The function window.location.reload(true) is failing to properly refresh the page

Currently, I have a PHP page that retrieves data from a MYSQL database and creates HTML content based on the returned rows. I recently implemented a feature where clicking a button adds a new row to the database using AJAX, jQuery, and PHP. However, after ...

What is the best way to automatically populate additional autocomplete options after selecting certain data, and include new data if it is not already present?

Is it possible to automatically populate the other Autocomplete fields based on matching first and last names? I am encountering scenarios where multiple individuals share similar first or last names but have different addresses. In addition, I would like ...

Issue: Cannot access the 'map' property of an undefined value in a React Mongo DB error

My React code was running perfectly fine until I encountered an error message in the browser console: "TypeError: Cannot read property 'map' of undefined". Let me share the snippet of my code with you. const MyComponent = () => { const [dat ...

The div element fails to display even after invoking a JavaScript function to make it visible

As a newcomer to JavaScript & jQuery, please bear with me as I ask a very basic question: I have created the following div that I want to display when a specific JavaScript function is called: <div id='faix' class="bg4 w460 h430 tac poa ...

Using the Rails cocoon gem to populate numerous input fields

I'm currently implementing the cocoon gem in my rails application, where I have a form with two nested fields (categories and subcategories). Initially, only the first field is visible while the second one remains hidden. When the first select field h ...

Force a video element to play by using React Context Provider

I've been incorporating the React Context in a similar manner here, and I'm facing a challenge on how to update my video element (to play the video). Shouldn't the components automatically update when they receive the prop? Below is a simpl ...

Differences between the scope of if statements in JavaScript and Python

I have a function in React where I am trying to achieve the following: renderPost(data) { const dateStr = new Date(data.pub_date).toLocaleString(); let img='empty'; if (data.header_image_url.url !== null) { ...

Create a regular expression that matches a combination of alphabets and spaces, but does not allow for

Can anyone provide a regular expression that allows for a combination of alphabets and spaces, but not only spaces? After searching extensively and reading numerous articles, I came across the expression /^[a-zA-Z\s]*$/, which unfortunately permits j ...

Javascript animations failing to run sequentially

I am working on a circular design made up of 12 arc segments and would like to create a smooth transition from the start pattern to the end pattern for the user to view. There will be multiple start and end patterns to showcase. Check out my current code ...

Error TS2322: The type 'Count' cannot be assigned to type 'ReactNode'

Hey everyone, I'm new to React and TypeScript and could really use some help. I'm currently trying to figure out how to use useState with TypeScript. import React,{useState} from 'react' interface Count{ count: number } const App = ( ...

In order to make Angular function properly, it is crucial that I include app.get("*", [...]); in my server.js file

Recently, I delved into server side JavaScript and embarked on my inaugural project using it. The project entails a command and control server for my own cloud server, operating with angular, Expressjs, and bootstrap. Presently, I am encountering challeng ...

Is it possible to automatically close navigation dropdowns when the screen size changes?

I am using a bootstrap 4 navbar with dropdowns that open with CSS animation/fade-in on desktop, and a separate toggle button for mobile. Everything works fine, but when I open the dropdowns on mobile and then resize the window screen, they remain open whic ...

Using PHP to Dynamically Add Content to HTML with AJAX

Utilizing AJAX for dynamic page functionality poses a challenge when attempting to replace specific content on the HTML page with data retrieved from a MySQL database. Echoing out the information directly will cause an error as the PHP code is executed thr ...