How to add content for an external file

31 views
Skip to first unread message

Maxime Garnier

unread,
Oct 17, 2022, 5:42:13 AM10/17/22
to nikola-discuss
High all,

I've been starting using Nikola and I have a question. I plan using a shortcode for a card on a "meet the team"-like page and I would like to call that shortcode with the path to a file (txt, possibly html or other) as argument that contains the content to write in the card.

I didn't figure out a way to do this. I am primarily using html files and the standard {%  include %} doesn't seem to work. Or maybe there's a better way to tackle the problem that I didn't thin of?

Anyways, thanks!

Best,

Maxime

Maxime Garnier

unread,
Oct 20, 2022, 3:19:23 AM10/20/22
to nikola-discuss
For more context, what I envision for card.tmpl would be something like

<div class="card" style="justify-content: center">
    <img src="/images/${image_path}.jpg" alt="Jane" class="im-center"  style="width:50%">
    <div class="container">
      <h2> ${name} Doe</h2>
      <p class="title">CEO </p>
      <p>Some text that describes me lorem ipsum ipsum lorem.</p>
<!-- some way to upload text from a file? with %{content_path} -->
      <p>exa...@example.com</p>
      <p><button class="button">Contact</button></p>
    </div>
  </div>

which would be called in my main html by
{{% card name=Maxime image_path=me content_path=/files/location/me.txt %}}

Would something close to that be possible ?

Thanks!

Chris Warrick

unread,
Oct 20, 2022, 5:30:58 PM10/20/22
to nikola-...@googlegroups.com
On Thu, 20 Oct 2022 at 09:19, Maxime Garnier <maxime.g...@gmail.com> wrote:
>
> For more context, what I envision for card.tmpl would be something like
>
> <div class="card" style="justify-content: center">
> <img src="/images/${image_path}.jpg" alt="Jane" class="im-center" style="width:50%">
> <div class="container">
> <h2> ${name} Doe</h2>
> <p class="title">CEO </p>
> <p>Some text that describes me lorem ipsum ipsum lorem.</p>
> <!-- some way to upload text from a file? with %{content_path} -->
> <p>exa...@example.com</p>
> <p><button class="button">Contact</button></p>
> </div>
> </div>
>
> which would be called in my main html by
> {{% card name=Maxime image_path=me content_path=/files/location/me.txt %}}
>
> Would something close to that be possible ?
>
> Thanks!

If you want to read a file from within the template, you could write a
simple Python function for that and expose it in the global context in
conf.py - but note this will not track dependencies correctly. If you
want dependencies to be tracked, you need a custom shortcode plugin.

Another route might be to slightly abuse the post-list mechanism:
every person gets their own page, and the post-list shows them
(including metadata and post content).

A third option would be server-side includes, but those are pretty silly IMO.

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

Maxime Garnier

unread,
Oct 21, 2022, 3:56:56 AM10/21/22
to nikola-discuss
Thanks for your answer! Seems like a bit of work.
Would it be easier to read the txt files and images from the page itself (and not the template)?

How would I go about writing a Python function in conf.py and calling it from the page?

Thanks!

Chris Warrick

unread,
Oct 26, 2022, 5:42:17 PM10/26/22
to nikola-...@googlegroups.com
On Fri, 21 Oct 2022 at 09:56, Maxime Garnier <maxime.g...@gmail.com> wrote:
>
> Thanks for your answer! Seems like a bit of work.
> Would it be easier to read the txt files and images from the page itself (and not the template)?

Pages can't do that, in general. You can use a custom shortcode or a
reST include (if both files are reST).

> How would I go about writing a Python function in conf.py and calling it from the page?

Write a function as usual, and then put it in GLOBAL_CONTEXT ({"foo": foo}).

Maxime Garnier

unread,
Oct 28, 2022, 8:53:46 AM10/28/22
to nikola-discuss
Thanks I'll try that ! Or maybe consider switching to reST if the include works better !

Maxime Garnier

unread,
Oct 28, 2022, 9:42:28 AM10/28/22
to nikola-discuss
Could I get a bit more detail on how to put the function in GLOBAL_CONTEXT and how to call it from a page please ?

Thanks !

Chris Warrick

unread,
Oct 29, 2022, 11:42:06 AM10/29/22
to nikola-...@googlegroups.com
On Fri, 28 Oct 2022 at 15:42, Maxime Garnier <maxime.g...@gmail.com> wrote:
>
> Could I get a bit more detail on how to put the function in GLOBAL_CONTEXT and how to call it from a page please ?

def foo(bar):
return bar + " world"

GLOBAL_CONTEXT = {"foo": foo}

You can use this as ${foo("hello")} within a Mako template or {{
foo("hello") }} from a Jinja template.

Maxime Garnier

unread,
Nov 2, 2022, 4:43:10 AM11/2/22
to nikola-discuss
Thanks a lot for your help, this might solve my problem!

Best,

Maxime Garnier

unread,
Nov 2, 2022, 5:16:25 AM11/2/22
to nikola-discuss
Hi again,

seems like in the template when using ${foo("hello")} I can't get it to use the variable specified when calling the template with for example

(in the main page)
{{% template var = dummy %}}

${foo("${var})} in the template raises a Mako syntax error. Would something like this be possible so that I can call the template from the main page (that calls my function) with different parameters?

Thanks again!

Best,
Reply all
Reply to author
Forward
0 new messages