I'm trying to send a meta-data image from an entry template to the main layout template. The image is in a matrix block on the entry. It comes out correctly when I call it on the entry page, but when I try to pass it to the layout it doesn't see it and reverts to the default image called in the layout. Any help is appreciated

_Layout template

<meta property="twitter:image:src" content={% if twitterpic is defined %}"{{ twitterpic }}"{%else%}"{{ siteImage.getUrl('twitterPic') }}" {%endif%}/>

_entry template

{#  social image  #}
{% for block in entry.projectImage%}
{% set image for block.projectImage.first() %}
{% set twitterpic =image.getUrl('twitterPic') %}
{% endfor %}
  • This seems like a duplicate of: craftcms.stackexchange.com/q/153/177
    – Victor In
    Jul 14 '14 at 15:48
  • 1
    Hi @Micah, and welcome to the Craft CMS Stack Exchange site! As Victor In pointed out, a similar question has already been asked and answered here. You might also find this question on passing meta tags useful.
    – Matt Stein
    Jul 14 '14 at 16:07
  • I'm in the same block, and all my other variables are passing fine. That's not the problem. The problem seems to be specific to the asset call Jul 14 '14 at 16:47
  • @MicahMorgan As the linked answer indicates, this is a scope issue. You're defining twitterpic within your foor loop, but you first need to define it outside the loop if you want it to be available elsewhere. You're also running .getUrl('twitterPic') twice. You should either pass the asset (image) or pass the URL reference (twitterpic).
    – Matt Stein
    Jul 14 '14 at 17:13
  • 1
    @MicahMorgan "I read it all, but it didn't sink in" explains most of the problems I have :)
    – Matt Stein
    Jul 14 '14 at 20:26

Browse other questions tagged or ask your own question.