1

I want to include JavaScript file in my view.

And I want to render it together and not as script file (registerScriptFile).

How can I do that?

6
  • use registerScript() Commented Feb 5, 2013 at 13:41
  • By using registerScript I can't load it from file. Commented Feb 5, 2013 at 14:02
  • 2
    just read the file using file_get_contents() and use its output in registerScript() Commented Feb 5, 2013 at 14:04
  • 2
    Simply add a <script src="..."></script> tag to the view? Commented Feb 5, 2013 at 14:12
  • I'm wondering - why do you want to do this in the first place? I don't understand what's the reason of not willing to include JS file as it's done normally. Why do you need to include content of JS file like that? Commented Feb 5, 2013 at 14:53

2 Answers 2

5

Just renderFile() it would render any file, remember to wrap it with <script> tag.

Sign up to request clarification or add additional context in comments.

2 Comments

So you should use renderFile, and add extension. Should work fine, ie: $this->renderFile('some/file.js'). I also edited my answer.
Thanks, This is the final results. public function actionRenderJS() { header("Content-type: text/javascript"); $js_file = 'scripts/widget.js'; $this->renderFile($js_file); Yii::app()->end(); }
0

Like this

$this->registerJs(
    $this->renderFile(
        '@app/views/path/js/jsfile.js',
    )
, $this::POS_END);

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.