Answers.Body: <p>It looks like you can check for the <a href="https://craftcms.com/docs/matrix-fields#getting-the-total-number-of-blocks" rel="nofollow noreferrer">total number of blocks within a matrix block</a>. So if you check that the length is greater than 0, you could then apply the classes.</p>
  
  <pre><code>{% if entry.myMatrixField|length &gt; 0 %}
      &lt;div class="col-sm-4"&gt;
          // other code
      &lt;/div&gt;
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-23T10:10:08.477Z
Answers.Text: No problem :) Would also second @Nutmeg on having a play around with the happylager site to familiarise yourself with the basics
Answers.UserDisplayName: Laura Montgomery
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-22T11:35:56.410Z
Answers.LastActivityDate: 2016-12-22T11:35:56.410Z
Answers.UserDisplayName: Laura Montgomery
Answers.Body: <p>Why not use:</p>
  
  <pre><code>{% if block.sidebar|length %}
      &lt;div class="col-sm-8"&gt;
  {% else %}
      &lt;div class="col-sm-12"&gt;
  {% endif %}
  </code></pre>
  
  <p>This way the column will automatically be smaller IF the side bar exists.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-22T15:10:10.133Z
Answers.Text: I think there's an issue with using "block.sidebar" to designate the field as opposed to "entry.sidebar", because this didn't work, but thank you for the suggestion.
Answers.UserDisplayName: FirstAidChris
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-23T10:08:31.077Z
Answers.Text: I was 90% sure that you could omit the > 0 but didn't have a craft build up and running to quickly test. Good call.
Answers.UserDisplayName: Laura Montgomery
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-23T20:27:51.287Z
Answers.Text: You can do this inline to avoid the repetition, so class="col-sm-{{block.sidebar|length ? '8' : '12'}}"
Answers.UserDisplayName: aw04
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-24T11:47:02.103Z
Answers.Text: Great tip, I'm using bootstrap myself, so this comes in handy :-)
Answers.UserDisplayName: noregt
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-22T12:34:17.733Z
Answers.LastActivityDate: 2016-12-22T12:34:17.733Z
Answers.UserDisplayName: noregt
Body: <p>I've got a project where I'm trying to set up a basic page layout – header, body, footer. But I also want the option for a user to add an image or bit of content (let's call it a "sidebar") that splits the body section into two columns – one for body copy on the left and one for the new content on the right.</p>
  
  <p>For this, I'm using a Matrix field (for the user to create sidebar content on the fly) and Bootstrap (for the grid). When sidebar content is created for the entry, I'm hoping to be able to add a "col-sm-8" class to the body wrapper, as well as add a div with the "col-sm-4" class for the sidebar column for it to reside in.</p>
  
  <p>Now, I have experience with creating static layouts in Bootstrap, but I'm woefully inexperienced at dealing with anything related to coding logic that isn't an if/else statement, and I can't seem to work out the code for Matrix in Twig.</p>
  
  <p>I also need to be able to add different kinds of content (or blocks) in the sidebar, so the logic should also need to check for whether one or more kinds of blocks are populating, but still apply the same effect.</p>
  
  <p>Can anyone provide an example of how to do this?</p>
  
  <p><a href="https://i.sstatic.net/udvTw.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/udvTw.jpg" alt="Logic Example"></a></p>
  
  <p><em>edit:</em> BTW, I <em>have</em> read the <a href="https://craftcms.com/docs/matrix-fields#templating" rel="nofollow noreferrer">documentation on templating Matrix fields</a>. I just don't know how to check for multiple block types in order to print a class name &amp; create a new column.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-12-21T23:35:03.667Z
Comments.Text: Although an indirect answer I would suggest you download and setup the [happylager](https://github.com/pixelandtonic/HappyLager) site from Pixel & Tonic. The Services section is setup as a dynamic Body Builder and it's based on Bootstrap. You could use this as a template and get yourself familiar with the Matrix fields....
Comments.UserDisplayName: Nutmeg
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-12-22T13:39:13.700Z
Comments.Text: Thank you! I was intimidated by digging through all of that, but now that I know which section to hone in on, I'll give it a shot.
Comments.UserDisplayName: FirstAidChris
Tags: <matrix><bootstrap-craft>
Title: Basic Bootstrap + Matrix Implementation
ThreadText: # Basic Bootstrap + Matrix Implementation
  
  (Asked by: username_0 on 2016-12-21)
  
  I've got a project where I'm trying to set up a basic page layout – header, body, footer. But I also want the option for a user to add an image or bit of content (let's call it a "sidebar") that splits the body section into two columns – one for body copy on the left and one for the new content on the right.
  
  For this, I'm using a Matrix field (for the user to create sidebar content on the fly) and Bootstrap (for the grid). When sidebar content is created for the entry, I'm hoping to be able to add a "col-sm-8" class to the body wrapper, as well as add a div with the "col-sm-4" class for the sidebar column for it to reside in.
  
  Now, I have experience with creating static layouts in Bootstrap, but I'm woefully inexperienced at dealing with anything related to coding logic that isn't an if/else statement, and I can't seem to work out the code for Matrix in Twig.
  
  I also need to be able to add different kinds of content (or blocks) in the sidebar, so the logic should also need to check for whether one or more kinds of blocks are populating, but still apply the same effect.
  
  Can anyone provide an example of how to do this?
  
  [![Logic Example](https://i.sstatic.net/udvTw.jpg)](https://i.sstatic.net/udvTw.jpg)
  
  *edit:* BTW, I *have* read the [documentation on templating Matrix fields](https://craftcms.com/docs/matrix-fields#templating). I just don't know how to check for multiple block types in order to print a class name & create a new column.
  
  **Tags:** matrix, bootstrap-craft
  
  > **Comments:**
  > **username_1**: Although an indirect answer I would suggest you download and setup the [happylager](https://github.com/pixelandtonic/HappyLager) site from Pixel & Tonic. The Services section is setup as a dynamic Body Builder and it's based on Bootstrap. You could use this as a template and get yourself familiar with the Matrix fields....  
  > **username_0**: Thank you! I was intimidated by digging through all of that, but now that I know which section to hone in on, I'll give it a shot.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2016-12-22. Score: 2 | Accepted answer
  
  It looks like you can check for the [total number of blocks within a matrix block](https://craftcms.com/docs/matrix-fields#getting-the-total-number-of-blocks). So if you check that the length is greater than 0, you could then apply the classes.
  
  ```
  {% if entry.myMatrixField|length > 0 %}
      <div class="col-sm-4">
          // other code
      </div>
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_2**: No problem :) Would also second @username_1 on having a play around with the happylager site to familiarise yourself with the basics  
  
  ---
  
  ### Answer by username_3 on 2016-12-22. Score: 3
  
  Why not use:
  
  ```
  {% if block.sidebar|length %}
      <div class="col-sm-8">
  {% else %}
      <div class="col-sm-12">
  {% endif %}
  
  ```
  
  This way the column will automatically be smaller IF the side bar exists.
  
  > **Comments:**
  > **username_0**: I think there's an issue with using "block.sidebar" to designate the field as opposed to "entry.sidebar", because this didn't work, but thank you for the suggestion.  
  > **username_2**: I was 90% sure that you could omit the > 0 but didn't have a craft build up and running to quickly test. Good call.  
  > **username_4**: You can do this inline to avoid the repetition, so class="col-sm-{{block.sidebar|length ? '8' : '12'}}"  
  > **username_3**: Great tip, I'm using bootstrap myself, so this comes in handy :-)  
  
  ---
  
  