1

I want to include asset with some script file in Twig extension. E.g. I want to declare twig function {{ init_project() }}, but when I writing in my twig function '<script type="text/javascript" src="{{ asset('bundles/mybundle/js/script.js') }}"></script>', it dont work, it return 404 error in debug panel of browser. So how I can do this?

2
  • well you can definetly do it without assetic src="/bundles/mybundle/js/script.js"> Commented Feb 26, 2015 at 8:29
  • @johnSmith, hm, in debuger i can see, that assetic add root of my project directory, like src="project/web/bundles/mybundle/js/script.js">, but I will try your way Commented Feb 26, 2015 at 8:39

1 Answer 1

1

Look at the 'asset' twig function :

You can find it in \Symfony\Bundle\TwigBundle\Extensions\AssetsExtension

public function getAssetUrl($path, $packageName = null, $absolute = false, $version = null)
    {
        $url = $this->container->get('templating.helper.assets')->getUrl($path, $packageName, $version);

        if (!$absolute) {
            return $url;
        }

        return $this->ensureUrlIsAbsolute($url);
    }

Instead of calling '{{asset}}' in your extension, just call the public function getAssetUrl()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.