Timber: Using theme.link in parent/child-theme on multisites-setup

Created on 11 Nov 2015  路  6Comments  路  Source: timber/timber

Please see: https://github.com/jarednova/timber/wiki/Cheatsheet

Specifically:

get_template_directory_uri() => {{ theme.link }} (Parent Themes)
get_template_directory_uri() => {{ theme.parent.link }} (Child Themes)
get_stylesheet_directory_uri() => {{ theme.link }}

If I use theme.link Timber will use get_template_directory_uri() if we're in the parent theme and get_stylesheet_directory_uri() if we're in the child theme.

However, if I want to inherit a complete template to my child, the child won't find whatever asset I'm looking for since it resides in the parent theme and we get the uri for the child theme.

Now if I use theme.parent.link instead my asset will load correctly from my child but the parent itself won't output anything.

This means that I have to overwrite the template file in my child theme and use _theme.parent.link_ there and _theme.link_ in my parent theme. Alternatively I have to copy my assets folder into my child OR create my uris with _get_template_directory_uri()_ in my backend with PHP/WP and pass them with my timbercontext.

Is this the intended behaviour? Shouldn't I be able to inherit this kind of functionality without having to overwrite the template file in my child theme or duplicate my assets?

2.0 has-patch

Most helpful comment

My feeling is that "A" makes a lot more sense. Since WP doesn't have any formal concept of grand-child themes, etc., {{ theme.parent }} always would refer to the parent theme, even if the current theme IS the parent theme. This feels reasonable and simple.

Sounds good to me, too 馃憤

As for loading asset files from the theme, I usually use {{ fn('get_theme_file_uri', 'relative/path/to/asset') }}. The get_theme_file_uri() and get_theme_file_path() functions were added in WordPress 4.7. They first look for the file in the child theme and if it isn鈥檛 found, they check the parent theme. This is useful because if you wanted to change a file, you could add it to the child theme.

We could add the following functions to access these functions a little more directly.

  • {{ theme.file_uri('relative/path/to/asset') }} which could use get_theme_file_uri().
  • {{ theme.file_path('relative/path/to/asset') }} which could use get_theme_file_path()

All 6 comments

For now a simple solution for my specific enquiry is to add my own variable to my global timbercontext that always gives me the uri to my parent theme root:

$context["parent_link"] = get_template_directory_uri();

@Dodsaren Is this still something you're actively working on or need addressed? Please respond in two weeks or we'll close this thread

This appears to still be broken in Timber 1.15. Seems to clearly be a bug in Timber itself and the workarounds @Dodsaren suggested aren't going to be practical for all users.

@Rocketpilot I'd love your insight to make sure we can solve this issue correctly. I realize it's an old issue, but it seems like a live one and meaningful to solve for the future.

The potential solve would be for {{ theme.parent }} to always return the parent theme (even if that's the SELF). The issue I see with this is any existing logic that tests for a parent theme would break. Example:

{{ theme.parent ? theme.parent.link : theme.link }}

This feels like an edge case though. I'm left with two potential solves:

A) In 2.x, break backwards compatibility (which I think is pretty edge-casey anyway) to make {{ theme.parent }} always give you a theme, even if it's the same theme as the one that's active
B) Introduce a new property/method to return the true "parent" ({{ theme.root }}? {{ theme.parent_theme}}? )

My feeling is that "A" makes a lot more sense. Since WP doesn't have any formal concept of grand-child themes, etc., {{ theme.parent }} always would refer to the parent theme, even if the current theme IS the parent theme. This feels reasonable and simple.

Would love insights/votes or counter proposals from anyone before I move forward with this.

Hi Jared. I agree that A seems like the cleanest approach. We have a master theme on our corporate multi-site and clients are beginning to ask for a lot of customisation, so this would be very welcome.

My feeling is that "A" makes a lot more sense. Since WP doesn't have any formal concept of grand-child themes, etc., {{ theme.parent }} always would refer to the parent theme, even if the current theme IS the parent theme. This feels reasonable and simple.

Sounds good to me, too 馃憤

As for loading asset files from the theme, I usually use {{ fn('get_theme_file_uri', 'relative/path/to/asset') }}. The get_theme_file_uri() and get_theme_file_path() functions were added in WordPress 4.7. They first look for the file in the child theme and if it isn鈥檛 found, they check the parent theme. This is useful because if you wanted to change a file, you could add it to the child theme.

We could add the following functions to access these functions a little more directly.

  • {{ theme.file_uri('relative/path/to/asset') }} which could use get_theme_file_uri().
  • {{ theme.file_path('relative/path/to/asset') }} which could use get_theme_file_path()
Was this page helpful?
0 / 5 - 0 ratings

Related issues

lmartins picture lmartins  路  4Comments

sgu1 picture sgu1  路  3Comments

jtschulik picture jtschulik  路  3Comments

garrettboatman picture garrettboatman  路  5Comments

KendallWhitman picture KendallWhitman  路  3Comments