0

In Craft 4.7.2.1 Pro, I try to create dynamic path location for my assets for my field "image". The field image is in MySection, which is shared with multiple sites.

I would like to organize images in separate folders for each site. I tried something like :

{entry.site.slug}

but doesn't work

1 Answer 1

2

There are two issues with your code:

  1. You aren't able to reference entry in the field. This section in the docs describes the rules around using Twig in the Entry title field, and the same rules apply in this case as well. The long and short of it is you can either use the shortcut syntax and forego referencing the entry at all: {site.slug}, or you can use the normal syntax, in which case you'll want to use the object variable to refer to the current entry: {{ object.site.slug }}.

  2. Secondly, Sites don't have slugs (you can see all possible variables here). My suggestion would be to use the Site's handle instead, and maybe throw the kebab filter on it so it looks better (/path/to/images/my-lovely-site instead of /path/to/images/myLovelySite):

    {site.handle|kebab}
    
    {# or {{ object.site.handle|kebab }} #}
    
3
  • 2
    I pushed a page for the 5.x docs on object templates, but haven't back-ported it to the 4.x docset just yet! Commented Feb 14 at 21:18
  • Yes it works ! thank you ! Commented Feb 15 at 10:45
  • It works but.... if you have a multilingual site, you'll have to upload two times the assets. So I needed to slice the last letter of my site handle (mySiteFR, mySiteEN...) which correponds to the letter of the language (FR, EN, DE...). Here the solution : {site.handle|kebab|substr(0,-2)} Commented Feb 22 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.