Returning JS containing HTML from PHP - javascript

I am using a PHP foreach loop to create JS variables, containing an HTML entered from a WYSIWYG field.
foreach($Entries as $Entry){
//$$WYSIWYG_Field calls a database field of pure HTML entered via WYSIWYG field.//
$Field_Cured=addslashes($WYSIWYG_Field);
$WYSIWYG_Return=
<<<HTML
'<p><strong>Statement: </strong>$Field_Cured</p>'
HTML;
$Variables.=
<<<HTML
var N_$Entry = '<div class="content">' +
'<span><strong>Name: </strong>$Name</span><br>' + $WYSIWYG_Return +
'</div>';
HTML;
}
I am then using those created JS $Variables to set them as innerHTML when called in a JS function.
My question is robustness based, is it enough to use addslashes() or would there be some characters, if present in the HTML, will break the JS?
In other words:
How compatible is PHP's addslashes(); with the JS syntax?

As far as I know, web servers typically execute the server-side files before any client-side scripting would execute on web browsers. It depends on php.ini and other configuration settings whether there may be warning or errors generated from PHP script that may affect JavaScript on the front-end.
There is PHP DOM to help generate HTML from server-side for JavaScript. Going that route, you would have flexibility about managing data between back-end and front-end.
As a simple example, <element onload="function">, could trigger a JavaScript function on web browsers.
Your code is unclear about the purpose to use String-delimiter and storing JS variable in PHP.

Related

Can I somehow call JSTL function from JavaScript file?

The question is as following: when I write JavaScript inside my JSP page, using JSTL function, it renders normally, understanding everything I want from it. But to make my code clear, I want to move that JavaScript from tag in JSP to a separate file. But when I try to call same function from the file, it doesn't work, but just appends to my page as a simple text.
Here is code example to make this more understandable.
...other JSP stuff
<script>
$.each(data, function(index, item) {
$('#holder').append(
'<tr>' +
'<td>item.price + ' <fmt:message key="currency.default"/></td>'
'</tr>'
);
});
</script>
This works perfect for me. The actual message from the resource bundle is pulled and set instead of the fmt:message function.
But when I move the same code to a separate file, all this doesn't transform and stays plain text.
I understand, that JSP serves on the server, and all transformations with those functions is done much earlier than actual javascript is loaded.
But maybe somebody knows a certain hack to make this work?
Thanks in advance.
You can use DWR for that cause. An old framework but still holds good if that is what exactly you are looking for in your question.
http://directwebremoting.org/dwr/index.html
DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible.
Running java methods or jstl functions(also jstl functions are java methods) from JavaScript is impossible. Because java methods run on server-side but javascript on client-side.
If you want to run java methods in client-side anyway you must create java applet for this. You can run java methods with JavaScript inside your applet. For detailed information see this Java Applet Tutorial
I hope this will help you

Templating engine - JS side or PHP side? Which is better?

I have a templating engine for dynamic page and table rows generation.
The idea is like most templating techniques, that is to prepare an HTML template (visually designing it) and use {{variable_name}} in the HTML block and do a search/replace routine.
At the moment, what i'm doing (and have work flawlessly for months) is that JS sends the HTML template block (say the dynamic/loopable row in a data table (the whole html code) to PHP where it is treated as a template string.. and PHP loops through the results of the SQL query and doing a preg_replace on the template string.
$html_content .= doTemplate("name,address,age",$template_string);
That $html_content is then outputted as a JSON object back to the client browser ..
I know i'm moving more bytes than necessary to and from the backend since my output contains HTML code instead of just the data, but i figured this is a small tradeoff since PHP processing the template replacement is faster than client browser (JS).
This time, I am thinking of doing the templating/string replacements in JS, but im wondering how much slower JS really is doing string.replace routines.
Is it really that much slower?

Implementing rjs in php

I want to implement a pretty basic rjs kind utility in php. For those who have no prior knowledge of it, the rjs utility in Ruby on Rails returns application\javascript content-type for ajax requests. This is pretty handy when we want to change the inner html of two different divs through ajax in one request.
So what I need is to read html stream of some *.tpl.php file so that from the server side code, I can return javascript strings as:
$rjsStr = "$('#div1').html(".Util::getHtmlFromTpl($outputParams1, 'tplfile1.tpl.php').");";
$rjsStr .= "$('#div2').html(".Util::getHtmlFromTpl($outputParams2, 'tplfile2.tpl.php').");";
return $rjsStr;
Now my ajax request would look like $.ajax({url:'/controller/action.php?param1=1&param2=2',success:function(result){eval(result);}})
Edit The problem is that I don't know how to get the html stream of php file. I simply tried include('path_to_file/tplfile1.tpl.php'); but that doesn't return the html stream as a variable.
PS: I don't want to use file_get_contents for the security reasons.
include('path_to_file/tplfile1.tpl.php'); includes code and executes it. If you need to just read the file, then you may use file_get_contents, but I suppose, you need rendered template as input.
Then you need to use output buffering.
ob_start();
include('path_to_file/tplfile1.tpl.php');
$rendered_tpl = ob_get_clean();
// now you may strip excessive part, escape html and do with it whatever you want

Writing HTML layout/template within javascript bookmarklet

I have a bookmarklet which creates a UI for the user to interact with. Currently, I have been using $('<element>').addClass().css({..});, but that becomes hard to maintain. Is there a better way to do this?
I have also tried doing something like
var html = "<div class='someclass'>";
html += "<more html/>";
html += "</div>"
Which is also incredibly hard to maintain. Is there a way I can write html within javascript, or a library like jade that i can use WITHIN a javascript bookmarklet?
Yes, there is a Domain Specific Language implementation in JavaScript to allow you to write more expressive HTML in JavaScript called Pithy.
Please remember that writing large amounts in Pithy is maybe not a good idea and you rather want to use a proper client side templating engine such as DustJS or many others.

Javascript in HTML and ASP file

Since JavaScript can be written (or contained) within an HTML file or in an ASP file, is there any difference?
ASP runs on the server-side. Any HTML or JavaScript generated by this is simply sent to the browser, which is where the HTML is rendered and JavaScript is executed.
ASP is a server side technology that (usually) outputs an HTML document when executed.
Any JavaScript you write might be part of the HTML document (and thus identical to any JS you might put in a static HTML document) or it might be written as server side code (in which case it will execute on the server, have access to ASP APIs instead of Browser APIs, and will generate output instead of being output).
The marvellous thing about Javascript and server-side programming languages like ASP and PHP is that they can be (sort of) intertwined. So, for example, if you have a server-side variable that you want to be able to mess about with using Javascript, you can include it in the JS when you output the page:
// THIS IS THE ASP CODE
string mystring = "This is my ASP string";
string html = "<script type=\"text/javascript\">var mystring = "+mystring+"</script>";
// then output the html
That's a terrible, badly-coded example, but you hopefully get the idea. You can emit Javascript to the page using ASP variables and the like. It's very handy.
One difference I can think of is, the way you bind HTML/ASP controls
--HTML button control--
<input type = "button" id = "myButtonHTML" />
--ASP button control--
<asp:button runat = "server" id = "myButtonASP" />
the way you bind this in javascript will be as follows
--HTML control--
document.getElementById("#myButtonHTML").value
AND
--ASP control--
document.getElementById("#myButtonASP.ClientID").value

Categories