Display data inside input value using Jade

19,249

Solution 1

You can try enclosing the variable in #{} to output it:

input(type="text", name="date", value="#{viewpost.date}")

Solution 2

Pug 0.1.0 (Jade 2.x) removed support for interpolation in attributes, so this works now:

input(type="text", name="date", value=viewpost.date)

See https://github.com/pugjs/pug/issues/2305

Share:
19,249
Trevan Hetzel
Author by

Trevan Hetzel

Updated on July 15, 2022

Comments

  • Trevan Hetzel
    Trevan Hetzel almost 2 years

    I'm fairly new to Jade and am wanting to display some outputted data as the value value of a text input. Like this:

    input(type="text", name="date", value="THISRIGHTHURR")
    

    But only the value needs to be viewpost.date. I've tried multiple ways and none seem to work:

    input(type="text", name="date", value=viewpost.date) // doesn't work
    input(type="text", name="date", value=.=viewpost.date) // doesn't work
    input(type="text", name="date", value=".=viewpost.date") // doesn't work
    

    I of course can get it to work outside of an input by doing something like

    each post, i in viewpost
      h1.=post.date
    

    Am I supposed to loop through in the input somehow too? This is the JS (using Node and Express) that's outputting my viewpost variable.

    // render show post view
    exports.viewpost = function(db) {
        return function(req, res) {
            var id = req.params.id;
    
            collection.find({ "_id": new BSON.ObjectID(id) }, function (err, data) {
                res.render("viewpost", {
                    "viewpost" : data
                });
            });
        };
    };
    
  • Adam F
    Adam F over 9 years
    Don't forget it's in a string!
  • SoManyGoblins
    SoManyGoblins almost 8 years
    This is outdated for Pug, view asym's answer below.