简体   繁体   English

Symfony2 Twig Asset Base_Url是不可预测的?

[英]Symfony2 Twig Asset Base_Url is being Unpredictable?

I'm using a Twig loop to generate <img src=""> for multiple images. 我正在使用Twig循环为多个图像生成<img src=""> My eventual use of this code is to take advantage of a CDN with base_url in the Framework configuration. 我最终使用此代码是为了利用Framework配置中带有base_url的CDN。

Below is the Twig code : 下面是Twig代码:

{% for key, mobj in productmedia.getImages %}
    <img src="{{ asset(path('cropImage', {'filename': mobj.getFilename }),'cdn') }}" />
{% endfor %}

{% for key, mobj in productmedia.getImages %}
  <img src="{{ asset(path('cropImageSm', {'filename': mobj.getFilename }),'cdn') }}" />
{% endfor %}

I would expect, at the very least for there to be some consistency in the output.. but it seems SF2 will sometimes use one base_url and sometimes not use one. 我希望,至少在输出中有一些一致性。但似乎SF2有时会使用一个base_url,有时候不会使用一个。 Why would this be? 为什么会这样?

Below is the output. 以下是输出。

<img src="http://cdn.cloudfront.net/cri/1/matt-skydiving.SH340_SW340.jpg" />
<img src="/cri/1/Swimming.SH340_SW340.jpg" />
<img src="http://cdn.cloudfront.net/cri/1/successman2.SH340_SW340.jpg" />

<img src="/cri/1/matt-skydiving.SH40_SW40.jpg" />
<img src="http://cdn.cloudfront.net/cri/1/Swimming.SH40_SW40.jpg" />
<img src="http://cdn.cloudfront.net/cri/1/successman2.SH40_SW40.jpg" />

As you can see in the first loop.. some inherit the base_url.. and some don't.. I've cleared all cache.. done all the general debugging steps.. It doesn't make sense why this isn't consistent. 正如你在第一个循环中看到的那样..有些继承了base_url ..而有些则没有..我已经清除了所有缓存..完成了所有常规调试步骤..这没有意义为什么这不是是一致的。

You can switch to the url twig function, it's the same as path but this one will always return an absolute url: 你可以切换到url twig函数,它与path相同,但是这个函数总会返回一个绝对url:

url('cropImageSm', {'filename': mobj.getFilename })

Further Reference: 进一步参考:

Asset() will use the host supplied in the config.yml Asset()将使用config.yml中提供的主机

framework:
    templating:
        packages:
            {althostname}:
                base_urls:  { http: ["http://www.domain2.com"], ssl: "/"] }

So {{ asset(path('route1', {'page': 1 }),'althostname') }} would output.. http://www.domain2.com/route1/page/1 所以{{ asset(path('route1', {'page': 1 }),'althostname') }}会输出.. http://www.domain2.com/route1/page/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM