angular-template icon indicating copy to clipboard operation
angular-template copied to clipboard

Make HtmlTemplate act more like $compile

Open lf94 opened this issue 8 years ago • 5 comments

Right now it's HtmlTemplate(template, scope, options), but Angular does $compile(template)($scope)

$compile should maybe be provided with a factory function, that takes options so it can be configured still.

$compile = $compileProvider(options); // returns $compile function $compile(template)($scope); // does what HtmlTemplate does

lf94 avatar Oct 06 '16 14:10 lf94

If anyone is interested in merging this code, feel free. This is much more angular-like.

index.js

const Express = require('express');
e = Express();
const AngularTemplate = require('angular-template');

const options = {
    prefix: 'ng'
};

$compileProvider = function(options) {
    return a => b => AngularTemplate(a, b, options);
};

$compile = $compileProvider(options);

const pages = {
    '/': {
        template: './templates/master.html',
        controller: function ($scope){

            $scope.content = "Test!";

            return $scope;
        }
    },
    '/home': {
        template: './templates/master.html',
        controller: function ($scope){

            $scope.title = ' | Home';
            $scope.content = "Home";

            return $scope;
        }
    }
};

for(let key in pages) {
    e.get(key, (req, res) => {
        const html = $compile(pages[key].template)(pages[key].controller({}));
        res.send(html);
    });
}

e.listen(3000);

master.html

<!doctype html>
<html>
  <head>
    <title>Game Boy®.Tech{{ title }}</title>
  </head>
  <body>
    <header ng-include="'header.html'"></header>
    <nav ng-include="'nav.html'"></nav>
    <section>{{ content }}</section>
    <footer ng-include="'footer.html'"></footer>
  </body>
</html>

lf94 avatar Oct 06 '16 14:10 lf94

It does make sense to me, 2 step flow aligns with what happens internally. Step 2 is simply jsTemplate function call and step 1 would be reusable. This is kind of the same as jsMode, but without modifying data. This would also most likely be a breaking change, unless we can trust that single param would trigger new flow (I guess we can). What do you think @allenhwkim ?

In the meantime @lf94, since you are using this together with express, be sure to set cache option. If you do not do this, each rendering call will load templates from disk.

FDIM avatar Oct 09 '16 10:10 FDIM

@FDIM I forgot what I did. I am so old :(. It's all yours now, thus, feel free to make changes as long as you are satisfied with. Unless I dive back to this again, it will take some time to have my memory back.

allenhwkim avatar Oct 09 '16 16:10 allenhwkim

Got it @allenhwkim, thanks for response.

FDIM avatar Oct 09 '16 17:10 FDIM

@FDIM I'll be sure to turn on caching :)

lf94 avatar Oct 14 '16 15:10 lf94