dougou8639 2013-11-13 08:41
浏览 11
已采纳

从javascript引用时,将图像置于资产中的位置

I am using assetic in Symfony 2. I have JavaScript and CSS loaded using assetic.

I need to reference some images in one of my JS files. I am new to assetic, and cannot find any documentation on this at all. The images currently reside in my bundle (/src/Vendor/BundleName/Resources/img/chair.jpg for example).

I see one major pitfall here: How would assetic have any idea what the image path is? As far as it's concerned it's just a string in a JS file. So, do I need to define that image path somewhere else or do I reference it in a different way within the JavaScript?

Or am I to not use assetic at all and just throw my images into the public directory (web by default)? I'm guessing not.

  • 写回答

1条回答 默认 最新

  • dqt83336 2013-11-13 10:19
    关注

    You should then use JmikolaJsAssetsHelperBundle.

    which allows you to expose the AssetsHelper service from Symfony2's templating component to JavaScript.

    It supports both relative and absolute asset URI(s).

    Example,

    Once configured, the bundle creates a AssetsHelper global variable within your javascript. It allows you then to call a getUrl() helper to build your asset path as follow,

    '<img src="' + AssetsHelper.getUrl('/images/yourImage.png') + '">';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。