Creating a Javascript Viewer Using CakePHP

Consider the following URL:

http://site.com/upload/qeSJGs,ZWURb4,qdMMTZ,yM62UX,RlwwWT,ecw7s1 

I need to get all 6 character strings separated by,, into a Javascript array and paste into my page. Here is my controller action:

 public function upload($imageHashes) { $this->set('title_for_layout', 'Uploads successful'); $this->set('imageHashes', explode(',', $imageHashes); $this->layout = 'complex'; } 

and in my view file I have the following:

 <?php echo $this->Html->scriptBlock('', array('inline' => false)); ?> 

Now, it will be very messy to write all Javascript in the scriptBlock method, but I cannot include an external Javascript file because the content changes based on the URL. Is there an easier way to do this that I don't know about?

+4
source share
2 answers

Take an array of imageHashes and json_encode. Then for your script block, you can just do something like

 echo $this->Html->scriptBlock('var jsArray = ' . $json_encoded_array . ';', array('inline' => false)); 
+4
source

For Cakephp 2.x and Cakephp 3.x you can use:

In a View or Template file:

 <?php $this->Html->scriptStart(array('block' => 'scriptBottom', 'inline' => false)); ?> $(document).ready(function(){ console.log('OK'); }); <?php $this->Html->scriptEnd(); ?> 

In the layout file:

 <?= $this->fetch('scriptBottom'); ?> 
+1
source

All Articles