Accessing asset from comment template

4 views
Skip to first unread message

Ivan Vilata i Balaguer

unread,
Mar 1, 2016, 5:34:46 AM3/1/16
to nikola-...@googlegroups.com
Hi, I'm writing a new comment system for Nikola and I'd like to access a
specific Javascript file from the ``comment_form()`` template.
The approach I take is to drop the JS file in ``files/assets/js``, then
add a new ``templates/comments_helper.tmpl``. Its ``comment_form()``
looks like this:

<%def name="comment_form(url, title, identifier)">
%if ...:
<div ...></div>
<script src="${url_replacer('link://post_path/', '/assets/js/foobar.js')}"></script>
%endif
</%def>

Generating the site seems to produce correct (relative) links. I'd like
to ask if this usage of ``url_replacer()`` is correct or if there is a
more appropriate way for referencing such language-independent files.

Thanks!

--
Ivan Vilata i Balaguer -- https://elvil.net/

Chris Warrick

unread,
Mar 1, 2016, 10:17:42 AM3/1/16
to Nikola—Discuss
This is not necessary. You can just use the regular path:

<script src="/assets/js/foobar.js"></script>

and Nikola will take care of fixing the path to be relative to the site root.

--
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16

Ivan Vilata i Balaguer

unread,
Mar 1, 2016, 11:54:08 AM3/1/16
to nikola-...@googlegroups.com
Chris Warrick (2016-03-01 16:17:41 +0100) wrote:

> > […] Its ``comment_form()`` looks like this:
> >
> > <%def name="comment_form(url, title, identifier)">
> > %if ...:
> > <div ...></div>
> > <script src="${url_replacer('link://post_path/', '/assets/js/foobar.js')}"></script>
> > %endif
> > </%def>
> >
> > Generating the site seems to produce correct (relative) links.
> > I'd like to ask if this usage of ``url_replacer()`` is correct or if
> > there is a more appropriate way for referencing such
> > language-independent files.
>
> This is not necessary. You can just use the regular path:
>
> <script src="/assets/js/foobar.js"></script>
>
> and Nikola will take care of fixing the path to be relative to the site root.

It works flawlessly, thank you very much!
Reply all
Reply to author
Forward
0 new messages