N
N
Nick Tit2017-02-15 00:47:59
Yii
Nick Tit, 2017-02-15 00:47:59

Yii2 how to add javascript to id, type?

Yii2 how to add javascript id, type ?
You need to add javascript to id="some-template" and type="text/x-handlebars-template"
TO THE CODE PAGE

Sample code:

<script id="template" type="text/x-handlebars-template">
Имя: {{name}}
</script>

1st option
//...
$script = <<< JS
    // code
JS;
$this->registerJs($script, yii\web\View::POS_END);

-- not good (registerJs has no way to add id, type)
2nd option
//...
class TmpAppAsset extends AssetBundle{
  public $js = [	
    'js/file.js',
    ];
  
  public $jsOptions = [ 
        'position' => \yii\web\View::POS_HEAD, 
    'id' => 'template'
        'type' => 'text/x-handlebars-template',
    ];	
}

-- not good (I can't load javascript from a file!)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
padlyuck, 2017-02-15
@tit777

Html::script($scriptContent,['id'=>'your-id','type'=>'your-type']);

the downside is that the method will spit out js to where it was called from, and not to the end of the page.
If it is critical to insert js in a certain place, you can use the blocks https://github.com/yiisoft/yii2/blob/master/framew...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question