0

次のコードを考慮に入れると、次のコードが切り取られます。

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

これは機能します。でも・・・これが正しいやり方なの?一方にphp、もう一方にjavascriptを配置したいのですが、それは理にかなっていますか?

適切に行われた場合、このようなものがどのように見えるかについて、誰かが私に例を提供してもらえますか?

4

4 に答える 4

1

私はあなたが使用しているフレームワークの経験が豊富ではありませんが、コントローラーからビューに変数を渡す方が良いでしょう:

コントローラ:

...
$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));
...

意見:

if('<?= $action_id ?>' == 'create'){
 $("#Event_name").focusout(function(){
  $.ajax({
    success: function(html)
    {
     type: 'get',
     url: '<?= $created_url ?>'
于 2012-06-15T20:39:34.360 に答える
1

はい、JSON を使用する必要があります。そうすれば、引用符や文字列の途中について心配する必要がなくなり</script>ます。

if(<?= json_encode(Yii::app()->controller->action->id) ?> == 'create'){
   ...
     url: <?php echo json_encode($this->createUrl('related')); ?>
于 2012-06-15T20:32:16.067 に答える
0

CJavaScript::encode()これは、変数や配列を渡したり、PHP から JavaScript に文字列をエスケープしたりするのに最適な方法です。

于 2012-06-16T17:52:38.220 に答える
0

個人的には、その条件をコントローラーに移動することをお勧めします。これは非常に単純で、PHP で処理できます。テンプレートでその方程式をチェックする必要はありません。

私は Yii にあまり詳しくありませんが、次のように書き直すことができます。

$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);
于 2012-06-15T21:26:52.943 に答える