Is there a method to write a response in an EJS template? - ejs

With classic ASP you can do a response.write('foo') inside <% ... %>. Is this possible with EJS?
The current workaround is to use 2 tags, one for logic, one for output:
<%
let someString = generateSomeStringHere()
%>
<%- someString %>
I do understand that some will recommend generating someString outside of the template but let's say for the sake of argument that is not desirable.
We would like to do:
<%
let someString = generateSomeStringHere()
ejs.write(someString);
%>

Related

How do I generate indentation events when using '<% ... %>' bounded templates in StringTemplate

This is related to
https://www.stringtemplate.org/api/org/stringtemplate/v4/AutoIndentWriter.html
https://github.com/antlr/stringtemplate4/blob/master/src/org/stringtemplate/v4/AutoIndentWriter.java
If I have a template like...
foo(bar) ::= <<
{
inner
}
>>
...it will cause an indentation event before 'inner' which ultimately calls pushIndentation.
My question is how to cause an indentation event when using '<% ... %>' templates?
Or, what would be the equivalent to the previous template?
The following is similar but not equivalent to the previous example.
foo(bar) ::= <%
{
<\n><\ ><\ >inner
<\n>}
%>
What needs to be done to make it equivalent?

How to pass parameter to a java function in JSP scriptlet?

I want to pass the value of assembler.area.id, sent by controller to the java function getIsAreaWaitTimeActiveByAreaId(), inside scriptlet. I am not sure how to do so. getIsAreaWaitTimeActiveByAreaId() takes in Long.
<c:set var="isWaitTimeActive" value="${assembler.area.id}" scope="page"/>
<%#page import="com.ihc.wtrack.service.impl.ReportServiceImpl"%>
<%
ReportServiceImpl reportController = new ReportServiceImpl();
boolean isActive = reportController.getIsAreaWaitTimeActiveByAreaId(isWaitTimeActive);
%>
Thanks.

mvc2 dropdownlist dynamic parameters

<% string disabled="new {disabled='disabled'}"; %>
<%= Html.DropDownList("clientId", someObject, disabled)%>
In the above code I want the text disabled to be replaced by what ever value I set that string to. When I check the HTML source on the page, I see that new {disabled='disabled'} has been added as a new item in the dropdown list instead of a property. How do I fix this?
The third parameter of DropDownList helper must be an object that contains the HTML attributes or object of type IDictionary<string, object>.
This is the proper solution:
<% var disabled = new { disabled = "disabled" }; %>

Outputting data whilst in a partial view

I have some MVC2 code that loops a collection of type Product held within the view model. Each time I want to output the data I have to use <%: %> along with using <% %>, for example:
<% foreach (Product item in Model.ProductsCollection) {
if (item.doesExist == true) { %>
<%: item.name %>
<% } %>
Is there any way to still output item.name without having to close & open tags, e.g. in classic asp we used response.write()?
I appreciate the need to encode data using <%: %>, and am not wanting to bypass this, just wanting to output the encoded data without needing the bracket overhead.
Thanks
You can use Response.Write() in conjunction with Html.Encode()
<% foreach (Product item in Model.ProductsCollection) {
if (item.doesExist == true) {
Response.Write(Html.Encode(item.name));
}
} %>
Not in aspx view engine, sir.
Check out MVC3 rc2 and #razor view engine.
#razor is the Man.

public definition of GetEnumerator in asp.net mvc missing?

Should I manually create a definition for GetEnumerator? Seems like it should know...
I get the following error:
foreach statement cannot operate on
variables of type
'MvcAppNorthwind.Models.Product'
because
'MvcAppNorthwind.Models.Product' does
not contain a public definition for
'GetEnumerator'
Line 9: <h2><%: ViewData["Message"] %></h2>
Line 10: <ul>
Line 11: <% foreach (MvcAppNorthwind.Models.Product p in ViewData.Model) { %>
Line 12: <li><%= p.ProductName %></li>
Line 13: <% } %>
In my controller I have this code:
NorthwindDataContext db = new NorthwindDataContext();
ViewData["Message"] = "Welcome to ASP.NET MVC!";
var products = from p in db.Products
select p;
return View(products);
I changed the declaration in my view like this and now it works:
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcAppNorthwind.Models.Product>>" %>
But if you want to display or use data from several models in the same view? How do you do it then?
Change the type of "products" from var to IEnumerable<MvcAppNorthwind.Models.Product> and make sure your cast reflects the same.
In answer to your last question, you could assign objects to a dictionary item in ViewData OR better yet you could create a View Model that contains all of the data that you need for the view. That way you have better separation of concerns by having a model that is specific for your view.