tibsar tibsar - 3 months ago 12
HTML Question

Converting HTML to Valid String

I currently have a PHP project that uses the Zend Framework. When a user clicks a button, I am using a partial to get the HTML that needs to be added to the page. The problem is that the HTML seems to be returned as HTML rather than a string:

document.getElementById("active_projects").innerHTML='<?php echo $this->partial("project/project/activeprojectstable.phtml", array(
"headers" => $headers,
"active" => $this->active

))?>';


.innerHTML =
takes a string, but the HTML has new lines and special characters that it does not accept. How can I convert the HTML to a valid HTML string?




Update:



For a better idea of the HTML being produced by the partial that is causing the issues, this is what it evaluates to:

error from html

Answer

Try encapsulating your partial into a script tag with type="text/template". Browser will not understand what type of script it is, therefore it will not execute it. Nevertheless, you can still access its contents, even if they are multiline.

<script type="text/template" id="active_projects_template">
  <?php echo $this->partial("project/project/activeprojectstable.phtml", array(
    "headers" => $headers, 
    "active" => $this->active
  ))?>
</script>

<script type="text/javascript">
  // ...
  document.getElementById("active_projects").innerHTML=document.getElementById("active_projects_template").innerHTML;
  // ...
</script>

Similar pattern is being used by templates in AngularJS (type is set to text/ng-template).

Comments