Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用URL作为手柄表达式的ExpressJS_Node.js_Express_Handlebars.js - Fatal编程技术网

Node.js 使用URL作为手柄表达式的ExpressJS

Node.js 使用URL作为手柄表达式的ExpressJS,node.js,express,handlebars.js,Node.js,Express,Handlebars.js,我有一个Handlebar视图,它是一个表单集,如果用户正在使用我的/edit/:annotation\u id路径中的表单,我将尝试对其进行操作,以使用值填充表单的字段。我最初的想法是传递一个对象,editMode:req.originalUrl,并将其作为条件使用,但我没有成功。我很好奇,如果表单来自/edit/:annotation\u id路径,那么我应该采取什么方法只显示带有填充值的表单,如果不是,则显示空白表单。有什么建议吗 路线: 批准路线(“/”) get('/edit/:ann

我有一个Handlebar视图,它是一个表单集,如果用户正在使用我的
/edit/:annotation\u id
路径中的表单,我将尝试对其进行操作,以使用值填充表单的字段。我最初的想法是传递一个对象,
editMode:req.originalUrl
,并将其作为条件使用,但我没有成功。我很好奇,如果表单来自
/edit/:annotation\u id
路径,那么我应该采取什么方法只显示带有填充值的表单,如果不是,则显示空白表单。有什么建议吗

路线:

批准路线(“/”)

get('/edit/:annotationId',函数(req,res){

注释-form.hbs:

<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <h2>Add a new annotation</h2>
        <div class="annotation-form">
            {{#if editMode}}
            <form action="/app/edit/{{annotation.annotationId}}" method="post">
                <div class="annotation-form-header">
                    <img class="user-image" src="http://placehold.it/80x80" alt="Generic placeholder image">
                    <label for="annotation-date">name</label>
                    <input type="date" name="name" id="name" value="{{annotation.name}}">

                </div>
                <button type="submit" id="update-button">Update</button>
            </form>
            {{else}}
            <form action="/app" method="post">
                <div class="annotation-form-header">
                    <img class="user-image" src="http://placehold.it/80x80" alt="Generic placeholder image">
                    <label for="annotation-name">Name</label>
                    <input type="date" name="name" id="name">
                </div>
                <button type="submit" id="create-button">Create</button>
            </form>
            {{/if}}

        </div>
    </div>  
</div>

添加新注释
{{{#如果编辑模式}
名称
更新
{{else}
名称
创造
{{/if}

我不知道车把-但您是否尝试过ejs与express的集成

要集成它,您只需要做四件事: 1.将以下内容添加到package.json并运行npm安装:

"ejs": "^2.3.4",
"express-ejs-layouts": "^2.0.0",
  • 更新应用程序以使用ejs:
  • 渲染代码基本上可以保持不变,但下面是一个简化版本:
  • 然后使用ejs语法更新视图/html页面:
  • 
    在编辑模式下,您的html如下所示
    在非编辑模式下,html如下所示
    
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <h2>Add a new annotation</h2>
            <div class="annotation-form">
                {{#if editMode}}
                <form action="/app/edit/{{annotation.annotationId}}" method="post">
                    <div class="annotation-form-header">
                        <img class="user-image" src="http://placehold.it/80x80" alt="Generic placeholder image">
                        <label for="annotation-date">name</label>
                        <input type="date" name="name" id="name" value="{{annotation.name}}">
    
                    </div>
                    <button type="submit" id="update-button">Update</button>
                </form>
                {{else}}
                <form action="/app" method="post">
                    <div class="annotation-form-header">
                        <img class="user-image" src="http://placehold.it/80x80" alt="Generic placeholder image">
                        <label for="annotation-name">Name</label>
                        <input type="date" name="name" id="name">
                    </div>
                    <button type="submit" id="create-button">Create</button>
                </form>
                {{/if}}
    
            </div>
        </div>  
    </div>
    
    "ejs": "^2.3.4",
    "express-ejs-layouts": "^2.0.0",
    
    var expressLayouts = require('express-ejs-layouts');
    app.set('view engine', 'ejs');
    
    res.render('pages/test.ejs',{
        editMode:true
    });
    
    <%if (editMode) { %>
    in editMode your html looks like this
    <%
    }
    else{
    %>
    In non editMode your html looks like this
    <%}%>