How do people handle not loading ALL the JS libraries on every page using F3?

101 views
Skip to first unread message

Ysguy

unread,
Jun 7, 2018, 7:41:01 PM6/7/18
to Fat-Free Framework
All, 

More of a design question for an intermediate n00b like me. I am wondering about my increasing use of JS libraries throughout my site. Some provide very specific functionality - such as PDF.js to display pdf's in a browser. I want to use common templates for my various site pages and keep the count of templates as low as possible. Currently my "logged in" template has all the JS includes at the bottom. As you can assume, every page using that template loads all the JS libs whether they are needed or not.


I was trying to think of a way that i could set a PHP variable on the inner PHP portion of the rendered page, my logic for page rendering is as follows

$f3->route('GET /',
 
function($f3)
 
{
 $f3
->set('name','Login');
 $f3
->set('content','login/index.php'); //page logic
 echo
View::instance()->render('login/template.php'); //template
 
}
 
);


So i'm thinking of a way to essentially place a variable in login/index.php that the template can read the right libraries in

I do this for page specific self-coded javascript requirements

$f3->route('GET|POST /dashboard/@idx',
 
function($f3)
 
{
 
$f3->set('js','/'.$f3->get("PARAMS.idx").'.js'); //load any per-page custom js code here
 $f3
->set('pagetitle',ucfirst($f3->get('PARAMS.idx')));
 $f3
->set('content','home/'.$f3->get("PARAMS.idx").'.php');
 echo
View::instance()->render('home/template.php');
 
}
 
);


What do people do to dynamically load the JS libraries they need for a page when using templated HTML with PHP includes? 

bcosca

unread,
Jun 7, 2018, 11:40:40 PM6/7/18
to f3-fra...@googlegroups.com
Let the subtemplates dictate which JS libraries to load:

<?php
ob_start();
<script src="pdf.min.js"></script>
$this->addons.=ob_get_clean();
?>

Then in your main template:

<? $this->addons=''; ?>
<? include('subtemplate.php') ?>
<script src="jquery.min.js"></script>
<?= $this->addons ?>

Yoeri Nijs

unread,
Jun 15, 2018, 5:04:47 PM6/15/18
to Fat-Free Framework
Why not working with Webpack, Gulp or something like that and combine all js assets to one minified js bundle, which is uglified and tree shaked?

ved

unread,
Jun 16, 2018, 8:36:03 AM6/16/18
to Fat-Free Framework
Nothing about F3 stops you from using any of those techs.

Also, this plugin can probably help.

Tim

unread,
Jun 16, 2018, 5:05:54 PM6/16/18
to Fat-Free Framework
I just have some conditionals in the template header/footer and set them to render if required from the controller.
Reply all
Reply to author
Forward
0 new messages