Is there any way to access codeigniter language and config properties from included javascript files?
Good morning!
I'm having great success so far with CodeIgniter. I'm new to PHP and web development in general, but I feel that CodeIgniter is giving me a leg up while I catch up on the basics.
My ques开发者_如何学运维tion for today is this - I have been happily loading config and lang values from my views for a while now, and everything is working fine. But what about JavaScript files being linked into my views? Is there any way to make the $this->lang->line and $this->config->item function references available to me in my JavaScript files?
I am implementing jQuery client-side validation, and would like to pull in my error messages from the server, both to support internationalisation and to make sure that validation gracefully degrades if JavaScript is not available, in that the error messages pushed back into the view from the server-side validation are identical to those displayed dynamically by the jQuery validation. I would not like to have to keep coming back to make sure that these strings are kept in sync. As for internationalisation, I'm fresh out of ideas on how to support that if it turns out that lang and config item strings are completely unavailable from my JS files.
Any help you can provide would be greatly appreciated! :)
You probably shouldn't pollute your Javascript files with stuff that requires that they be evaluated on the server, because that is going to make them ineligible for caching. Instead, perhaps you can have part of your pages contain a small script block where some configuration values are stored (as Javascript globals). Then your standalone .js file can just look for that information on the page.
精彩评论