easywebhub / tasks

List of tasks for EasyWeb members
1 stars 4 forks source link

Bổ sung helper, lookupTag #87

Open baotnq opened 6 years ago

baotnq commented 6 years ago

Mô tả

@nemesisqp support dùm anh nghe.

nemesisqp commented 6 years ago
ewh-support commented 6 years ago

anh đã thử bỏ code phía trên của em, và chạy với lookupTag bên dưới

{{#with (lookupTag AllTag 'thuoc-ca' ) }}
   <div class="mh-image-caption mh-posts-list-caption">{{displayName}}</div>
{{/with}}

Lỗi như trong hình easybuild-lookuptag_errors

Anh nghĩ chỗ xử lý bên trong hơi khác nhau đó, ku kiểm tra lại 1 lần nữa nghe.

nemesisqp commented 6 years ago

new handlebars-helper.js

'use strict';

var HandlebarsLayouts = require('handlebars-layouts');
var Helpers = require('handlebars-helpers');
var moment = require('moment');

module.exports = function (Handlebars) {
    Handlebars.registerHelper(HandlebarsLayouts(Handlebars));
  //  Helpers({handlebars: Handlebars});
    ['array', 'code', 'collection', 'comparison', 'date', 'fs', 'html', 'i18n', 'inflection', 'logging', 'markdown', 'match', 'math', 'misc', 'number', 'path', 'string', 'url'].forEach(function(name) {
        Helpers[name]({
        handlebars: Handlebars
        });
    });

    // dang ky rivetData helper block cho handlebars ở đây

    // rivetData helper, bat buoc key trong meta data cua content phai la 'rivetData'
    Handlebars.registerHelper('rivetData', obj => {
        if (obj.data.root.rivetData)
            return JSON.stringify(obj.data.root.rivetData);
        else
            return '{}';
    });
    Handlebars.registerHelper('ifCond', function(v1, v2, options) {
        if(v1 === v2) {
          return options.fn(this);
        }
        return options.inverse(this);
      });

    Handlebars.registerHelper('json', function (obj) {
        return JSON.stringify(obj);
    });

     Handlebars.registerHelper('toString', function (obj) {
        return obj.toString();
    });
    Handlebars.registerHelper('removeIndex', function (url) {
        return url.replace('index.html', '');
    });

    var lookupEx = function (obj, propertyPath) {
        if(!propertyPath.split) propertyPath = String(propertyPath);
        if (propertyPath === '') return obj;
        var props = propertyPath.split('.');
        var current = obj;
        while(props.length) {
            if(typeof current !== 'object') return undefined;
            current = current[props.shift()];
        }
        return current;
    };

    Handlebars.registerHelper('lookupCategory', function (obj, childPath, propertyPath, ctx) {
        if(!childPath.split) childPath = String(childPath);
        if (typeof(childPath) !== 'string') childPath = '';
        if (typeof(propertyPath) !== 'string') {
            ctx = propertyPath;
            propertyPath = '';
        }

        var chunks = childPath.split('.');
        var count = 0;
        var node = obj;
        // find child have matched full name
        chunks.some(function (name) {
            count++;
            var fullLookupName = chunks.slice(0, count).join('.');
            var found = node.children.some(function (childNode) {
                if (childNode['category'] == fullLookupName) {
                    node = childNode;
                    return true;
                }
                return false;
            });

            if (!found) {
                node = undefined;
                return true;
            }
            return false;
        });

        if (typeof(propertyPath) === 'string' && node != undefined) {
            // lookup property of found child
            return lookupEx(node, propertyPath);
        }
        return node;
    });

    Handlebars.registerHelper('lookupTag', function(obj, childPath, propertyPath, ctx) {
        if(!childPath.split) childPath = String(childPath);
        if (typeof(childPath) !== 'string') childPath = '';
        if (typeof(propertyPath) !== 'string') {
            ctx = propertyPath;
            propertyPath = '';
        }

        var node = obj[childPath];
        if (!node || typeof(node) !== 'object' || typeof(propertyPath) !== 'string') return null;

        // lookup property of found child
        return lookupEx(node, propertyPath);
    });

    /**
     * Lookup nested object
     */
    Handlebars.registerHelper('lookupEx', lookupEx);

    /**
     * return array of category from root to leaf of @param {string} childPath
     */
    Handlebars.registerHelper('genBreadcrumb', function (obj, childPath) {
        if(!childPath.split) childPath = String(childPath);
        var chunks = childPath.split('.');
        var count = 0;
        var node = obj;
        var ret = [];
        chunks.some(function (name) {
            count++;
            var fullCategoryName = chunks.slice(0, count).join('.');
            var found = node.children.some(function (childNode) {
                if (childNode.category == fullCategoryName) {
                    node = childNode;
                    ret.push(childNode);
                    return true;
                }
                return false;
            });

            if (!found) {
                ret = undefined;
                return true;
            }
            return false;
        });

        return ret;
    });
};

usage

{{#with (lookupTag AllTag 'feature') }}
    <div class="mh-image-caption mh-posts-list-caption">{{displayName}}</div>
    {{#each files}}
        <li>{{title}}</li>
    {{/each}}
{{/with}}

<h1>EasyWeb</h1>
<h3>Default Tag Layout</h3>
baotnq commented 6 years ago

phần lookupTag anh test ok rồi, cảm ơn @nemesisqp . Nhưng sao thử AllTag.children thì không ra vậy ku. Biến này cũng rất quan trọng trong việc hiển thị tất cả tags hiện có, ku bổ sung dùm anh nghe.

nemesisqp commented 6 years ago

AllTag nó là cai js object bình thường nên anh sài handlebar loop được thoải mái

{{#each AllTag}}
    <li>tag name {{@key}} --- {{this.displayName}}</li>
{{/each}}