If statement to check if content exists in Frontmatter

I’m creating a landing page builder and I’m including all content in frontmatter and having partials pull that local data in dynamically. But I would like to include some sort of check to see if that front matter data exists, and if it doesn’t, I’d like to tell the partial to not render a blank tag.

For instance. My frontmatter may look like this:

cta:
  headline: This is a call to action.
  subhead: We really think you're gonna love us.
  button_text: Sign Up

My partial call looks like this:

= partial(:'partials/landing_builder/_cta_form.slim', :locals => { :headline => current_page.data.cta.headline, :subhead => current_page.data.cta.subhead, :button_text => current_page.data.cta.button_text })

And the partial code is something like this:

section.landing_cta
  .row.align-center
    .small-11.columns
      h2 = headline
      p = subhead

Can I wrap the h2 and p tags in some sort of if statement that checks to see if that content exists in frontmatter?

Hiya

I use tons of partials on - http://codeblender.net/
Check out - https://github.com/iwarner/CodeBlender-Middleman

Now firstly for yours I would just likely pass in the whole

current_page or current_page.data

= partial(:'partials/landing_builder/_cta_form.slim', :locals => { data: current_page.data })

Make it easier for the partial to manipulate - you can then do sense checking in the partial

data = locals[:data]

- if ! locals[data.heading]

Maybe able to use .empty() .blank() etc also

Anyway hope this points u in the right direction.

You should also read up on HAML statements - or check out some of my partials for guidance on whats possible. Or use codeblender :slight_smile:

1 Like

Thanks. This is helpful, but I’m having some trouble implementing it. Getting NameErrors

Nevermind! I figured it out.

data: current_page.data needed to be :data => current_page.data for my slim templates.

ok, so I’m able to pass data into the partial this way and it’s much cleaner, for sure. I’m still not sure how to perform the check for data in the partial though.