CodeIgniter Forums
using helpers outside the systems folder - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: using helpers outside the systems folder (/showthread.php?tid=11229)



using helpers outside the systems folder - El Forum - 08-31-2008

[eluser]Philipp Datz[/eluser]
hello, maybe an easy question, but i didn't find a way to handle it, neither in the userguide nor in the forums.
the structure of my application looks like this:

application
assets
system

application outside the system.
assets for js/img/etc.
and system.

im storing my js files, in that case mootools-functions and stuff in assets/js, but want to use the uri-helper for the url in the ajax requests. is there a proper way to include/load the helpers in that place?


using helpers outside the systems folder - El Forum - 09-01-2008

[eluser]crumpet[/eluser]
I have not found a perfect solution for this.
One thing you can do is write your ajax functions so that they take a parameter called URL and then call that function from within your view file and use a php brace there where you echo the value you need.

Code:
[removed]
$("form").submit(function(){  
    ajaxFunction(<?php echo uriHelperFunc() ?>);
});
[removed]

Or you can just practice bad coding practises and put all your javascript in your view files and then you can put php wherever you want (what i did)


using helpers outside the systems folder - El Forum - 09-01-2008

[eluser]Pascal Kriete[/eluser]
I usually have a general javascript view that defines an object literal with a few constants. Just put that in front of all your other scripts.
Code:
</script type="text/javascript">
    <!--
        var CI = {
            'base_url': '<?php echo site_url(); ?>/',
            'blab': '<?php echo $something_my_js_needs; ?>'
        };
    //-->
</script>
Then, to call a url:
Code:
var url = CI.base_url + 'some/url/goes/here';



using helpers outside the systems folder - El Forum - 09-29-2008

[eluser]JulianM[/eluser]
I like this idea. Thanks.

I was using a different approach, but I think I will change for this approach. Before, I was using a <a href="&lt;?php echo site_url() ?&gt;ajax.php" id="ajax"> in the view and then in the javascript file (jQuery) I did something like this: $('ajax').attr('href') to retrieve the ajax function to use.


[quote author="inparo" date="1220339580"]I usually have a general javascript view that defines an object literal with a few constants. Just put that in front of all your other scripts.
Code:
&lt;/script type="text/javascript"&gt;
    &lt;!--
        var CI = {
            'base_url': '&lt;?php echo site_url(); ?&gt;/',
            'blab': '&lt;?php echo $something_my_js_needs; ?&gt;'
        };
    //--&gt;
&lt;/script&gt;
Then, to call a url:
Code:
var url = CI.base_url + 'some/url/goes/here';
[/quote]