• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to dynamically load css and js per view (similar to WordPress's wp_enque_script)?

#1
I've used CI3 in the past, but it's been awhile and I'm not finding the answer to this question.

In Laravel, I would implement this by doing something like:

Code:
@section('script')
       <script src="javascript.js" type="text/javascript"></script>
@endsection


But in CodeIgniter, I'm at a loss. I have seen this codeigniter-enqueue library, but it's 2 years old and I wanted to make sure this functionality didn't make its way into CI in that time before going with a 3rd party library.

Any ideas?

How do you handle head/foot injection of css/js?
Reply

#2
I collect additional css files into an array called additional_css and pass it to the header_view. The header view just checks to see if it is set and if it is, outputs the css files in the header view in a foreach loop. Its a bit clunky but it works well for me. I do the same with JS files too in the footer_view, where I also have another array for additional inline js. I suppose it all depends on the complexity of your page formation and if you are using a templating system or themeing system or not.

What does Laravel do for page meta data? In CI I have yet another loop that checks to see if any meta data has been sent and if so sets it in the header. I do the same for canonical link settings too. Again clunky but works perfectly fine.
Reply

#3
Cheers. I ended up just modifying the the class I found for my needs.

As for your Laravel question, you would use @yield('title') Where the title goes and then @section('title', 'Page Title') to set the title. Laravel is the first framework I have used where I have used a templating engine. I don't particularly fancy templating engines, but blade is actually quite nice.

I have two different projects going right now: One using Laravel and one using CI. Between the two, I personally prefer CI because I have used it more and it makes more sense to me. It's put together the way I would put it together. I wrote my own framework many moons ago and CI is similar in architecture. Laravel, however, feels put together by people who really know what they're doing, though. It feels modern whereas CI feels a bit long in the tooth.

That said, my Laravel project is probably getting migrated to Angular.js in the coming weeks, so there's that to look forward to...
Reply

#4
I just watched all the laravel beginner introduction videos again and of course I can see why people like it. There are template libraries like twiggy that accomplish some of the things that blade does in terms of markup. I am not keen on template systems myself either, but I can see where they have their uses. I do know what you mean by the 'long in the tooth' comment, and I am very much looking forward to CI4 and the changes that will bring. Ellis Lab seemed to do their best to kill off CI, before finally handing it over, and CI3 was very much an overhaul with massive amounts of bug fixing. CI4, I suspect, is going to really bring the power of php to the fore, and we are going to have a much more flexible beast to ride.

Anyway, glad you solved the issue. (And welcome back to CI of course).
Reply

#5
I found out about this benchmarking test recently - its just PHP frameworks, all running on the same setup. Last test was a couple months ago, scroll down the page to see all the results: https://github.com/kenjis/php-framework-benchmark
You will be shocked by the results and Codeigniter 3 performance.
Reply

#6
Wow, that is quite a discrepancy. I wonder if it would even up under a real world situation though. However, Laravel does so much 'magic' stuff that I am not surprised it is so much slower. I mean, all that stuff doesn't actually happen by magic, they are just other classes running, and they must be necessarily generalized, and so must add quite a toll on the speed of operation. Look at Phalcon though, wow, that is impressive. I am going to watch their into video out of interest in a mo.

PS Those two, Phalcon and Ice are both pre compiled.
Reply

#7
Do you think Ice doctored their version of the graph :-)

http://www.iceframework.org/
Reply

#8
Back in 2009 the main reason I switched from Kohana to CodeIgniter was because CodeIgniter was noticeably faster. I was impressed with the active forum, and having my questions answered in mere minutes, by people who wanted to help and didn't have a God complex. Over time CI's development has kept things simple and fast for development, and I'd hardly want to think about using something that is slower, no matter what the hipsters think. I don't need the "magic".

There may be other considerations though. For instance, how slow can we expect a framework to be when larger database queries are run and more numerous database queries are run. I can tell you one thing, ORM usage is going to take those benchmarks to a whole new low.

Back on topic, I normally do something like what PaulID is doing. I have my main CSS and JS hard coded into the header, but then have the ability to add more via dynamic data.
Reply

#9
Cheers skunkbad. That's initially what I had found that I did in a prior project, but this one required something a bit more capable.

Back off topic, performance is largely a non-issue at this point for me. We live in a world now where you can get edge-cached content for free with CloudFlare. Even WordPress sites with dozens of plugins on shared hosting environments can be set up to feel extremely responsive to the end user.

For me it's about ease of development and out-of-the-box featureset. I like CI for how dead simple it is. I like Laravel for how feature-packed it is. I haven't played with anything else.
Reply

#10
(05-20-2016, 12:45 PM)PaulD Wrote: Wow, that is quite a discrepancy. I wonder if it would even up under a real world situation though. However, Laravel does so much 'magic' stuff that I am not surprised it is so much slower. I mean, all that stuff doesn't actually happen by magic,

Note that he is the same person who wrote the Codeigniter testing book. I thought maybe it was biased toward CI but in the issues section he responds to people who have questioned this and that. It seems like he has tried to make it as fair as possible, and Codeigniter is not the "winner".
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.