Welcome Guest, Not a member yet? Register   Sign In
using CI's $this-> within JavaScript file
#1

[eluser]GF[/eluser]
Hi there,

I'm a brand new CI user, and so far I have been very impressed with the CI framework. Getting my head around MVC is still confusing at times, but overall I see the benefits that CI offers.

However, one thing I still can't figure out is this. I would like to create a dynamically-generated JS file that uses $this->lang->line('some_content') from my Language file.

Specifically, in my template file, I'm using this snippet to produce any extra head content:

if (isset($extraHeadContent)) {
echo $extraHeadContent."\n";
}

The respective controller uses something like the following to reference the various JS files I need for the specific view:

$data['extraHeadContent'] = "";
$data['extraHeadContent'] .= "";

The username_text.php file, located at the /root/js/ folder, looks something like:

<?php
echo 'availText = "'.$this->lang->line("reg_username_available").'";';
echo 'notAvailText = "'.$this->lang->line("reg_username_not_available").'";';
?>

I've tried various approaches, but usually the error Firebug shows is that the $this-> variable is not used properly. I've tried putting the JS file (username_text.php) within the /system/application/ folder, but to no avail.

I'm sure I'm missing something really obvious. Any pointers in the right direction are greatly appreciated.

Blessings,

Graham
#2

[eluser]xwero[/eluser]
If you are using language strings both in javascript and php the best thing to do is to create a php language file and generate a javascript file for instance is the json format. This means you should write language switching code in javascript but that is easy. And then you load the language files like CI does.

This will benefit you when you are putting the framework above the public directory.
#3

[eluser]GF[/eluser]
Hi xwero,

Thanks for the quick reply. Unfortunately I'm not familiar with what you suggest. Can you explain further what you mean by "language switching code in javascript" and "load the language files [in javascript]"?

I would like to make the site multi-lingual capable, so tips on doing that for the JavaScript are greatly appreciated. I've been using the Language class in CI to make the PHP files multi-lingual.

Blessings,

Graham
#4

[eluser]Pascal Kriete[/eluser]
When you echo your script tag it's only outputing the path, it doesn't actually look at the file. That path gets sent to the browser, which then requests the file from the server (using the given path). The problem is that when that request is run, $this has no relation to your CI instance.

For just a few variables, I just include them in a view.

A view - let's call it javascript_vars.
Code:
<script type="text/javascript" charset="utf-8">

    var BASE_URL = '<?=site_url()?>';

</script>

And the template file:
Code:
<?php $this->load->view('javascript_vars')?>
<script type="text/javascript" src="<?=base_url()?>public/js/jquery.js" charset="utf-8"></script>

I've never done it with a lot of variables, like a language file.
#5

[eluser]GF[/eluser]
Hi inparo,

Thanks for the suggestion. I experimented a bit with your idea and came up with a working solution. Thanks again.

Blessings,

Graham




Theme © iAndrew 2016 - Forum software by © MyBB