wbond / pybars3

Handlebars.js template support for Python 3 and 2
GNU Lesser General Public License v3.0
179 stars 46 forks source link

How to use `precompile()`? #45

Closed AniX closed 6 years ago

AniX commented 7 years ago

According to the pybars3 README:

pybars.Compiler().precompile() that is equivalent to Handlebars.precompile()

For performance reasons, I want to render server-side HTML as fast as possible. compile() is too slow, so I was thinking to use precompile(), then store the returned Python code in memcache or database, and if a user request needs a server-sided rendered HTML, the server would pick up the propper precompiled value and run it.

But I'm not sure what to do with the Python code returned by precompile().

(Handlebars.js API reference) [https://handlebarsjs.com/reference.html] explains this usage:

var templateSpec = Handlebars.precompile('{{foo}}');
var template = Handlebars.template(templateSpec);
template({});

I couldn't find an equivalent template() in pybars. As far as I understand pybars3 implementation, precompile() and compile() both share common code in _generate_code(). precompile() only uses the container's full_code, and compile() continues with both, the full_code and the name, to render the (HTML) output.

So, what I'm expected to do with the Python code returned by precompile() to get the rendered (HTML) output?

isaacdd commented 6 years ago

@AniX did you see the example in test_acceptance.py.