Handlebars IF value contains x.*

为君一笑 提交于 2021-02-20 05:20:26

问题


I have an issue with some JSON I'm parsing into a template. Essentially, I'm constructing a query via a simple web interface, pulling back data in JSON format, and controlling the data/template with Mustache.

However, I'm unable to query values on nested objects in the JSON via the URL...so I think I need to resort to conditional statements using Handlebars.

Is it possible to run a like or indexof style comparison in an handlebars block helper using wildcards?

i.e.

{{#if folderPath == "content/published/.*"}}
 Render content
Else, do nothing

Appreciate any help, Thanks


回答1:


I know I'm a little late, but I stumbled across this and it should have an answer. I wrote a helper for this that works.

Handlebars.registerHelper('contains', function(needle, haystack, options) {
   needle = Handlebars.escapeExpression(needle);
   haystack = Handlebars.escapeExpression(haystack);
   return (haystack.indexOf(needle) > -1) ? options.fn(this) : options.inverse(this);
});

The template should look like this (Expressions, Block Helpers)

{{#contains "content/published" folderPath}}
    something
{{/contains}}

This will only return "something" if the "content/published" is found. No need for wildcards.

This can be used as a sort of switch case using "else". This part wasn't clear to me in the documentation. Should be useful to someone.

{{#contains "content/published" folderPath}}
    Something
{{else contains "content/something" folderPath}}
    Something else
{{else}}
    Something else v2
{{/contains}}

For those using express-handlebars, your implementation will look like this(in RegisterHelpers.js):

var register = function(Handlebars) {
    var helpers = {
        if_op: (a, op, b, options) =>{
            switch (op) {
                case '!=': return (a != b) ? options.fn(this) : options.inverse(this);
                case '!==': return (a !== b) ? options.fn(this) : options.inverse(this);
                case '>=': return (a >= b) ? options.fn(this) : options.inverse(this);
                case '<=': return (a <= b) ? options.fn(this) : options.inverse(this);
                case '==': return (a == b) ? options.fn(this) : options.inverse(this);
                case '===': return (a === b) ? options.fn(this) : options.inverse(this);
                case '>': return (a > b) ? options.fn(this) : options.inverse(this);
                case '<': return (a < b) ? options.fn(this) : options.inverse(this);
                default: return options.fn(this);
            }
        },

        contains: (Needle, Haystack, options) => {
            Needle = Handlebars.escapeExpression(Haystack);
            Haystack = Handlebars.escapeExpression(Haystack);
            return (Haystack.indexOf(Needle) > -1) ? options.fn(this) : options.inverse(this);
        }
    }

    if (Handlebars && typeof Handlebars.registerHelper === "function") {
        for (var prop in helpers) {
            Handlebars.registerHelper(prop, helpers[prop]);
        }
    } else {
        return helpers;
    }
}

module.exports.register = register;
module.exports.helpers = register(null);



回答2:


You can indeed do this, but you'll need to create a custom Handlebars helper.

Your template will end up looking something like

{{#like folderPath '==' 'content/published/.*'}}
  something
{{/like}}

Where #like is the name of your custom helper.



来源:https://stackoverflow.com/questions/15497794/handlebars-if-value-contains-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!