You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As per OC 8 it is possible to use the Handlebars library to create JS templates.
One problem is how to store the template.
There are multiple possibilities:
Store in a JS variable: a bit ugly but would work with JS unit tests
Store in a PHP template (script tag): it runs but the JS unit tests have no way to read that template out of the PHP template, because there is no running server instance
Store in a separate HTML file: could work, but need to find out how to load the template asynchronously. Trouble is that most of the core JS code is not async-compatible yet so loading the templates could be an issue. Also JS unit tests also need to have a way to load such files asynchronously (it should work, need to figure out the best way).
Needs further research.
Only once these questions are solved, we'll be able to enforce usage of Handlebars.
For now we could go with solution 1).
The problem is not the loading itself, but how to fit the loading into the existing core apps.
If for example we want the files app to preload the template, then we must defer the app's initialization. Might be feasible in the files app as it's more modern than the others (app.js)
But for settings/users or others it might be more tricky / might need additional major refactoring (actually a similar problem like we met for translations)
If we say we want to go with templates in their own files then let's do that.
Let's make them ".html" files and store them in "templates/" ?
Hmm on the other hand any app that wants to switch to such templates would need changing anyway. So even if the template loading is async (ajax loading of html page or of the precompiled JS) the apps would need to be modified anyway.
I need to look into these template formats. But if it's possible to have it as a precompiled JS file, apps could simple preload them with addScript() like we already do implicitly for translations.
Other apps that don't mind using async load could then load these asynchronously.
Another idea, just discussed: have the JS templates stored under "templates/somehandlebartemplate.html".
The template would be included as partial inside one of the PHP templates (probably index.php).
The JS unit test would access the template using Jasmine's built-in server, loading "templates/somehandlebartemplate.html". That loading would be async but could hopefully be done somehow before running the tests.
store the templates as html files "templates/something.html"
use PHP's include() to include them into an existing template and wrap it into a script tag (with handlebar mime type)
the template is available through $('#somethingTemplate') and can be compiled on the client side
Problem: JS unit test cannot preload the "html" file as there is no PHP when running karma. To load the template, the test files need to have extra calls to ajax-load the html templates. Not ideal.
Problem: breaks CSP due to eval
I tried this locally and it is doable, but it is not ideal to have to explicitly load templates in the test files as you cannot always predict what templates a set of classes will need.
Solution 2:
same as solution 1, but for the JS unit test, do some JS magic in "autotest-js.sh" or "karma.config.js" that precompiles every HTML templates with handlebars, then add them to the list of JS files to preload.
Problem: breaks CSP due to eval
Solution 3:
precompile all HTML templates with a script or ./occ command
auto-include precompiled templates with \OCP\Util::addScript() so they are available directly (no async load)
JS unit tests can easily include the precompiled templates as they are on-disk already (no async load)
solves CSP issue
This might only work with assets pipeline enabled, but even then compiled scripts won't be available for the JS unit test runner
Having a script that devs need to run is not acceptable due to the design goal of having everything ready to go after checkout.
And there is the question of checking in the compiled scripts or not. There is a risk of having out of date precompiled scripts. Maybe a git hook could help in some way.
Hey, this issue has been closed because the label status/STALE is set and there were no updates for 7 days. Feel free to reopen this issue if you deem it appropriate.
Activity
DeepDiver1975 commentedon Dec 15, 2014
Hmmm - storing handlebar templates in their own files would even allow precompilation using assetic - see https://github.com/kriswallsmith/assetic/blob/master/src/Assetic/Filter/EmberPrecompileFilter.php
http://handlebarsjs.com/precompilation.html
synchronous and asynchronous loading of handlebars templates:
https://gist.github.com/utkarsh2012/2287070
PVince81 commentedon Dec 15, 2014
The problem is not the loading itself, but how to fit the loading into the existing core apps.
If for example we want the files app to preload the template, then we must defer the app's initialization. Might be feasible in the files app as it's more modern than the others (app.js)
But for settings/users or others it might be more tricky / might need additional major refactoring (actually a similar problem like we met for translations)
If we say we want to go with templates in their own files then let's do that.
Let's make them ".html" files and store them in "templates/" ?
PVince81 commentedon Dec 15, 2014
or ".handlebars" as in your example.
Can handlebars templates be concatenated in such scenario ?
DeepDiver1975 commentedon Dec 15, 2014
*.hbs seems to be the common file extension afaik
DeepDiver1975 commentedon Dec 15, 2014
@PVince81 regarding handlebars and requirejs: http://www.slideshare.net/iivanoo/requirejs-handlebars
PVince81 commentedon Dec 15, 2014
Yes... requirejs would solve a lot of issues... We could use it in core but don't want to make it mandatory for apps...
PVince81 commentedon Jul 3, 2015
Hmm on the other hand any app that wants to switch to such templates would need changing anyway. So even if the template loading is async (ajax loading of html page or of the precompiled JS) the apps would need to be modified anyway.
I need to look into these template formats. But if it's possible to have it as a precompiled JS file, apps could simple preload them with
addScript()like we already do implicitly for translations.Other apps that don't mind using async load could then load these asynchronously.
PVince81 commentedon Jul 16, 2015
Another idea, just discussed: have the JS templates stored under "templates/somehandlebartemplate.html".
The template would be included as partial inside one of the PHP templates (probably index.php).
The JS unit test would access the template using Jasmine's built-in server, loading "templates/somehandlebartemplate.html". That loading would be async but could hopefully be done somehow before running the tests.
PVince81 commentedon Aug 14, 2015
Additional issues with CSP: #14404
PVince81 commentedon Aug 14, 2015
The goal is to be have the templates available synchronously for both the JS apps and also JS unit tests, and possibly solve the CSP issue too.
Solution 0: (what we have now)
Solution 1:
include()to include them into an existing template and wrap it into a script tag (with handlebar mime type)I tried this locally and it is doable, but it is not ideal to have to explicitly load templates in the test files as you cannot always predict what templates a set of classes will need.
Solution 2:
Solution 3:
This might only work with assets pipeline enabled, but even then compiled scripts won't be available for the JS unit test runner
Having a script that devs need to run is not acceptable due to the design goal of having everything ready to go after checkout.
And there is the question of checking in the compiled scripts or not. There is a risk of having out of date precompiled scripts. Maybe a git hook could help in some way.
I still think that some day we'll want to have a bootstrap script, maybe a nice Makefile that will run the scripts for everything (composer, npm, translation, handlebars templates, javascript concat+minification, etc). See how it's done in the news app: https://github.com/owncloud/news/blob/master/Makefile
PVince81 commentedon Sep 1, 2016
Requires #26006 to compile JS template thanks to the new Makefile
ownclouders commentedon Jan 13, 2018
Hey, this issue has been closed because the label
status/STALEis set and there were no updates for 7 days. Feel free to reopen this issue if you deem it appropriate.(This is an automated comment from GitMate.io.)
PVince81 commentedon Jan 15, 2018
leaving closed in favor of #26006 which is the solution
lock commentedon Jul 31, 2019
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.