Employing specific delimiters with hogan-express while managing the {{{ yield }}} statement

I've hit a roadblock trying to solve this issue. My goal is to incorporate hogan.js (via hogan-express) into a new expressjs application while also utilizing hogan.js on the front-end with Backbone, lodash, and other tools.

The layout I am using consists of:

    ...
    {{> header }}
    {{{ yield }}}
    {{> footer }}
    ...

At the bottom of the layout, there is:

    <script type="text/template">
        {{> template }}
    </script>

For now, the content within those delimited entries is managed by hogan-express. However, the template inserted in the {{> template }} call contains its own delimited entries as shown below:

    <section>{{name}}</section>

I want these specific delimiters to be ignored by hogan-express so that hogan on the front-end can handle them instead.

I don't mind adjusting the delimiters for hogan-express. I attempted this change which worked, but it resulted in breaking the {{{ yield }}} call. I experimented with using <% %> as delimiters and modifying the yield to something like <%{ yield }%> (along with different variations), but none of my attempts were successful.

Does anyone have any suggestions on how I can implement custom delimiters in hogan-express without disrupting 'yields'?

Thank you!

Answer №1

I encountered a similar issue to yours and struggled to find a straightforward solution. Eventually, I decided to merge various suggestions to make it work effectively. In my case, I opted for a combination of hogan-express and hogan-express-partials.

  1. Implement the advice provided above regarding app.locals

    app.set('views', config.root + '/server/views');
    app.enable('view cache');
    app.set('view engine', 'html');
    app.locals.delimiters = '<% %>';
    app.set('layout', 'layout');
    app.set('partials', { nav: 'nav', footer: 'footer' });
    app.engine('html', hoganExpress);
    app.use(partials.middleware());
    
  2. Personally adjust the yield interpolation within your main template. Note that I had to temporarily change the delimiters to use the default ones.

    <%> nav %>
    
    <div class="container">
    
        <!-- Switch to original delimiters -->
        <%={{ }}=%>
        {{{ yield }}}
    
        <!-- Revert back to non-conflicting delimiters with Angular -->
        {{=<% %>=}}
    
    </div>
    
    <%> footer %>
    

The setup seems functional, but I'm not completely satisfied with it. I explored solutions closely resembling yours, yet one peculiar aspect I noticed in the hogan.js library was the potential usage of an ampersand (&) with the tripleStash, although I struggled to make it function correctly.

Answer №2

In order to address this issue within the realm of Express.js 4.x, I implement the following solution:

app.locals.delimiters = '<% %>';

To execute this fix, simply insert the above code snippet into your app.js file after the lines:

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');

Answer №3

If you're looking for a solution, one option is to adjust the delimiters in your templates Mustache-style and leave yield as it is. You can find more information on how to set delimiters here:

For example, in your template:

{{=<% %>=}}
<section><% name %></section>
<%={{ }}=%>

While this method may be a bit outdated, it could still be useful for someone facing a similar issue.

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

The validation using regex is unsuccessful when the 6th character is entered

My attempt at validating URLs is encountering a problem. It consistently fails after the input reaches the 6th letter. Even when I type in "www.google.com," which is listed as a valid URL, it still fails to pass the validation. For example: w ww www ww ...

When dragging a new connection in jsPlumb, the existing one should be automatically removed

After using jsPlumb, I was able to create a setup with the following features: Multiple nodes functioning like nodes in a unique flowchart design. Each node has a single target where connections can be dropped. Every node has zero, one, or more exits. Ea ...

What's the importance of including (req, res, next) in the bodyParser function within Express?

