Chris
Chris

Reputation: 8099

How to reference a bundle-image from within a Bundle?

I have an image in my bundle at Bundle/Resources/public/images/abc.png.

I want to reference it from a template file. My first thought was <img src="{{ asset('images/abc.png') }}" /> which is obviously not working.

I don't want to have a folder like Bundle/Resources/public in the rendered HTML, so is there a way to reference a resource file in a nice way? I couldn't find anything about this in the documentation.

Upvotes: 3

Views: 2444

Answers (2)

B&#225;rtfai Tam&#225;s
B&#225;rtfai Tam&#225;s

Reputation: 2464

The assets:install command places (or symlinks) all bundle resources under the web/bundles directory. The AcmeDemoBundle/Resources/public/images/img.png then placed under web/bundles/acmedemo/images/img.png and you can use asset('bundles/acmedemo/images/img.png') in your template. This is the "official" way, most bundles use assets this way.

You can write your own asset installing command if you want, see vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php or any other solution which places assets under the web directory.

Upvotes: 5

F.P
F.P

Reputation: 17831

Just put the image in the folder you specify in the asset method.

| app/
| src/
| web/
`-| images/
  `-| abc.png
  | javascript/
  | css/

Upvotes: 0

Related Questions