How to split a word in JSTL? - jstl

For example, my string is INWORK. I want to split that string into IN ad WORK.
How can I achieve this?

You could use the JSTL EL functions to get a substring:
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
${fn:substring('INWORK', 0, 2)}
${fn:substring('INWORK', 2, 6)}

Related

generating barcode for Barby gem under EAN-13

While attempting to render in HTML a collection of article barcodes and proceeding incrementally to view the data (relative to other objects on tha page), the controller
require 'barby/outputter/html_outputter'
require 'barby/barcode/ean_13'
and the view invokes
<%= this_barcode = article.barcodes.first.barcode %>
<%= this_barcode.class %>
<%# barcode = Barby::EAN13.new(this_barcode) %>
<%# outputter = Barby::HtmlOutputter.new(barcode) %>
<%# barcode.to_html.html_safe %>
[returns as expected]
8001300303466 String
however, when wanting to get the barcode and uncommenting that element it fails to process because data not valid for the line barcode = Barby::EAN13.new(this_barcode) (also occurs with barcode = Barby::EAN13.new(this_barcode.to_i))
note possibly on garden path here, as there is confusion with what the wiki suggests with
barcode = Barby::DataMatrix.new(number) as that would generate the error
uninitialized constant Barby::DataMatrix
How does one ensure the data is correct for proper rendering?
Here's the issue:
8001300303466 has 13 characters. It is the correct barcode.
I was assuming one could submit a correct barcode. However line 54 of the gem's ean_13.rb file
allows a 12 character data element FORMAT = /^\d{12}$/ and proceeds to calculate the check digit.
Thus,
<%= barcode = Barby::EAN13.new(this_barcode[0...-1]) %>
will end up processing the data correctly.

Error in a Phoenix template list

I'm trying to extract values form a list (<%= #evento %>) in a template but I'm getting this error:
lists in Phoenix.HTML and templates may only contain integers representing bytes, binaries or other lists, got invalid entry: %Skeleton.News.Evento{__meta__: #Ecto.Schema.Metadata<:loaded, "news_eventos">, date: "DEZ 2011", id: 69, imgPaths: ["images/fabasa/eventos/one/1.jpg", "images/fabasa/eventos/one/2.jpg", "images/fabasa/eventos/one/3.jpg", "images/fabasa/eventos/one/10.jpg"], inserted_at: ~N[2017-06-30 12:38:15.452214],...
So, my question is how to transform this in a datatype structure that I can still iterate in my template?
You cannot print a list like that in a template because templates only allow iolists to be printed, which are lists containing integers, binaries (also called String in Elixir), or iolists.
If you want to print the inspect representation of the list (the one you see in iex), you can do:
<%= inspect #evento %>
To iterate through the list, you can use for:
<%= for event <- #evento %>
<%= event.id %>
<% end %>

Can JSTL fmt:formatNumber be used to get this output: "1 234.56"?

I'm trying to get numbers formatted in this specific format:
"1 234.56"
So, two decimals, separated by a dot. And grouping thousands with a space char (optionally a single quote). The input values will never be larger than 9999.99.
I tried using patterns fo this, and even playing around with locales, but to no avail.
Here is one way.
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="val" value="9999.99" />
<fmt:formatNumber value="${val}" pattern="#,###.##" var="pat" />
${fn:replace(pat, ",", " ")}

JSTL calculation error

Is this the correct code to print out the calculated value? There seems to be no error but it just directly prints out all my values with the addition, times sign etc.
This is the code:
Monthly Instalment = <c:out value="(${LoanAmount} + (${LoanAmount} * ${IR} * ${param.loanPeriod}))/ (${param.loanPeriod} * 12)" />
You need to wrap the entire calculation in ${...} rather than just the individual variables:
Monthly Instalment = <c:out value="${(LoanAmount + (LoanAmount * IR * param.loanPeriod))/ (param.loanPeriod * 12)}" />
This causes the entire expression to be evaluated, whereas in your case each occurrence of ${...} being evaluated individually and the result inserted into a string.
One cause of that problem is not importing the JSTL library. Try putting
<%# taglib uri="/WEB-INF/c.tld" prefix="c" %>
...at the top of the JSP file.

Dynamic markup with MVC

I have markup stored in a database, which I am pulling out and placing onto a page.
below is a basic sample of what I have, without any db calls for ease of example;
The controller:
ViewData["testMarkup"] = "I was here <%= DateTime.Now.Year %>";
The View:
<%= ViewData["testMarkup"] %>
now this out puts: I was here
and no date, this is because it is ignoring the <%= %> part, is there anyway I can output the above said string and woudl include the year?
Many thanks,
Just do the following:
ViewData["testMarkup"] = "I was here " + DateTime.Now.Year.ToString();
Or am I missing something? Code blocks, such as <%= DateTime.Now.Year %> are only valid when they are part of the markup:
<div>The year is <%= DateTime.Now.Year %></div>
The markup in the database is being treated as a string, not as code in your view language, so it is simply writing it out as text, c# and all.
Two alternate methods:
1 - Use a templating system, such as
ViewData["testMarkup"] = "I was here #YEAR#";
and have a method that replaces your tokens (e.g. #YEAR#) with their values at render time, e.g.,
<%= ReplaceTokens((string)ViewData["testMarkup"]) %>
Where ReplaceTokens looks like:
public static ReplaceTokens(string s)
{
return s.Replace("#YEAR#", DateTime.Now.Year)
}
2 - Store your markup in a partial view, and save the name of the partial view in the database if necessary.
I do believe Phil Haack has the answer to my issue. http://haacked.com/archive/2009/04/22/scripted-db-views.aspx
I will have to check this out and see what happens

Resources