0

I want to put a php code inside of Yii::app()->clientScript->registerScript of yii1

How can I put it inside of this one?

<?php Yii::app()->clientScript->registerScript("form", <<<JAVASCRIPT
// I want the PHP code inside of this
JAVASCRIPT, CClientScript::POS_END); ?>

is there a way besides of ending the PHP in the middle of the code?

EDIT

if I put <?PHP ?> in the middle I'm getting an error of

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\yii\project\protected\views\group_Form.php on line 275

and that line is

JAVASCRIPT, CClientScript::POS_END); ?>
Jovs
  • 852
  • 7
  • 23

1 Answers1

1

Since you can access the controller instance via $this in view context, I would suggest you doing something like that:

  1. Create partial view php file where you can build your mixin (js + php) which obviously will contain any script type with some conditions on top of PHP.

  2. Use CBaseController#renderPartial (which actually returns string instead of rendering, according to 3rd parameter return = true) in view context to get the mixin view contents as string and pass it as 2nd parameter to Yii::app()->clientScript->registerScript.

The implementation would look like this:

// _partial_view.php 
// here is your mixin between js + php
/* @var $this CController */

<?php

echo '<script>' .
   Yii::app()->user->isGuest 
   ? 'alert("you\'re guest")' 
   : 'alert("you\'re logged user")' 
. '</script>';

Then go back to your registering js invocation:

<?php Yii::app()->clientScript
                ->registerScript("form",
                                 $this->renderPartial('_partial_view.php', [], true), // setting 3rd parameter to true is crucial in order to capture the string content instead of rendering it into the view
                                 CClientScript::POS_END); ?>
G.Spirov
  • 198
  • 9
  • Is there no other way that I'm not gonna use a quote ? My javascript already had a good amount of line code so using a single quote or double quote would be a little hassle because of changing the structure of the whole code if not some. – Jovs Apr 16 '20 at 15:17
  • I strongly suggest you to split your mixin into separate partial view in case of readability and reusability. It will make your mixin reusable instead of involved with single page. The more structured organisation to your concepts, the less spaghetti. – G.Spirov Apr 16 '20 at 15:46
  • Well I tried what you answer after editing it for so many time this is what I get `require(_formScript.php): failed to open stream: No such file or directory` – Jovs Apr 16 '20 at 16:40
  • my code are this `clientScript->registerScript("form", $this->renderFile('_formScript.php', array( 'vendors' => $vendors, 'group' => $group, 'descriptions' => $descriptions, ), true), 2);` – Jovs Apr 16 '20 at 16:41
  • Are you sure you're pointing the renderFile to proper view path? Because the error which you had presented me basically says that you do not describe properly the view path. – G.Spirov Apr 16 '20 at 16:48
  • yes im sure and they're on the same folder – Jovs Apr 16 '20 at 16:58
  • your renderFile is not working. I changed it to renderPartial and its working fine now. – Jovs Apr 16 '20 at 17:02
  • Basically they do the same thing. I will edit my answer to be up-to-date. I'm glad to hear that my answer helped you somehow. – G.Spirov Apr 16 '20 at 17:04
  • Yeah it did help me to solve it but I still prefer to not separate it in PHP and fully just echo the javascript. its not easy to maintain in this way. – Jovs Apr 16 '20 at 18:22