Step 1: Count the triangles.
- The figure consists of several small triangles within larger triangles.
- There are 6 small triangles in the bottom layer, 3 triangles in the middle, and 2 triangles at the top of the pyramid.
- Additionally, there are 10 triangles formed by combining different segments, including the larger triangles and overlapping areas.
Thus, the total number of triangles is:
\[
6 + 3 + 2 + 10 = 21.
\]
Step 2: Count the squares.
- The squares are present in the middle portion of the figure, formed by intersections of lines.
- There are 7 squares in total.
Thus, the total number of squares is:
\[
\boxed{7}.
\]
Step 3: Final Answer.
The figure contains 21 triangles and 7 squares.
\[
\boxed{21\ \text{triangles},\ 7\ \text{squares}}
\]