3

Is there a variable I can use to determine that name of the template that is currently being accessed/displayed? For example, if I have a /pages/home/ template, inside the home template I'd like to know it's called "home". Possible?

I plan to use this variable for the following:

  • assigning an id to the <body> tag
  • determining if the navigation is "active" or not (it's active if user is on the page where the navigation points to)
4

I typically accomplish this based on URL, using segment variables. For instance:

<body id="{segment_1}">

You can also use if statements to add classes to your navigation, then target those classes in CSS:

<a href="" class='{if segment_1 == "foo"}foo{/if}'>

Additionally, if you're using embeds and want to carry info about the template in question into the embedded template, you can use embed variables:

{embed="template" section="foo" title="bar"}

In the above example, section and title are custom defined, you can call these variables whatever you want, then access them in the embedded template like this:

{embed:section}
| improve this answer | |
  • Update your answer by adding the two add-ons mentioned as well and you'll have the most complete answer so far Pat. – Natetronn Nov 28 '12 at 23:41
1

Would NSM Body Class work for you? http://ee-garage.com/nsm-body-class

It "generates a class='' attribute value from embed template parameters allowing greater CSS styling of templates."

| improve this answer | |
1

Based on your example, use {segment_2} to identify the template in use.

If using the template for different pages, or using Structure, I may also add an html comment to identify the template - while in dev.

| improve this answer | |
1

In addition to {segment_1}, {segment_2} ... {segment_9}, there is also a {last_segment} variable in EE 2. There are a lot of ways that these can be used, but a few potential uses would be:

{if segment_1 = last_segment}{redirect="/"}{/if}

<a href="{segment_1}/{segment_2}/step_2">Move along to Step 2</a>

{if last_segment = 'home'}
    ...
{/if}

Sitenote: As you become more experienced with ExpressionEngine, you may learn that some {if} blocks are parsed early, and others are parsed later. Both of the blocks in the sample above are parsed early.

| improve this answer | |
  • Hi & welcome to the site. Could you perhaps expand your answer a bit with why you might want to use {last_segment} for the original poster's use case? This would help to make it of more use to the community. – Tom Davies Nov 29 '12 at 0:05
0

Would {segment_2} variable do the job you are asking? You could also do simple check

{if segment_2 == "home"}Do happy dance{/if}
| improve this answer | |
0

For the two scenarios you posted you could do:

<body {if segment_1}id="{segment_1}"{/if}>

So if you are on yoursite/about then you'd see id="about". If you are on yoursite.com/contact then you'd see id="contact". If you were on the home page, there would be no id.

For the second, you could do the same with the {segment_#} parameter:

<li {if segment_1 == "about"}class="active"{/if}><a href="/about"></a></li>

There are more advanced/dynamic ways of handling the active class as well.

| improve this answer | |
0

There is an add-on that might help for setting the body class:

http://devot-ee.com/add-ons/clasee-body

Using a url segment variables and conditionals is another option for setting the body class and the active stage though, I see other have posted that option right as I was typing so, I won't add the code again.

| improve this answer | |
0

If I were to look at this question in a more general sense, it's possible that you are in fact accessing / viewing the output of several templates because of embeds.

One of the best ways to determine which templates are being used in a page view is to enable template debugging and look through the list.

http://ellislab.com/expressionengine/user-guide/cp/admin/output_and_debugging_preferences.html

In that list you will see which templates are being loaded (the main template as well as the embeds) and which order they are being loaded in. You will also see a list of tags which are being loaded.

However, in your example, you are really looking at segment variables, which aren't always coupled directly to specific templates. There are lots of methods which may make URL segments unreliable indicators of which templates are being used. For example, the pages module allows for custom URL segments.

In your case, I would typically do something like what Pat Brumfield mentioned. I would create an embedded template for the navigation and then from the main template (the one referencing the embed) I would use a embed variable to indicate which section the site is in.

{embed="includes/sidebar" section="home"}

And then, in the sidebar I would have a conditional which would look something like this.

<li{if embed:section == "home"} class="active"{/if}>Nav Item</li>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.