Escaping placeholder in Yeoman generator - placeholder

I am trying to use Yeoman to create a generator for a web app and I have *.jsp and *.gradle files which I want to be generated (sometimes just copied) during bootstrapping. Unfortunately Yeoman throws an error when there are JSP comments in *.jsp files like <%# ... %> or when there are placeholders like ${ .. } in *.gradle files.
I guess the reason of the error is that Yeoman treats this entries like placeholders to be processed but it cannot find appropriate values so it throws.
How can I overcome this? How can I escape or disable processing of some kind of placeholders?
I still want to use EJS-styled placeholder <%= .. %> in the same files though.

I had the same problem on maven properties where I have to use ${propertyname}.
I solved it by using the following for templates.
this.template('_pom.xml', 'pom.xml', null, { 'interpolate': /<%=([\s\S]+?)%>/g });
I'm new to yeoman so I don't know what { 'interpolate': /<%=([\s\S]+?)%>/g } does but it worked.

If you want to render jsp tags like:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
you could use <%% which prints a literal <%
So change the previous line for:
<%%#page contentType="text/html" pageEncoding="UTF-8"%>
That solved my code without making adhocs. The same solution can be applied to other scenarios.

Related

Rails 4: how to insert line breaks in text_area?

I have created a blog in rails. I'm a beginner and got quite far, but now I'm stuck with a seemingly minor detail: I can't seem to format the posts (articles).
Here's the relevant part of my show.html.erb:
<p>
<strong>Content:</strong>
<%= simple_format (#article.content) %>
</p>
When I write something and insert html-tags, they are not recognized as such. What am I doing wrong?
Rails will automatically remove html tags to prevent someone from injecting code into your webpage (e.g. malicious javascript)
If your users cannot enter data into #article.content and it's always safe then you can flag it as safe usng the html_safe method.
<%= (simple_format (#article.content)).html_safe %>
Can you post the article content for reference? If I had to guess, I'd imagine Rails is escaping the html tags and inserting them as plain text (so the output looks like: Article content !
Take a look at Rails' helper methods like content_tag (http://apidock.com/rails/ActionView/Helpers/TagHelper/content_tag) and concat (http://apidock.com/rails/ActionView/Helpers/TextHelper/concat) and consider using those to help with generating the appropriate html tags.
An issue to be concerned with is who's going to be supplying the content. For example, if you're writing an application that other people will use, you want to make sure any html give you is escaped to avoid XSS attacks. In that case, you'll want to spend some time reading about how to properly sanitize user input.
You can now specify the tag it gets wrapped in (defaults to p) like so:
<%= simple_format (#article.content, {}, wrapper_tag: "div") %>
or
add white-space: pre-line style.
It will display \r or \n (enter) in user input as a new line.
for more info:
http://apidock.com/rails/v4.0.2/ActionView/Helpers/TextHelper/simple_format

Call a Ruby on Rails helper method from a JavaScript js.erb file

Is it possible to call a ruby helper method from within a js.erb file?
I have a helper in application_helper.rb called solve which makes a API call to a third party service, and they only have a ruby client.
The js.erb file isn't being run on the client side, it is being called from within a controller method and run server side as it is PhantomJS.
I call the JS file from the controller
Phantomjs.run('phantom.js.erb', url)
Then within the PhantomJS js.erb file I have tried
var response = '<%= solve(variable) %>'
which just sets the variable as the string <%= solve(variable) %>
I have also tried
var response = <%= solve(variable) %>
but that just seems to make the application hang and become unresponsive.
I have seen other questions similar to this. In those questions they are asking if it is possible to call it from client side JS which I know you need to use an ajax request to do so.
Is this possible?
Try this:
var content = '#{solve()}'
Need a bit more context for this question, but I'll try my best to answer:
Essentially, you wouldn't be able to access your application_helper methods outside of any .erb files. (ie. if you have application.js or any other js file in your pipeline and you are trying to <%= solve %> from there it wouldn't work - mainly because it isn't an .erb file)
There are a lot of ways and architecture to go about solving this, but here are two simple ones:
If you put the JS you want to evaluate inline on the same page as your partial/html.erb page by using <script> //JS ERB CODE GOES HERE </script> It will actually evaluate properly since it is inside of an erb file. However, this is generally looked upon as unclean...
What you probably want to do is pass the value (presumably) you want that comes from the "solve" application_helper in a 'data' attribute on the html element that it affects. By utilizing "unobtrusive javascript" in this way, you simply pass the value through markup and then in your JS you can get the variable by using jQuery code like this. Here's an example:
<%= link_to "test_link", root_path, :data => {:solve => solve } %>
Of course it doesn't have to be a link, any HTML element will do, and then in your jQuery:
$("#test_link").data("solve");
will return to you whatever output comes out of your "solve" method in the application_helper.
it can possible but there are different ways to do it. one way is define the method in helper_method in your controller and call it from your view. and another way is use the gon gem to access some controller value in your javascript. please check what is best for you please check the below links for more help
http://apidock.com/rails/ActionController/Helpers/ClassMethods/helper_method
https://github.com/gazay/gon
http://railscasts.com/episodes/324-passing-data-to-javascript

In Struts2,How could i redirect control to a java file from a jsp page ,without the use of a form submision?

In Struts2,How could i redirect control to a java code from a jsp page ,without the use of a form submision?
Call action inside jsp
What I understand is that you would like to use Java code in the execution flow of jsp. You can write the code in the class file and later on use the scriptlets to access it.
Please refer the link below:
Scriptlets
You can use redirect within scriptlets
<% response.sendRedirect("myclass"); %>
or
where myclass is class name I used in struts.xml
if you want to use any conditions before this statement then you can make it as a conditional redirect, like
<%
if(condition){
response.sendRedirect("search1"); %>
}
else{
response.sendRedirect("search2");
}
%>
otherwise if you want to use javascript you can use
window.location='mypage.jsp';
or
window.location='myclass';

Rails - Whitespace added to content of textarea on save

I'm trying provide a textarea for the user to enter javascript. Each time the form is saved more whitespace is appended throughout the content. Any ideas how to ensure this doesn't happen?
Using Rails
If you're using a meta-HTML framework such as HAML, you need to ensure that there's no indentation happening to the content of your tag. While this is usually not a problem with ERB, you do need to be aware that whitespace inside the tag is submitted with the form.
Have a look at the source of your page to see what is rendered. It would be useful to append that to your question as a code snippet if possible.
Add a hyphen to the inside of your final %> tag, to prevent Rails from adding a newline and some whitespace. And make sure there's no whitespace in the HTML, of course :)
e.g.
<%= <blah> -%>
instead of
<%= <blah> %>
I converted the ERB to HAML and it works since that. (erubis 2.7.0, haml 4.0.4)

Overriding grails.views.default.codec='html' config back to 'none'

In Grails (<2.3), if I leave grails.views.default.code='none' in the grails Config.groovy, it's up to me to HTML encode my expressions explicitly in the GSP files: ${myValue?.encodeAsHTML()}.
If I set grails.views.default.codec='html" in the Config.groovy, then the HTML encoding happens automatically for every expression: ${myValue}.
My question: If I set the default to 'html', how do I get back to 'none' for one expression when I don't want the HTML encoding behavior?
To summarize the various levels at which the codec can be applied:
Set Config.groovy's grails.views.default.codec='html' to get HTML escaping by default on all ${expressions} in the application.
Then when you want to default a whole page back to none, use the directive:
<%#page defaultCodec="none" %>
or
<%# defaultCodec="none" %>
To disable HTML encoding for one expression in a page that is otherwise defaulting to HTML, use <%=expression%> notation instead of ${...}.
If default encoding level is set to html using
grails.views.default.codec = "html"
then for removing the html encoding for one expression in a page you can use
${raw(expression)}
Try using ${raw(myValue)} , you do not need to declare page codecs etc
From GRAILS-1827, it looks like you can override the default codec for a specific page with
<%# defaultCodec="HTML" %>
or
<%#page defaultCodec="HTML" %>
in some versions (see the referenced issue).
I may have a solution. I'm not sure how accepted it is, though.
I can set the default codec for expressions to HTML, but then use <%=myValue%> notation in GSP instead of ${} expressions to get the unescaped values onto the page.
Write your own tag and write the expression direct to the output stream:
class YourTagLib {
static namespace = "x"
def unescaped = { attrs, body ->
out << attrs.value
}
}
Use it in your GSP:
<x:unescaped value="${yourexpression}"/>

Resources