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.

Is it possible to change code inside an EE tag based on the current channel entry?

April 07, 2012 1:09pm

Subscribe [5]
  • #1 / Apr 07, 2012 1:09pm

    martinj

    32 posts

    Hi all

    I’m working on an EE site using Michael Boyink’s excellent Building an ExpressionEngine 2 Site as a guide. I have several template groups set up which follow the section structure of the site (technical-information, water-supplies, news, etc). There’s some template code repetition between the technical-information and water-supplies index template; I’m thinking that for the navigation menu within each section it could be possible to use a more DRY approach. The current template code for the technical-information index template is:

    {embed="embeds/html_header"}
    
    {embed="embeds/page_header"}
     
     
     <div class="main-menu-outer">
      <div class="main-menu-centered">
       <ul class="main-menu-basic main-menu-advanced">
              
              {exp:channel:entries channel="technical_information" disable="categories|member_data|pagination" dynamic="no" sort="asc" offset="1"}
              <li{if count == total_results} class="last"{/if}>
               <a href="http://{path=">{title}</a>
              </li>
              {/exp:channel:entries}
              
          </ul>
      </div>
     </div>
     
     <div id="main-content">
      {exp:channel:entries channel="technical_information" disable="categories|member_data|pagination" sort="asc" limit="1"}
       <h1>{standard_main_page_heading}</h1>...

    The menu code inside <div class=“main-menu-outer”> is repeated in the water-supplies index template but with {exp:channel:entries channel="water_supplies"

    This works fine but if there is a way of replacing "technical_information" with some code that automatically inserts the channel of the current entry being viewed, I could move the code into the {embed="embeds/page_header"} and have it in just one template file.

    I’ve not found any info on the possibility or otherwise of pulling the current entry channel into EE code dynamically. Is it possible or should I just stick with the current solution?

    Thanks in advance for your help

  • #2 / Apr 07, 2012 1:10pm

    martinj

    32 posts

    I made a mistake in the original anchor code which messed up when I posted, the corrected code I’ve written is:

    <a href="http://{path=technical-information}/{url_title}">{title}</a>
  • #3 / Apr 07, 2012 3:03pm

    ahmad saad

    364 posts

  • #4 / Apr 07, 2012 4:54pm

    PhilBrienesse

    187 posts

    channel_id is a variable that is used inside an exp:channel:entries call.

    Since you are using dynamic =  no it will not be relevant to the page you are on. However not using dynamic=no will only return the current page you are on in your nav list.

    If your site has a standard url structure of /template/url_title then you could just use a segment variable.

    {exp:channel:entries channel="{segment_1}" disable="categories|member_data|pagination" dynamic="no" sort="asc" offset="1"}
      <li{if count == total_results} class="last"{/if}>
        <a href="http://{site_url}{segment_1}/{url_title}">{title}</a>
      </li>
    {/exp:channel:entries}

     

  • #5 / Apr 07, 2012 5:31pm

    martinj

    32 posts

    That’s brilliant Phil. I’ve used segments but didn’t know they could be placed within {exp:channel:entries} - yes I am using the standard url structure so I’ll give it a try.

    I think I’m going to have to revise the template naming though. Currently I’m using underscores to separate words in the channel names but dashes for the templates. I think they’ll have to use the same separator - no problem to change.

    Thanks for your help.

  • #6 / Apr 09, 2012 12:18pm

    Kyle Cotter

    730 posts

    Glad Phil was able to assist you here, martinj.

    Is there anything else I can help you with?

    Thanks!

  • #7 / Apr 10, 2012 6:11am

    martinj

    32 posts

    Hi Kyle

    Phil’s solution works great providing channel names and template names both use underscores (or both use dashes) to separate words.

    As I’m still on the EE leaning curve I’m using underscores in channel names and dashes in template names - it helps me distinguish between the two in my code. I would love a solution which retains the different naming structures yet allows for the DRY coding. Otherwise I’ll just wait until I’m more familiar with EE tags.

  • #8 / Apr 11, 2012 10:33am

    Shane Eckert

    7174 posts

    Hey martinj,

    I am glad to see that Phil was able to help you out!

    If you need anything else, please just let us know by opening a new thread.

    Cheers,

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

ExpressionEngine News!

#eecms, #events, #releases