0

I've decided to just test an idea for custom meta tags on every page without using Low Variable and try a native approach. The idea is to use an embed template with embed variables to check if a custom field is entered and if not use the global site meta description.

The embed template is:

{exp:channel:entries channel="{embed:channel_name}" dynamic="yes" disable="members|categories|pagination"}
    {if embed:meta_tag}
         {{embed:meta_tag}}
            {if no_results}
            blah
            {/if}

    {if:else}
          Nothing entered
    {/if}
    {if embed:meta_tag != ""}
         {{embed:meta_tag}}
            {if no_results}
            blah
            {/if}
    {/if}
    {if embed:meta_tag == ""}
          {gv_meta_desc}
    {/if}
 {/exp:channel:entries}

I then pass the embed template into my templates as

 {embed="embeds/template_file" channel_name="channel_name" meta_tag="meta_description"}

If the meta_description is entered, content will show. If not nothing will show. I cannot see an error or anything that would cause this to occur.

1

Your code works ok in EE 2.9 but versions below this, this should work:

{exp:channel:entries channel="{embed:channel}" dynamic="yes"}
   {if "{{embed:meta_tag}}" }
       {{embed:meta_tag}}
            {if no_results}
            blah
            {/if}
   {if:else}
       Nothing entered
   {/if}
   {if "{{embed:meta_tag}}" != ""}
       {{embed:meta_tag}}
       {if no_results}
               blah
        {/if}
   {/if}
   {if "{{embed:meta_tag}}" == ""}
       {gv_meta_desc}
   {/if}
{/exp:channel:entries}

In expressionengine 2.9, they rewrote the conditional parser, so in order to achieve this kind of effect in older versions, you need to double wrap all your embed var tags for conditionals.

Your first conditional was being rendered like:

   {if } {!-- blank if! --}
       {{embed:meta_tag}}
            {if no_results}
            blah
            {/if}
   {if:else}
       Nothing entered
   {/if}

You were passing in a blank variable, which I think was breaking the template. (See first response in this thread)

  • Thanks Johnathan, that is very close and I'll mark it as the answer. What I ultimately had wrong was not wrapping the custom field in brackets when it was passed at the embed point. Also instead of double brackets it works as "{embed:meta_tag}" – thatgibbyguy Jul 25 '14 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.