0

I would like to use segment_1 in my address to refer to different locations. For example, my-site.com/haiti, my-site.com/anchorage or my-site.com/portland. But they would always be directed to the same template group and template ({embed="HeWo/main"}).

The only way I know how to do this is to create a separate template group for each location. Otherwise I just receive an error message. I was looking at Template Groups and imagine it might be possible to use that to accomplish this but couldn't figure out how to do that.

Thanks for your help!

1

You have lots of options, which is best will depend on what your site design / data looks like. The following suggestions are not in a recommended order, just trying to separate out your options:

  • You can access the content of the first nine segments directly within EE templates via URL Segment Variables - so in your case the content of segment_1 is available as {segment_1} and you can use this to direct conditional statements in your template: for example
   {if segment_1 == 'haiti'}
    << template code for haiti >>
   {if:elseif segment_1 == 'portland'}
    << template code for portland >> 
   {if:else}
   << Ooops! Segment not found >>
   {/if}
  • You could set up template groups for each of your cities - put the template you want to load for a city in the template called index.html that is saved within the template group with the correct name. Then when EE sees a URL https://your.domain/city it will try to process the template in group city.
  • You could use the Template Routes system in EE to route all URLs with city names (of your choice) to a particular template. Though I would be cautious of this approach - EE's Template Route system is not easy to work with, there are some quirks in how it is implemented, and you can waste a lot of time chasing around to get even simple things to work... But in principle, if the template you want to use for haiti and portland is called coastal.html in template group places you could add a rule like the example given for a route for the places/coastal template to ensure that portland and haiti are routed to it...
/{portland|haiti}
  • If the content relating to a city primarily sits within a single channel entry you could enable the built-in add-on "Pages" and then define an explicit path for each channel-entry you plan to use using the format you describe.

So lots of options. If it was my choice, I would go for the first option (using conditionals to control content / flow).

NB: If you are going to use the segment_x conditional approach (option 1 above) and want to apply it to the root (i.e. https://your.domain/) this will only work if you also set the site's 404 page to point to the default template. Then when EE gets a combination of segments it can't recognise / process it puts processing onto your default page, and you can access the segments and do something with them. Without the 404 you just get a not very helpful EE error page.

HTH

PS - always helpful to include details of your EE system / version in your questions as sometimes this information is helpful in giving more complete / accurate answers... ;)

Follow up to answer (from comments)

From what you have said in your comments you've got the following in your template...

{if segment_1 == ""}
    {embed="HeWo/main"} 
{/if} 
{if segment_1 == 'haiti'}
    {embed="HeWo/main"} 
{if:elseif segment_1 == 'portland'} 
    {embed="HeWo/main"} 
{if:else}
    << Ooops! Segment not found >> 
{/if} 

Not sure if this is your actual code, or just a test template. As you have it written currently it appears that if the URL has no segment (e.g. https://your.domain/) or has the first segment haiti or portland (e.g. https://your.domain/portland) you include in the template the content generated by the embed HeWo/main.

You could achieve the same result with much more concise code...

{if segment_1 == "" || segment_1 == "portland" || segment_1 == "haiti"}
    {embed="HeWo/main"}
{if:else}
    << Ooops! Segment not found >>
{/if}

Also not clear why you are using an embed and not passing anything to it - the value of embeds is that they allow you to nest calls to things like {exp:channel:entries} and so usually this is linked to some value in the calling template which is passed to the embed. If all you are doing is using the embed as a convienient place for some common template code, you can get better results putting the code into a template partial as the embed system in EE introduces quite a lot of processing overhead that is not present if you use a template partial. Or it might be that you could just put your code into the template and skip the embed completely. It might be that you are planning for your embed to refer to the segment_1 segment variable (and so do not need to pass a variable to the embed); an unusual way of using embeds.

| improve this answer | |
  • 1
    Great answer ! Ressource Router is a great tools too. devot-ee.com/add-ons/resource-router. True, I tried to answers, but i confess I didn't understood very well his question. – Stéphane May 17 at 21:20
  • HTH, Thanks for the list of possibilities! I really would like to use the first one as it is the simplest, but it simply isn’t working for me. I have done exactly as you suggested, putting the conditional in the site/index template, and get this error message: “Error: The page you requested was not found.” The only thing I have found that allows a segment_1 variable to work is when that is already created as a template group. Since you suggested this though I realize that this is supposed to work, so I’m guessing there is some configuration problem in my setup somewhere? I can't find that. – Geoff May 17 at 23:20
  • I know I can create template groups for the cities and that will work. The problem is the I have an indefinite and growing number of cities, at least 50 but possibly many more. In terms of the other possibilities you mentioned, I’ll look deeper into those if I can’t resolve the first option. By the way, I’m using version 5.3.0. Geoff – Geoff May 17 at 23:20
  • Hi Geoff. Maybe if you post some actual code from the template you have created that is not working it will be possible to spot what is going on. EE also has a pretty good built-in debug system - if you turn it on (docs.expressionengine.com/latest/control-panel/settings/…) when logged in as an admin you can see more about what is happening in your templates, and among other things shows you what it is evaluating in conditionals - makes it easier to spot where there are problems. Also HTH == "Hope That Helps" ;) – JCOGS Design May 18 at 7:57
  • Thanks! Here is the code in the site/index file. I have turned on debugging visible to everyone. {if segment_1 == ""} {embed="HeWo/main"} {/if} {if segment_1 == 'haiti'} {embed="HeWo/main"} {if:elseif segment_1 == 'portland'} {embed="HeWo/main"} {if:else} << Ooops! Segment not found >> {/if} – Geoff May 19 at 1:18

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.