Referencing functions by variable names?

3 views
Skip to first unread message

Hartmut Goebel

unread,
Jun 13, 2024, 9:19:03 AMJun 13
to nikola-discuss

Hi.

I have defined some variables svg_icon_home, svg_icon_blog, svg_icon_mastodon. These are for inlining the icons, which are rather small.

A template-based shortcode shall get the name of the icon to use as an argument and then call the respective function. Of course, I don't want to write a long if-elif-elif case-handling with hard-coded names. But I want to calculate the function name based on the parameter and then call the function.

Like this:

$($svg_icon_{$name}()}

How can I do this?

(I did not yet try this, since my template-based shortcode is not yet working, see my other post.)

Thanks in advance for any answer.

-- 
Regards
Hartmut Goebel

| Hartmut Goebel          | h.go...@crazy-compilers.com               |
| www.crazy-compilers.com | compilers which you thought are impossible |

Chris Warrick

unread,
Jun 13, 2024, 3:47:49 PMJun 13
to nikola-...@googlegroups.com
> --
> You received this message because you are subscribed to the Google Groups "nikola-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nikola-discus...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nikola-discuss/bd31864e-41bc-436b-8331-f91082113284%40crazy-compilers.com.

Hi,

variable variables tend to be a bad idea. Check Mako docs, there might
be a way to handle this — although you might be happier with defining
a dictionary (in GLOBAL_CONTEXT?).

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

Hartmut Goebel

unread,
Jun 14, 2024, 8:47:00 AMJun 14
to nikola-...@googlegroups.com
Am 13.06.24 um 21:47 schrieb Chris Warrick:
variable variables tend to be a bad idea. Check Mako docs, there might
be a way to handle this — although you might be happier with defining
a dictionary (in GLOBAL_CONTEXT?).

Mako has 

<%call expr="my_func()" />

Anyhow, this does not allow embedded expressions.

I'll probably will take a different approach and use CSS to include the icons.

Thanks anyway.

Reply all
Reply to author
Forward
0 new messages