How to pass textbox param in nodejs - node.js

I tried to pass textbox as a parameter in each render.
and tried following,
in controller,
res.render('index', { dynamic_textbox: '<input type="text" value="test" />' });
and in index.ejs,
<%= dynamic_textbox %>
It displaying result as a string not textbox..
Please any one..

EJS autoescapes output by default. The following will be escaped:
<%= dynamic_textbox %>
You need to use this syntax instead, if you want to output raw HTML:
<%- dynamic_textbox %>

Related

How to trim a result field in an EJS template?

I currently use a simple EJS template to display blog posts:
<main>
<% results.forEach(function(results){ %>
<article>
<section>
<h2><%= results.title %> </h2>
<p>
<%= results.body %>
<label for="<%= results.id %>" class="margin-toggle sidenote-number"></label>
</p>
<input type="checkbox" id="<%= results.id %>" class="margin-toggle"/><span class="sidenote"><%= results.date%></span>
</section>
</article>
<% }) %>
I would like to trim the date field
<%= results.date%>
I did try results.date.substring(1,10) but it returns substring not defined.
May be results.date is not string. You should convert date value to string
<%= results.date.toString().substring(1,10)%>
Or:
<%= '' + results.date.substring(1,10)%>
Try
String(results.date).substring(_,_)
It should work, I had the same issue
String(results.date).slice(_,_)
Worked for me in this situation.
I hadn't realized at first that my date was not a string and thus needed to be casted as one. MYSQL was inserting -0400 GMT EASTERN TIME and wanted to remove that portion, thus my need for this.

cant show data using EJS inside Placeholder

i am trying to show data using EJS inside a placeholder (FORM),
here is my code:
<input class="input" type="text" placeholder="name: <%= user.firstName %>" id="name" required>
on the site, i can see "user:" but nothing more,
and i put the ejs outside the placeholder it work just fine .
try
value= <%= user.firstName %>
Try to wrap "placeholder=" in ejs tags as well, like below.
<input type="text" <%="placeholder="%><%=user.firstName%>>

Node.js pass a value to a partial view

I have a route that looks like this:
app.get('/Search', function (req, res) {
var searchString = req.query.SearchString;
var request = require('./cstapi')(searchString, onBody);
function onBody(body) {
res.render('index', { output: body });
}
});
and an index.ejs that looks like this:
<div id="searchDiv">
<% include ../partials/search %>
<br>
<% include ../partials/results %>
</div>
search looks like this:
<form action="./search" method="GET">
<input type="text" name="SearchString" class="form-control" >
<input type="submit" value="Search" class="form-control">
</form>
and results looks like this:
<p>Search returned the following: <%= output %></p>
I'm trying to get the search results to display in a partial view after the user submits the form. With the above I get output is not defined when attempting to render index and the subsequent partial views.
I found a work around although I'm not 100% sure this is the best way of doing it:
By adding 'null check' to output in index I can avoid the output not defined error when accessing the results partial without search results.
So index now becomes:
<div id="searchDiv">
<% include ../partials/search %>
<br>
<%if (typeof output !== 'undefined') { %>
%> <% include ../partials/results %>
<% } %>
</div>
Hopefully someone can provide a better way?

How to define jade template for using with Backbone

I need to use template for rendering of each ItemView:
var ItemView = Backbone.View.extend({
className: 'item',
template: _.template($('#itemTemplate').html()),
initialize: function () {
}
});
So I need to define html template at first:
<script id="itemTemplate" type="text/template">
<img src="<%= photo %>" alt="<%= name %>" />
<h1><%= name %><span><%= type %></span></h1>
<div><%= address %></div>
<dl>
<dt>Tel:</dt><dd><%= tel %></dd>
<dt>Email:</dt><dd><%= email %></dd>
</dl>
But I use Nodejs Jade Template Engine and I don't understand how shuold I define in it.
Help please.
That's easy, but there's one catch: You don't want Jade to escape attributes content so use foo!='<%= bar &%>' instead of just foo='<%= bar &%>'.
Here we go:
script#itemTemplate(type='text/template')
img(src!='<%= photo %>', alt!='<%= name %>')
h1 <%= name %>
span <%= type %>
div <%= address %>
dl
dt Tel:
dd <%= tel %>
dt Email:
dd
a(href!='mailto:<%= email %>') <%= email %>
It's tested, so you can use it right away :)

How do I get params attributes in post?

I am using Sinatra with Ruby 1.8.7. I'm new to web development, so I don't totally understand get and post, but I got some stuff working. What I need to know next is how to interrogate params in post for certain attributes. In my main file, I have this code:
get "/plan_design" do
erb :plan_design
end
post "/plan_design" do
# do stuff with params
end
In plan_design.erb, I have:
<% if (hash[paramTitle].kind_of?(String)) %>
<div> <input class="planDesignAsset" name="<%= paramTitle %>" value="<%= hash[paramTitle] %>" ></input> </div>
<% else %>
<div> <input class="planDesignAssetNum" name="<%= paramTitle %>" value="<%= hash[paramTitle] %>" ></input> </div>
<% end %>
As you can see I'm using a different class for non-strings. In post, I need to ask params[some_key], what kind of class are you? Then I can treat each param accordingly. Does this make sense?
In Sinatra you use params to access the form data. You should put the values you need into an instance variable, which you can access from your view:
post "/plan_design" do
#title = params[:title]
erb :plan_design
end
<input name="<%= #title %>" />
I’m not sure if this answers your question, but I hope it helps.
Further to Todd answer, you might want to get all params in an instance var i.e
#params = params
& then in the view
you can do
<%= #params[:title] %>

Resources