Macros for Bottle's STPL

23 views
Skip to first unread message

Vinay Sajip

unread,
Dec 29, 2017, 8:54:34 AM12/29/17
to bottlepy
I want to be able to reuse functions which produce output in Bottle templates, using STPL - functionality which is referred to as "macros" in e.g. Jinja2. I couldn't see any discussion on this forum other than this old post from 2012. I am not sure the approach described there is still valid (given that the include implementation has since changed). To give an example of what I'm talking about: imagine a template full of definitions, such as funcs.tpl given here as an example with just one function:

% def cdnlink(lib, version, type):
% if type == 'css':
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/{{ lib }}/{{ version }}/css/{{ lib }}.min.css" />\\
% else:
<script src="https://cdnjs.cloudflare.com/ajax/libs/{{ lib }}/{{ version }}/js/{{ lib }}.min.js"></script>\\
% end
% end

I'd like to be able to use functions defined like in the following example, page.tpl:

% macrodefs('funcs.tpl', 'macros')
<html>
  <head>
    {{! macros.cdnlink('select2', '4.0.5', 'css') }}
  </head>
  <body>
    {{! macros.cdnlink('select2', '4.0.5', 'js') }}
  </body>
</html>

such that when the page.tpl template is rendered, I can get output like this:

<html> <head> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css" /> </head> <body> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script> </body> </html>

Is there such functionality available, and if not, would there be any interest in adding it, if that could be done in a simple way? I have an idea that it could be done fairly simply and would be willing to come up with a PR if there is interest in merging this type of feature.

Regards,

Vinay Sajip

Reply all
Reply to author
Forward
0 new messages