insert JSP file into other JSP file to specific div - html

Is that possible? For example, if I have my main.jsp that is just a container/template in which other JSP files get included, to their specific predefined places.
Something like the following:
<body>
<!-- user input -->
<!-- Insertion point for user input template -->
<div id="user_input">I want user_input.jsp here</div>
<!-- Insertion point for user data template -->
<div id="user_data"> I want user_data.jsp here</div>
</body>
So if I just include files user_input.jsp and user_data.jsp on the top of the page like <%# include file="user_input.jsp" %> then those HTML elements won't be in place. So in user_input.jsp I have the HTML input form, and I want it to be exactly where the <div id="user_input"> is.
How to achieve that?

What you can do is use a JSP fragment (.jspf). Which can put in a fragment of JSP into a specific place in the file.
You can input a .jspf into your jsp file using this:
<%# include file="/path/to/file.jspf" %>
To convert your .jsp files into .jspf, you just put an f at the end, and you may need to reformat your code as if you we're putting it into main.jsp
(eg: removing imports, remove unneccesary HTML tags...etc)
JSP fragments are just JSP files but are pretty much combined with your .jsp file, so any imports that you need you may need to do on your main.jsp.
Hope this helps

Related

Rails generate html file from html.erb template

I know thas it sounds a little weird. I need to generate an html file based on an html.erb template which has many variables. I will give an oversimplified example.
In my viwes/pages/home.html.erb I have:
<html>
<h1> <%= #name %> </h1>
</html>
In my pages_controller.rb:
def home
#name = 'Michaela'
end
When the home funcion is called, it will render the home.html.erb and in the browser, the content will be showed as if it was opnening an .html file that looks like the following:
<html>
<h1> Michaela </h1>
</html>
What I need is to generate an plain html file with that content, after #name has been defined, so if instad of being hardcoded, it´s inputted by the user, the .html file defines it´s content accroding to that user input.
As Maxence recommended, I used the render_to_string helper to finally store the generated html into a variable, and manipulate it in that way.

Need w3-include-html partial include

I´m using this W3 script:
<!DOCTYPE html>
<html>
<script src="https://www.w3schools.com/lib/w3.js"></script>
<body>
<div w3-include-html="content.html"></div>
<script>
w3.includeHTML();
</script>
</body>
</html>
It works perfectly, but my issue would be that I only need one particular portion of the "content.html", and not all of it. So my question is: How do I use w3-include-html for PARTIALLY html include?
Thanks!!
You can't include part of a partial; the whole point of a 'partial' is that it in itself represents part of the code, not code that you should extract a part from.
You can include more than one partial on a page, but the partials themselves must be exactly what you're trying to include at that point; you can't 'extract' content from a partial.
Simply shrink your content.html so that it only contains the output that you would like to include on your main page.
Having said that, considering W3.js can only import HTML, there's literally no reason to store the partial HTML in an external file. Not only does this create a second, unecessary file, but it also adds a reliance on JavaScript. If your visitor opts to disable their JavaScript, your partial won't work. Thus, I strongly recommend simply writing the content of content.html in the main page itself.
Hope this helps!

HTML Template - XML - HTML

I'm working on a mailer system where I'm required to replace variables in html template with actual values in our database. Earlier, I was getting a plain HTML template where I was required to overwrite the variable's actual value from database to the template itself. However, I was not comfortable with it. I made the following suggestion that instead of whole HTML template, pass XML containing the attributes which we seek to replace with actual values from database(sort of RSS). Now I am finding a way to put the actual values found from the database to the original template. Let us take an example:
<html>
<body>
<div var="toxml">
<li>
PROFILEID(This will be a number)
NAME
AGE(etc.)
</li>
</div>
</body>
</html>
The XML which will be generated out of this will be
<PROFILEID val="Number">
<NAME></NAME>
<AGE></AGE>
</PROFILEID>
Now I will stuff in the values from database in this xml only(XML parsing tools are available for php).
Now my question is, how can I put the stuffed values in the XML back to the HTML template from which it came?

Can you get ERB to properly indent when rendered?

I have several partials that I'm including in my Rails application.html.erb file, but the resulting HTML doesn't preserve my indenting (formatting). I've been told that the first line gets rendered with the same indentation-level as the call to _partial.html.erb, but all subsequent lines in the partial just get left-aligned.
This results in code like (see my comments for positioning):
<body>
<div id="outer">
<div class="contentwidth main">
<div class="logo"> <<<<< Shouldn't be this far to the right
<h1>minimal.</h1>
</div><!-- end logo --> <<<<<<< Shouldn't be way over to the left
Is there any way to fix this/format my included partials better using ERB? Or do I need to use HAML?
Not the answer you want - but - no, there's nothing built into Rails to auto-indent ERB.
The doc on filters contains an example of using an after filter to compress the html before sending it to the browser.
Doing something similar, but using something like Tidy to reformat and replace the html where this example does a compress might do the trick.

interpreting escaped html

I am using rails 3.0 and I have an xml file where I store the content of my webpage. So for example, to fill in the body section of a given html page, I extract the content of the tag using REXML methods in ruby.
I would like to store a content with HTML tag inside this XML tag. Say, the following is my favorite content:
<body><strong>XXX</strong></body>
I am inserting this text in its escaped version so that XML parser doesn't interpret it as some content.
"<strong> XXX </strong>"
Running seeds.rb file, I am reading this content to the database and eventually render it as an html page.
I tried many methods, I was unable to obtain what I want, namely: XXX
thanks for your help.
Another easy to output raw content
<%== #content.body %>
It's exactly the same as
<%= raw #content.body %>
it's just a shorthand method to output raw content
try putting the html inside a CDATA section (unescaped):
<xmlNode>
<![CDATA[
<body><strong>XXX</strong></body>
]]>
</xmlNode>
solved the problem, in my XML file, I have this content entered unescaped as it is:
<body>
<strong>XXX</strong>
</body>
in my seeds.rb file, I read the child of node with:
k.children[1].to_s
and finally in the controller, I declare the content as .html_safe:
<%= content_tag(:div, #content.body.html_safe) %>