[RFC|Merged] Template tag to include JS files

These requests for comments/change have lead to an implemented feature that has been successfully merged into the 3.1/Ascraeus branch. Everything listed in this forum will be available in phpBB 3.1.
User avatar
callumacrae
Infrastructure Team
Infrastructure Team
Posts: 1046
Joined: Tue Apr 27, 2010 9:37 am
Location: England
Contact:

[RFC|Merged] Template tag to include JS files

Post by callumacrae » Mon Feb 20, 2012 11:25 am

I propose an INCLUDEJS template tag. It would work as follows:

Code: Select all

<!-- INCLUDEJS 'template/somefile.js' -->
It would be callable from anywhere with the document, and would add a script tag to include JS in the footer after jQuery.

This would have the advantage of reducing the number of conditionals in the header and footer for JavaScript files that are only used on some pages (such as editor.js).
Last edited by callumacrae on Tue Feb 21, 2012 9:40 am, edited 1 time in total.
Made by developers, for developers!
My blog

User avatar
Arty
Registered User
Posts: 970
Joined: Wed Mar 06, 2002 2:36 pm
Location: Mars
Contact:

Re: [RFC] Template tag to include JS files

Post by Arty » Mon Feb 20, 2012 1:31 pm

+1

Another advantage of using this is allowing template inheritance functionality. No more {T_SUPER_TEMPLATE_PATH} to include js file from main style. To locate js file phpBB can use template locator class.

How would it be implemented code wise? I suggest using the easy way: converting it to HTML <script> code and adding it to template variable {SCRIPTS} that will be added after jQuery in footer template

Another question is how path to js files will be calculated? Relative to template directory or relative to style directory? I think relative to template directory would make more sense for consistency with INCLUDE and allow path to start with ../ if js file is in other style's directory
Formerly known as CyberAlien.

Free phpBB styles | Premium responsive XenForo styles

User avatar
Arty
Registered User
Posts: 970
Joined: Wed Mar 06, 2002 2:36 pm
Location: Mars
Contact:

Re: [RFC] Template tag to include JS files

Post by Arty » Mon Feb 20, 2012 4:37 pm

Test implementation: https://github.com/cyberalien/phpbb3/tr ... /includejs
It also includes second test style: progreen, that inherits from prosilver.

Implemented instructions:

Code: Select all

<!-- INCLUDEJS test.js -->
This will locate file test.js and append code for it to variable {SCRIPTS}. If file is not found, locator returns url to where file is supposed to be in current style.
Then in overall_footer.html after jQuery I've added variable {SCRIPTS}

It also works with template variables, like this:

Code: Select all

<!-- DEFINE $TEST = 'test.js' -->
<!-- INCLUDEJS {$TEST} -->
In cases like this files are located dynamically during template display, so its a bit lower than hard coded file names.


In order to implement that, I had to add some other functionality that might be useful for other modifications:
- $template->context->append_var(var, value) - append to template variable (maybe add template->append_var for it too?)
- $template->locate(file, is_template) - finds file, returns file name relative to phpBB root directory. If second parameter is true, $file is relative to template directory, if false, $file is relative to style directory.
Formerly known as CyberAlien.

Free phpBB styles | Premium responsive XenForo styles

User avatar
callumacrae
Infrastructure Team
Infrastructure Team
Posts: 1046
Joined: Tue Apr 27, 2010 9:37 am
Location: England
Contact:

Re: [RFC] Template tag to include JS files

Post by callumacrae » Mon Feb 20, 2012 5:42 pm

Looks good.

Could this be moved to 3.1, please?
Made by developers, for developers!
My blog

User avatar
nextgen
Registered User
Posts: 128
Joined: Sat Jul 24, 2010 4:59 am
Location: Guatemala
Contact:

Re: [RFC] Template tag to include JS files

Post by nextgen » Mon Feb 20, 2012 6:11 pm

Arty wrote:Test implementation: https://github.com/cyberalien/phpbb3/tr ... /includejs
It also includes second test style: progreen, that inherits from prosilver.

Implemented instructions:

Code: Select all

<!-- INCLUDEJS test.js -->
This will locate file test.js and append code for it to variable {SCRIPTS}. If file is not found, locator returns url to where file is supposed to be in current style.
Then in overall_footer.html after jQuery I've added variable {SCRIPTS}

It also works with template variables, like this:

Code: Select all

<!-- DEFINE $TEST = 'test.js' -->
<!-- INCLUDEJS {$TEST} -->
In cases like this files are located dynamically during template display, so its a bit lower than hard coded file names.


In order to implement that, I had to add some other functionality that might be useful for other modifications:
- $template->context->append_var(var, value) - append to template variable (maybe add template->append_var for it too?)
- $template->locate(file, is_template) - finds file, returns file name relative to phpBB root directory. If second parameter is true, $file is relative to template directory, if false, $file is relative to style directory.
+1
*Imagine a signature super spectacular.*

User avatar
brunoais
Registered User
Posts: 958
Joined: Fri Dec 18, 2009 3:55 pm

Re: [RFC] Template tag to include JS files

Post by brunoais » Tue Feb 21, 2012 2:01 pm

Looks good. Does that work among the template inheritance RFC?

User avatar
callumacrae
Infrastructure Team
Infrastructure Team
Posts: 1046
Joined: Tue Apr 27, 2010 9:37 am
Location: England
Contact:

Re: [RFC] Template tag to include JS files

Post by callumacrae » Tue Feb 21, 2012 2:10 pm

brunoais wrote:Looks good. Does that work among the template inheritance RFC?
Yes
Made by developers, for developers!
My blog

User avatar
brunoais
Registered User
Posts: 958
Joined: Fri Dec 18, 2009 3:55 pm

Re: [RFC] Template tag to include JS files

Post by brunoais » Wed Feb 22, 2012 9:22 am

^ This will certainly create more transparency between the final code (the cached code) and the template code which means more flexibility without hitting performance. I love it!
* brunoais went to take the pills to calm down the excitment (kidding :D )

Anyway, for optimization purposes, we could have that tag to place the script after jQuery and we could have another tag to place the script before jQuery. This is meant for optimization only, no big deal if you don't want to.
callumacrae wrote:You and performance >_<
I'm a speed freak and hate when things take longer as they should.
Last edited by brunoais on Wed Feb 22, 2012 10:11 am, edited 2 times in total.

User avatar
callumacrae
Infrastructure Team
Infrastructure Team
Posts: 1046
Joined: Tue Apr 27, 2010 9:37 am
Location: England
Contact:

Re: [RFC] Template tag to include JS files

Post by callumacrae » Wed Feb 22, 2012 9:25 am

You and performance >_<
Made by developers, for developers!
My blog

User avatar
Arty
Registered User
Posts: 970
Joined: Wed Mar 06, 2002 2:36 pm
Location: Mars
Contact:

Re: [RFC] Template tag to include JS files

Post by Arty » Thu Feb 23, 2012 11:44 pm

Updated tree to latest code that uses [RFC] Template class changes. Also changed all scripts that could be changed from <script> to INCLUDEJS without changing scripts (yes, all 3 of them :cry: )

Will have to wait for that PR to be merged before making PR for this RFC because having 2 pull requests that share code will get messy.
Formerly known as CyberAlien.

Free phpBB styles | Premium responsive XenForo styles

Post Reply