无法从数组 TWIG 打印值



我有一个来自Instagram API的数据数组。我已经知道如何获得低分辨率值来打印页面上的图像。但是由于某些原因,我无法获得标题值。

下面你可以看到我的TWIG代码。图片链接很好,但为什么我不能得到文本?我从symfony得到的错误是:

无法在default/geo .html中访问null变量上的属性("text")。第23行

{% for value in images_array %}
<article class="col-md-3">
  <img src="{{ value.images.low_resolution.url }}" alt="" class="thumbnail img-responsive" />                 
  <p>{{ value.caption.text }}</p>
</article>
{% endfor %}

下面是从Instagram引入的数据的第一个索引。

Array
(
    [0] => Array
        (
            [attribution] => 
            [tags] => Array
                (
                    [0] => style
                    [1] => fashion
                    [2] => millinery
                    [3] => simonandmary
                    [4] => cvp
                    [5] => lfw
                    [6] => doghousestudios
                    [7] => lfw15
                    [8] => londonfashionweek
                    [9] => charleyvanpurpz
                    [10] => hat
                )
            [location] => Array
                (
                    [latitude] => 51.5122299
                    [name] => BFC Show Space, Brewer Street Car Park, London
                    [longitude] => -0.13417
                    [id] => 1014693484
                )
            [comments] => Array
                (
                    [count] => 0
                    [data] => Array
                        (
                        )
                )
            [filter] => Normal
            [created_time] => 1443003740
            [link] => https://instagram.com/p/7-DcwVrY3J/
            [likes] => Array
                (
                    [count] => 0
                    [data] => Array
                        (
                        )
                )
            [images] => Array
                (
                    [low_resolution] => Array
                        (
                            [url] => https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/s320x320/e35/11881754_1044165445613821_1998401000_n.jpg
                            [width] => 320
                            [height] => 320
                        )
                    [thumbnail] => Array
                        (
                            [url] => https://scontent.cdninstagram.com/hphotos-xpf1/t51.2885-15/s150x150/e35/c6.0.1068.1068/10809626_1642110999378208_443887411_n.jpg
                            [width] => 150
                            [height] => 150
                        )
                    [standard_resolution] => Array
                        (
                            [url] => https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/s640x640/sh0.08/e35/11881754_1044165445613821_1998401000_n.jpg
                            [width] => 640
                            [height] => 640
                        )
                )
            [users_in_photo] => Array
                (
                    [0] => Array
                        (
                            [position] => Array
                                (
                                    [y] => 0.98366013
                                    [x] => 0.078125
                                )
                            [user] => Array
                                (
                                    [username] => brad_hobbs
                                    [profile_picture] => https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-19/s150x150/11849014_1659972384225731_1340681672_a.jpg
                                    [id] => 1376238
                                    [full_name] => Brad Hobbs
                                )
                        )
                    [1] => Array
                        (
                            [position] => Array
                                (
                                    [y] => 0.98366013
                                    [x] => 0.934640519
                                )
                            [user] => Array
                                (
                                    [username] => doghousestudios
                                    [profile_picture] => https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-19/10731472_1576813469216190_1504144557_a.jpg
                                    [id] => 1544718516
                                    [full_name] => Doghouse Studios
                                )
                        )
                    [2] => Array
                        (
                            [position] => Array
                                (
                                    [y] => 0.128686798
                                    [x] => 0.6046875
                                )
                            [user] => Array
                                (
                                    [username] => simonandmary
                                    [profile_picture] => https://scontent.cdninstagram.com/hphotos-xfp1/t51.2885-19/10954789_372599292911615_195833317_a.jpg
                                    [id] => 309223601
                                    [full_name] => Simon and Mary
                                )
                        )
                )
            [caption] => Array
                (
                    [created_time] => 1443003740
                    [text] => In My Fav Felt Dip Dyed Monza By @simonandmary My Fav Hat Brand Right About Now Thanks To @doghousestudios For The Introduction ☺️🙏🏾. 📷: @brad_hobbs 👌🏾. Day4 #LFW #londonfashionweek #LFW15 #Fashion #Style #CVP #CharleyVanPurpz #Hat #SimonandMary #doghousestudios #millinery
                    [from] => Array
                        (
                            [username] => charleyvanpurpz
                            [profile_picture] => https://igcdn-photos-h-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-19/s150x150/11311163_951968284859623_331958112_a.jpg
                            [id] => 54462914
                            [full_name] => Charley Purpz
                        )
                    [id] => 1080316135090261762
                )
            [type] => image
            [id] => 1080316130803682761_54462914
            [user] => Array
                (
                    [username] => charleyvanpurpz
                    [profile_picture] => https://igcdn-photos-h-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-19/s150x150/11311163_951968284859623_331958112_a.jpg
                    [id] => 54462914
                    [full_name] => Charley Purpz
                )
        )

我猜不是每个迭代中的value都有caption。如果是,请尝试:

{% if value.caption is defined %}
  <p>{{ value.caption.text }}</p>
{% endif %}

或必要时(如caption可以不定义text):

{% if value.caption is defined and value.caption.text is defined %}
  <p>{{ value.caption.text }}</p>
{% endif %}

参考:http://twig.sensiolabs.org/doc/tests/defined.html

相关内容

  • 没有找到相关文章

最新更新