JSTL : Enable c:set to accept expression as variable name - jstl

I'm trying to modularize my JSTL code. A particular code needs to be called in many places inside a JSP so I've separated it out in a separate INC file. In my JSP, I include the file using <%# include file="snippet.inc" %>. The snippet needs to work on some variables in page context, determined by the caller.
I'm storing the NAME of the relevant variable in a variable called __var which will be used by the snippet. The expectation is that the snippet will read and modify the actual variable in pageScope whose name is passed via __var.
The JSP file:
<c:set var="name1" value="Admin"></c:set>
<c:set var="name2" value="Moderator"></c:set>
<c:set var="__var" value="name1"></c:set>
<%# include file="snippet.inc" %>
${name1}
<c:set var="__var" value="name2"></c:set>
<%# include file="snippet.inc" %>
${name2}
The INC file:
<c:set var="__value" value="${pageScope[__var]}"></c:set>
<c:set var="${pageScope[__var]}"> Hello ${__value} </c:set>
The first line works fine and I can read the value of the variable name. The second line does not work and complains that I can't use expressions as variable names.
How do I go about this issue?

It's things like these that make you realize what a wonderful thing StackOverflow is! Found and modified a solution from https://stackoverflow.com/a/8750411/1101040 to achieve my cause.
<c:set var="__value" value="${pageScope[__var]}"></c:set>
<c:set target="${pageScope}" property="${__var}"> Hello ${__value} </c:set>
All set! :)

Related

Using Redcarpet Gem with .erb file

I am trying to pull up a markdown file from a database that I created. My server. rb file has the following:
get "/designers/:id" do
id = params[:id]
#content = conn.exec_params("SELECT * FROM articles_info WHERE id = $1",[id]).first
erb :raf
end
In that table is the following markdown information:
# **RAF SIMONS**
*THIS IS THE RAF SIMONS BIO* :blush:
*[Raf Link](http://rafsimons.com/)*
The .erb file works when I have plain text or the following in it:
<%= "#{#content["content"]}" %>
However it only prints the actual .md text as it is. So I have tried using Redcarpet to render the markdown file. The code is as follows in my .erb:
renderer = Redcarpet::Render::HTML.new
markdown = Redcarpet::Markdown.new(renderer)
<%= markdown.render(#content.content) %>
But I am getting the error:
ArgumentError at /designers/1
wrong number of arguments (0 for 1..3)
Which is stemming from the <%= line. I thought at first that the syntax was wrong, so I tried:
markdown.render <%= "#{#content["content"]} %>
However that did not change anything. I also tried adding in a method in the server.rb file, but that gave me more errors. I read through the entire documentation: https://github.com/vmg/redcarpet file but am not understanding where the error is coming from. Is it possible that I need extensions in order to make it work? I also tried:
markdown = Redcarpet::Markdown::new(renderer, extensions={})
But that didn't change anything. Not sure what else to try here! I know that it is tricky but I thought I had a handle on it and have now tried many different iterations and still am having trouble executing it. Any advice would be much appreciated!

.erb equivalent to the PHP $_GET["name"];

I have a page set up in PHP which pulls data from the URL and echos it in HTML. Is there a .erb Ruby equivalent to this?
<?php echo $_GET["name"];?>
Is this possible? Thanks!
In ERB, it looks like this:
<%= variable_name %>
If you want to perform some action without printing the result, you do this
<% #user.sign_up %>
Notice the difference being the =

Rendering Error: RangeError: Maximum call stack size exceeded

I'm getting the following error generated from my default.html.eco layout when I attempt to render:
RangeError: Maximum call stack size exceeded
My docpad version is: v6.54.2, and the specific line causing it is this:
<%- #getBlock('scripts').add(['/vendor/foundation.min.js',
'/vendor/audiolib.js','/vendor/freqfinder.js','/vendor/modernizr.js']).toHTML() %>
If I remove this, I get a clean build.
Note that the styles block just above it renders just fine:
<%- #getBlock("styles").add(['/vendor/foundation.css']).toHTML() %>
So I decide to try truncating that list in the scripts block and it works:
<%- #getBlock("scripts").add(['/vendor/foundation.min.js']).toHTML() %>
Any ideas on how to work around this? I'll go file a bug if I'm not doing something wrong - new to docpad.
Do you have a line break in your code? It fails for me when I copy-paste from here to my layout file, but if I delete the line break between '/vendor/foundation.min.js', and '/vendor/audiolib.js' then it compiles as expected.
Alternatively, you could also a string of .add() commands like this:
<%- #getBlock('scripts').add('/vendor/foundation.min.js').add( '/vendor/audiolib.js').add('/vendor/freqfinder.js').add('/vendor/modernizr.js').toHTML() %>
That also comiples fine for me.
And a related note, in case anyone else comes across this error but doesn't have any line breaks: collection.add(null) now causes the same error message. So, if you're doing something like this:
<%- #getBlock("scripts").add( #getDocument().get('scripts') ).toHTML() %>
It will die if you don't have a scripts metadata field on every page.
The fix, however, is pretty simple:
<%- #getBlock("scripts").add( #getDocument().get('scripts') or [] ).toHTML() %>

Play framework: how to pass parameters to include

I want to include an html file in a view and pass parameters to that include.
Can i pass parameters when using: (?)
#{include 'page.html' /}
Something along the lines of a jsp:include where you can specify which parameters you want to pass.
example: In the same page do something like
<jsp:include page="page.jsp">
<jsp:param name="a" value="aValue"/>
<jsp:param name="b" value="bValue"/>
<jsp:param name="c" value="cVlaue"/>
</jsp:include>
<jsp:include page="page.jsp">
<jsp:param name="a" value="aOtherValue"/>
<jsp:param name="b" value="bOtherValue"/>
</jsp:include>
Thanks.
If you want to pass parameters, then you are better off making a groovy tag, rather than creating an include.
More information can be found on the Play documentation - http://www.playframework.org/documentation/1.2.4/templates#tags
Basically, though, you would do the following
#{mytag param:'data', param2:'data2' /}
Then, in the following file app/views/tags/mytag.html, you would add the following (note the underscores)
Some html, which can access the parameters as ${_param} and ${_param2}

undocumented ERB syntax: <%=h ... %>

http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html lists a set of Recognized Tags - however, this list seems incomplete; for one thing it's missing the dash variant (<%- ... -%>, which suppresses line breaks IIRC).
Now I've come across another seemingly undocumented variant:
<%=h some_variable %>
<%= link_to h(some_variable) ... %>
Google wouldn't tell me what that was all about; can anyone point me to an explanation?
It's not an ERB syntax. It is <%= ... %> and inside it is calling the ERB::Util.hmethod
h here is just a regular method, in fact it's an alias for html_escape.
http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html#M000868

Resources