2016-11-23 26 views
1

我正在用expressjs構建一個簡單的CRUD操作。我的應用運行良好,默認模板引擎Jade,但我想嘗試Handlebars模板引擎express-handlebars。在句柄中,如何在URL中使用if語句? 我有這樣的代碼在我玉模板:Handlebars使用If語句獲取URL參數

[...] 

form(action="https://stackoverflow.com/users/edit/#{ (_id == undefined) ? user._id : _id }", method="POST").form-horizontal 

[...] 

然後我改變這些代碼到車把:

[...]  

<form class="form-horizontal" action="https://stackoverflow.com/users/edit/{{ _id undefined ? user._id : _id }}" method="post"> 

[...] 

我得到這個錯誤:

Error: Missing helper: "_id" 
    at Object.<anonymous> (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/helpers/helper-missing.js:19:13) 
    at Object.eval [as main] (eval at createFunctionContext (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:9:79) 
    at main (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:173:32) 
    at ret (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:176:12) 
    at ret (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:525:21) 
    at ExpressHandlebars._renderTemplate (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/express-handlebars/lib/express-handlebars.js:247:12) 
    at ExpressHandlebars.<anonymous> (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/express-handlebars/lib/express-handlebars.js:173:21) 

我用這個代碼再次嘗試:

<form class="form-horizontal" action="https://stackoverflow.com/users/edit/{{#if _id undefined}} user._id {{else}} _id {{/if}}" method="post"> 

仍然得到了一個錯誤:

TypeError: Cannot read property 'hash' of undefined 
    at Object.<anonymous> (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/helpers/if.js:16:17) 
    at Object.eval [as main] (eval at createFunctionContext (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:9:32) 
    at main (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:173:32) 
    at ret (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:176:12) 
    at ret (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:525:21) 
    at ExpressHandlebars._renderTemplate (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/express-handlebars/lib/express-handlebars.js:247:12) 
    at ExpressHandlebars.<anonymous> (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/express-handlebars/lib/express-handlebars.js:173:21) 

這裏是我的用戶線路(編輯操作):

router.put('/edit/(:id)', Auth.login, Auth.is_admin, function(req, res, next) { 
    session_store = req.session; 

    req.assert('username', 'Name is required').isAlpha().withMessage('Required letter or number').notEmpty(); 
    req.assert('email', 'Invalid Email').notEmpty().withMessage('Empty Email').isEmail(); 
    req.assert('firstname', 'Required letter or number').isAlpha(); 
    req.assert('lastname', 'Required letter or number').isAlpha(); 

    var errors = req.validationErrors(); 
    console.log(errors); 

    if (!errors) { 
    v_username = req.sanitize('username').escape().trim(); 
    v_email  = req.sanitize('email').escape().trim(); 
    v_firstname = req.sanitize('firstname').escape().trim(); 
    v_lastname = req.sanitize('lastname').escape().trim(); 
    v_admin  = req.sanitize('admin').escape().trim(); 

    User.findById(req.params.id, function(err, user) { 
     user.username = req.param('username'); 
     user.email  = req.param('email'); 
     user.firstname = req.param('firstname'); 
     user.lastname = req.param('lastname'); 
     user.admin  = req.param('admin'); 

     user.save(function(err, user) { 
     if (err) { 
      req.flash('msg_error', 'Error!!!'); 
     } else { 
      req.flash('msg_info', 'Success!!!'); 
     } 

     res.redirect('/users/edit/'+req.params.id); 
     }); 
    }); 
    } else { 
    // displaying an error 
    errors_detail = "<p>Please check your data.</p></ul>"; 

    for(i in errors) { 
     error = errors[i]; 
     errors_detail += '<li>'+error.msg+'</li>' 
    } 

    errors_detail += '</ul>'; 

    req.flash('msg_error', errors_detail); 
    res.render('users/edit', { 
     _id   : req.params.id, 
     session_store : session_store, 
     username  : req.param('username'), 
     email   : req.param('email'), 
     firstname  : req.param('firstname'), 
     lastname  : req.param('lastname') 
    }); 
    } 
}); 

順便說一句,如果我改變操作URL這樣action="https://stackoverflow.com/users/edit/#{{ user._id }}", method="POST")然後它工作。

但我只想知道是否有可能做到以上的事情?

任何幫助,將不勝感激。謝謝。

回答

1

從文檔(http://handlebarsjs.com/builtin_helpers.html):

You can use the if helper to conditionally render a block. If its argument returns false, undefined, null, "", 0, or [], Handlebars will not render the block.

{{#if user._id}}{{user._id}}{{else}}{{_id}}{{/if}} 
+0

我定這樣的代碼:'{{#如果user._id}} {{user._id}} {{}其他} {{_ id}} {{/ if}}'現在就開始工作。謝謝。 – metaphor

+0

對不起,我忘了周圍的鬍鬚,我已經更新了答案。我很高興你現在能夠運行它。 –