My original use of bodyParser looked like this: app.use(bodyParser.json()); However, I now have a need to conditionally use bodyParser: app.use((req, res, next) => { if (req.originalUrl === '/hooks') { next(); } else { ...

Exploring the world of web development with JavaScript and the randomization magic

As per information from a Stack Overflow discussion, Math.random() in JavaScript seems to rely on the browser or operating system, indicating that there is no standard algorithm for generating uniform random variables in JavaScript. Another discussion thre ...

Transform the javascript ES6 class into a functional programming approach

I am interested in converting my reactjs class to a functional programming approach rather than using OOP. Can anyone provide guidance on how to achieve this? Please refer to my code snippet below. import * as h from './hydraulic'; const vertic ...

Setting the CSS position to fixed for a dynamically generated canvas using JavaScript

My goal is to fix the position style of the canvas using JavaScript in order to eliminate scroll bars. Interestingly, I can easily change the style using Chrome Inspector with no issues, but when it comes to implementing it through JS, I face difficulties. ...

retain the input data from the form by using the keyup event

Currently, I have a scenario where an input field is used to capture user input. Instead of displaying the entered value directly, I am looking to store it in a variable and subsequently use it to retrieve data from a database. Below is the code snippet I ...

Creating crawlable AMP versions of Angular websites

Having an Angular website where I dynamically load object properties, I am creating separate AMP sites for each of these objects. Typically, I would link to the AMP site from the canonical site. However, the issue arises because the crawler cannot find the ...

Preserving the <script> tag when defining HTML code

During an AJAX call, I am receiving plain HTML with some JavaScript code. When I try to display this response in a container using .html (jQuery) or innerHTML (plain JavaScript), it removes the <script> tag and all JavaScript code. However, when I c ...

Transfer the data from an XML element to generate a fresh element with an integrated value

I am looking to modify an XML file that contains multiple products by adding a new element with values from existing elements. Specifically, I want to add a new element to each product titled SKU, which should include the values of ProductOption, PurchaseO ...

Unlimited Caching: A Guide to Storing HTTP Responses permanently

Is there a way to send an HTTP response that will be cached by any client indefinitely, so that the browser can retrieve it from the local file system without making any new HTTP requests when needed? Just imagine using this for versioned client code in a ...

What is the proper method for submitting a mail form via AJAX without using jQuery?

Currently, I am working on integrating a PHP mail form with AJAX. The aim is to create a straightforward form that collects user information such as phone numbers and sends it to my email address. While I have managed to set up the form to send emails usi ...

Combining plugin setups and functionalities into a unified script

When it comes to grouping plugin initializations and scripts in a website, I often have a "tools.js" file that contains various functions, click events, and more. I prefer keeping all these script calls centralized in one file for simplicity, organization, ...

Tips on how to retrieve a variable from an array in Vue JS

New to Javascript and Vue, I am exploring examples to grasp the fundamental concepts. <template> /*<p v-bind:class="['bold', 'italic', isValid ? 'valid' : 'invalid']">*/ <p v-bind:class= ...

What is the best way to expand a jade layout using a view/parent/child hierarchy?

My Views are organized in a specific structure. I have been trying to extend the layout.jade to all jade files located under my user folder. However, using extends ../layout in the files under the user folder doesn't seem to work as expected. Unfortun ...

v-autocomplete no selected option

Within my Vue.js 2 and Vuetify component, I am receiving the following data : [ { "anio": 2022, "__typename": "Grupo" }, { "anio": 2020, "__typename": "Grupo" }, { "anio": 2018, "__ ...

Steps for implementing AJAX to display a success function and update database results in real-time

I'm struggling with allowing my AJAX call to send data to my PHP file and update the page without a reload. I need the success message to display after approving a user, but their name doesn't move on the page until I refresh. The goal is to app ...

Different body background color changes every second

I have a function called makeRandColor that generates a random color using RGB and template string literals. However, I am struggling to figure out how to make it work every second. I tried using setInterval but it doesn't seem to be functioning prope ...

What is the process for executing PhantomJS commands through a NodeJs server?

My current challenge involves setting up a Node Server and incorporating PhantomJS commands within the NodeJS server. An example command includes: phantomjs phantom-server.js http://example.com Although I found some information related to this issue on ...

limitations on passing url parameters in react.js

Currently, I am facing an issue with passing URL parameters in my React.js web app. For illustration purposes, here is a snippet of the routes: import React from "react"; import {BrowserRouter, Route, Switch, Redirect} from 'react-router-dom'; ...