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.

Using local template variables to keep DRY

June 17, 2011 8:32am

Subscribe [5]
  • #1 / Jun 17, 2011 8:32am

    Fritzinger

    35 posts

    I use the same URL in two different places in a template. The URL is actually calculated using entry_id and channel_short_name. What I’d like to do is this:

    {exp:channel:entries channel="channel1|channel2|channel3"}
         {assign_variable:post_url="/index.php/pages/{channel_short_name}_detail/{entry_id}"}
         <a href="http://{post_url}">{title}</a>
         <!-- other uses of {post_url} later -->
    {/exp:channel:entries}

    From what I’ve heard, assign_variable won’t work inside the channel entries tag. I don’t think PHP will work either, because I need to evaluate the PHP expression that sets the variable after the entry variables have been evaluated, but before the {post_url} variable is evaluated. Do I have that right?

    Is this EE’s way of telling me I’m doing it wrong? The above looks so much cleaner than when I repeat the URL everywhere, but maybe there’s a better way.

    Fritz

  • #2 / Jun 17, 2011 5:19pm

    Sue Crocker

    26054 posts

    Hi, Fritz.

    Can you give me an idea what you’re url looks like? Is the entry_id in a segment? No, you can’t use assign_variable inside an exp:channel:entries loop.

  • #3 / Jun 18, 2011 8:14am

    Fritzinger

    35 posts

    I want use the channel short_name to find the template, and the entry_id as segment_3. Is there a way of doing this?

    So the URL would look like index.php/group/channelname_detail/entryid

  • #4 / Jun 19, 2011 12:08pm

    Greg Salt

    3988 posts

    Hi Fritzinger,

    This depends on what your other uses for the post_url are. Is this used as the parameter for another EE tag? You might find that that an addon like Stash will work for you.

    Cheers

    Greg

  • #5 / Jun 20, 2011 10:04am

    Fritzinger

    35 posts

    Ok I’ve figured out how to do what I wanted. I pass the channel short name and the entry id to a template that builds the URL and surrounding HTML. From a DRYness perspective it’s better because I get to use the template across multiple pages.

    Is there a major performance hit when you embed templates inside a tag that loops (channel:entries)? The nested template doesn’t fetch anything from the database so I’m hoping not.

  • #6 / Jun 21, 2011 8:18am

    John Henry Donovan

    12339 posts

    Fritzinger,

    Is there a major performance hit when you embed templates inside a tag that loops (channel:entries)?

    It depends how many iterations of that loop you have as for each one you will be calling a new instance of the embedded template. How many times will it loop?

  • #7 / Jun 21, 2011 8:33am

    Fritzinger

    35 posts

    Eek: as many times as there are entries in my channel. At some stage I’ll turn on pagination, I assume that would cap it at n instances where page size = n.

  • #8 / Jun 22, 2011 10:45am

    Sue Crocker

    26054 posts

    Hi, Fritzinger. You might want to turn on template debugging and see how things are working with the embed and when you restrict the number of items in your loop.

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

ExpressionEngine News!

#eecms, #events, #releases