ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How best to loop through entries?

November 02, 2009 12:19am

Subscribe [1]
  • #1 / Nov 02, 2009 12:19am

    mnh

    11 posts

    Hi everyone. I’m getting familiar with ExpressionEngine and can’t find good direction on how to loop through exp:weblog:entries while keeping your code DRY.

    Scenario: (pretty common) I’m developing a site that has lots of “posts” on a majority of the templates (home/category/content). Each of these posts requires the same code: title, description, image, author, date. Rather than write the same exact code in 5 different templates, and possibly more than one time per template I would like to write the code in a new template and embed it.

    From what I’ve been able to find it seems that you can pass a variable using the “entry_id” method. This method also seems to be looked down upon because you’re making another call for the entry within your embedded template. I’m also reading that embeds themselves are a bad idea if you want to keep your site performing well (i.e. quick).

    I feel like I’m missing something glaringly obvious, I’m just looking to loop through some entries with minimal code sludge.

    Can anyone point me in the right direction?

  • #2 / Nov 02, 2009 4:00am

    Lisa Wess

    20502 posts

    Hi, mnh - welcome to the forums!

    Embeds are not in and of themselves bad, but like anything, you want to use them in moderation and make use of other features, like user defined global variables, when possible.

    The weblog entries tag is very powerful and can filter information based on the URL, so that may be all you need.  Have you read the importance of semantics?

  • #3 / Nov 02, 2009 2:33pm

    mnh

    11 posts

    Thanks for the warm welcome Lisa.

    That’s a great article series, I just read through it. It helped quite a bit and I definitely have a better idea of how the url structure affects templates in EE, thank you. While this answers many other (future) questions I’m still a little confused about how most EEers go about abstracting commonly used bits of code for items that may get displayed 50 times a page. Maybe I can explain a bit better.

    Essentially what I want is a common (abstracted) “post thumbnail” embed, it would contain a title, description, author, image, maybe a rating. On a site with thousands of articles, you may display 30-40 of these posts on a page. 10 featured (at the top), 20 for the category (main body), 20 more related (sidebar). All of these posts use the exact same markup and because of this, I would like to maintain that markup in only one file. Even if there were subtle design differences I could still keep the same markup and make visual changes with CSS. What I would like to be able to do, but I’m pretty sure EE doesn’t work this way, would be:

    {exp:weblog:entries weblog="default_site" limit="10"}
      {embed="embeds/post_entry" entry="{entry}"}
    {/exp:weblog:entries}

    From there I could just magically “utilize” the entire entry object without ever calling the url structure or heavy functions. This is close to how Rails works and it’s what I’m familiar with. I’m trying to find a solid alternative in EE so that I don’t have multiple places to maintain the same code site wide, even if it is simple code.

    The more I think about this I’m guessing that most EE devs are creating a few embeds that they can call on a variety of pages. Perhaps a “featured”, “post-list” or “related” embed (with these you could pass limits or restrictions), rather than looping through “post_entry” 50 times total from 3 different loops (“featured”,“posts”,“related”) within the template (like above). The “bigger embeds” method would still leave you with multiple places to maintain your markup if you need to change it but also gives you only 3 embed calls rather than 50 (unless you made embed calls from within those templates, giving you the same problem).

    I believe part of my roadblock is that the “render :partial” method in rails is fairly seamless performance wise.

    Is this a bit more clear? I still feel like I’m missing something. 😊 How are you guys keeping your code DRY while still avoiding 50+ embeds on a page?

  • #4 / Nov 02, 2009 2:38pm

    Lisa Wess

    20502 posts

    You can do that, but it adds much necessary overhead.

    Why do you need to pass the entry to the embed?  Could you include the embed without the surrounding weblog entries tag and still get the information that you need?  That’s how I would approach it.

    I think that people that come from a programming background or different CMS have the hardest time with this transition.  You need to try to forget what you know and come at ExpressionEngine fresh.

  • #5 / Nov 02, 2009 2:47pm

    mnh

    11 posts

    Why do you need to pass the entry to the embed?

    You don’t, but assuming I have similar content all over my site it would be nice to be able to abstract out that portion of code.

    Could you include the embed without the surrounding weblog entries tag and still get the information that you need?  That’s how I would approach it.

    This is starting to sound like the way to go. Chunk out code into much larger (self-contained) pieces, even if it adds slight duplication it saves in overhead.


    Thank you, I feel like I can move forward now 😊

  • #6 / Nov 02, 2009 2:50pm

    Lisa Wess

    20502 posts

    You can’t split up the inner part of the entries tag -  you can, however, pass the parameters.  So you could put your entire entries tag into an embed and embed it with the variables wherever you need to.

    Another option may be Reeposition.

    And no worries. =)

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases