Import twig function not working anymore

{% import 'site/macros' as macros %}

and there is a partial site/macros.htm in the partials folder.

the site is running with site definition on the latest OC v3.

It used to work as I am resuming an old project.

Since then, the only I changed was to install Herd along the way to manage my php versions.
Could that be the culprit here ?

thanks

Just tested this on v4 and {% import 'site/macros' as macros %} with a partials/site/macros.htm works fine, so the feature itself hasn’t been removed or changed.

A few things to check on your end:

  1. Clear the Twig cache. A PHP version change (which Herd will have done) can leave stale compiled templates that fail silently. Run php artisan cache:clear and delete the contents of storage/cms/twig/.
  2. Check storage/logs/laravel.log for the actual error. “Not working” could be the import failing to find the partial, or the macro itself throwing on a stricter PHP version. The log will tell you which.
  3. Confirm the active theme. If you’re using site definitions, make sure the site is pointing at the theme that actually contains partials/site/macros.htm.

My guess is #1, Herd jumped you to a newer PHP and the old compiled cache is poisoned. Let us know what the log says if clearing doesn’t fix it.

thanks @daftspunk for coming back to me.

I am using OC v3.

  1. yes already tried, cleared cache. config and view but it does not fix the exception
  2. in the /logs/system.log i see the exception
[2026-05-24 11:34:07] local.ERROR: InvalidArgumentException: View [site/macros] not found. in /Users/christophevidal/Sites/oc-christophevidal/vendor/laravel/framework/src/Illuminate/View/FileViewFinder.php:137
Stack trace:
#0 /Users/christophevidal/Sites/oc-christophevidal/vendor/laravel/framework/src/Illuminate/View/FileViewFinder.php(79): Illuminate\View\FileViewFinder->findInPaths('site/macros', Array)
  1. yes, the correct theme is applied to the site browsed.
    but it could be the problem, maybe twig is thinking to be in the main default site when i’m in a sub site.

yes, if I changed the .env file ACTIVE_THEME value with the “sub theme”, then it works fine again.
But that is not the solution indeed

Hmm ok, this sounds like a bug.

Can you check, does it work when you render it with {% partial %}?

yes indeed, this {% partial 'site/macros' %} will work (no exception) as replacement of {% import 'site/macros' as macros %}

Ah, yes, I see it. The {% import %} path is not site aware, it uses the default data source (the theme found in config).

A patch has been added in v4.2.23 to make the native Twig functions site aware.

In the Twig Loader (modules\cms\twig\Loader.php) this new method does it:

protected function findFallbackObject($name)
{
    if (strpos($name, '::') !== false) {
        return false;
    }

    if (array_key_exists($name, $this->fallbackCache)) {
        return $this->fallbackCache[$name];
    }

    try {
        if (($site = Site::getSiteFromContext()) && $site->theme) {
            return $this->fallbackCache[$name] = CmsPartial::inTheme($site->theme)->find($name);
        }
    }
    catch (Exception $ex) {
    }

    try {
        return $this->fallbackCache[$name] = CmsPartial::find($name);
    }
    catch (Exception $ex) {
        return false;
    }
}
2 Likes

thanks @daftspunk

how can I insert this method in my OC v3 then ?

Yes, you could try, let me know if it works and we can patch 3.x with this fix as well.

Hi @daftspunk

OK yes this code is working for OC v3 with adding the use Site; within the declaration.

thanks

1 Like

Thanks! It has been added to the next 3.x patch