0

By taking into consideration the following code snipped:

if('<?= Yii::app()->controller->action->id?>' == 'create'){
 $("#Event_name").focusout(function(){
  $.ajax({
    success: function(html)
    {
     type: 'get',
     url: '<?php echo $this->createUrl('related'); ?>'

This works. But... It's this a proper way of doing it ? I would prefer to have php on one side, and javascript on the other side, does it makes sense?

Can someone please provide me an example about how would something like this look like when properly done?

4 Answers 4

1

I am not well experienced with the framework you are using but it is better if you pass the variables from the controller to your view:

Controller:

...
$action_id = Yii::app()->controller->action->id;
$created_url = $this->createUrl('related'); //$this might not be in context here

$this->render('your_view_name', array('action_id'=>$action_id, 'created_url'=>$created_url));
...

View:

if('<?= $action_id ?>' == 'create'){
 $("#Event_name").focusout(function(){
  $.ajax({
    success: function(html)
    {
     type: 'get',
     url: '<?= $created_url ?>'
Sign up to request clarification or add additional context in comments.

3 Comments

"it is better if you pass the variables from the controller to your view" - can I please ask why? I've read that a lot, indeed, here and there, but I'm not sure why is this a best practice. (I do accept any link with a go read it text. ;) )
That's probably the closest to what you're looking for. Even if you use JSON, you'd still need PHP to set the variable in your js code. Ex. var params = <?= json_encode(array('action_id'=>$action_id, 'created_url'=>$created_url))?>, then in your js, you can do: if (params.action_id == 'create')...
@MEM - variables are passed to your views to have a clear separation of controller to view (as much as possible), you see that if you do this in your view, you only have to know what is being displayed, and not worry about the logic behind its value (because it in the controller), aside from that, it is readable
1

Yes, you should use JSON. That way you don't have to worry about quotes or </script> in the middle of the string.

if(<?= json_encode(Yii::app()->controller->action->id) ?> == 'create'){
   ...
     url: <?php echo json_encode($this->createUrl('related')); ?>

2 Comments

so the fact that we have php tags inside javascript isn´t something we should avoid or something. it's natural ?
It's acceptable. Just make sure that you use JSON so that you emit JavaScript literals.
0

Personally, I would recommend moving that condition into your controller. It is very simple and can be handled by PHP, there is no need to check that equation in template.

I'm not very familiar with Yii, but that might be rewritten as follows:

$action_id = Yii::app()->controller->action->id;
$params = array();
if ($action_id == 'action') {
    $created_url = $this->createUrl('related'); //$this might not be in context here
    $params['handler'] = $this->render('yourhandler.tpl.php', array('created_url'=>$created_url));
}
$this->render('your_view_name', $params);

Comments

0

CJavaScript::encode() is your man, it's a great method to pass variables and arrays, or even escaping strings from PHP to JavaScript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.