<p>You have the full power of Twig in your template to render the entries Uri so you can conditionally render what you want. This could be your template in your section settings </p>

<pre class="lang-twig prettyprint-override"><code>{% if object.parent.field == true %}{{ object.parent.uri }}{% else %}{{object.parent.slug}}{% endif %}
</code></pre>

<p>There are no real limitations since you can do what you are used to in Twig in your frontend </p>

CC BY-SA 4.0
2019-03-06T22:25:35.363Z
Thanks for that. I feel like I'm close but not quite there. 
`{% if object.parent.excludeFromUri %}{{ object.parent.slug }}{% else %}{{object.parent.uri}}{% endif %}/{slug}` - which doesn't quite do what I'm after as in the first condition, (where the exclude is true) I need to get the objects parent parents uri.
shorn
CC BY-SA 4.0
2019-03-06T22:28:05.130Z
Then add another `parent` to your output but check if there is a parent. You might want to do the following: create your well formatted code in your entries Twig template and set `{% set object = entry %}` and when you have the correct format you copy it in your cp.
Robin Schambach
CC BY-SA 4.0
2019-03-06T22:48:23.720Z
Ok, Got it working, not sure it's the best to be doing it as it only allows up to 4 levels, so it might be worth rewriting to enable it to handle any level, but I'm pretty sure 4 levels is more than enough for my needs. `{{ object.parent.parent.parent.excludeFromUri == 0 ? object.parent.parent.parent.slug~'/'  }}{{ object.parent.parent.excludeFromUri == 0 ? object.parent.parent.slug~'/'  }}{{ object.parent.excludeFromUri == 0 ? object.parent.slug~'/'  }}{{ object.slug }}` - bit of beast!
shorn
CC BY-SA 4.0
2019-03-06T22:50:44.830Z
You might want to think about coding a little bit more defensive cause I guess most of your entries can't be resaved because of the exceptions that are thrown
Robin Schambach
CC BY-SA 4.0
2019-03-06T22:53:24.023Z
Yea, just wanted to test it's possible then optimise.
shorn
CC BY-SA 4.0
2019-03-06T22:57:22.307Z
To clean up your code: you could loop your entries ancestors via reverse filter https://twig.symfony.com/doc/2.x/filters/reverse.html and do what you did above
Robin Schambach
CC BY-SA 4.0
2019-03-06T23:12:44.153Z
`{% if object.level > 1 %}
    {% for parent in object.getAncestors()  %}{{ parent.excludeFromUri != 1 ? parent.slug~'/' }}{% endfor %}
{% endif %}{{ object.slug }}` - seems to do the job
shorn
CC BY-SA 4.0
2019-03-06T23:17:45.367Z
Seems good well done glad you got sorted
Robin Schambach
CC BY-SA 4.0
2019-03-06T23:19:28.607Z
Cheers, thanks for the nudge in the right direction. Was hitting a bit of a blank!
shorn
CC BY-SA 4.0
2019-03-06T21:47:22.660Z
2019-03-06T21:47:22.660Z
Robin Schambach
<p>I have a structure where the Entry URI Format is <code>{parent.uri}/{slug}</code> - what I'm looking to do is conditionally not include entry slugs within the uri based on a show/hide setting in the entry.</p>

<p>For example, I might have structure where an entry, <code>sub-page2</code> has a uri like so <code>page/sub-page/sub-page2</code> but if there is a setting on sub-page to exclude it, it's uri should be <code>page/sub-page2</code></p>

<p>This would mean that the conditional to show it should only be considered if it's being called as a parent. <code>sub-page</code> should still be accessible from <code>page/sub-page</code> - trying to find a way to achieve this?</p>

<structure>
Custom URI to exclude entries in structure
# Custom URI to exclude entries in structure

(Asked by: username_0 on 2019-03-06)

I have a structure where the Entry URI Format is `{parent.uri}/{slug}` - what I'm looking to do is conditionally not include entry slugs within the uri based on a show/hide setting in the entry.

For example, I might have structure where an entry, `sub-page2` has a uri like so `page/sub-page/sub-page2` but if there is a setting on sub-page to exclude it, it's uri should be `page/sub-page2`

This would mean that the conditional to show it should only be considered if it's being called as a parent. `sub-page` should still be accessible from `page/sub-page` - trying to find a way to achieve this?

**Tags:** structure


---

## 1 answers

### Answer by username_1 on 2019-03-06. Score: 2 | Accepted answer

You have the full power of Twig in your template to render the entries Uri so you can conditionally render what you want. This could be your template in your section settings 
{% if object.parent.field == true %}{{ object.parent.uri }}{% else %}{{object.parent.slug}}{% endif %}

There are no real limitations since you can do what you are used to in Twig in your frontend

> **Comments:**
> **username_0**: Thanks for that. I feel like I'm close but not quite there.  `{% if object.parent.excludeFromUri %}{{ object.parent.slug }}{% else %}{{object.parent.uri}}{% endif %}/{slug}` - which doesn't quite do what I'm after as in the first condition, (where the exclude is true) I need to get the objects parent parents uri.  
> **username_1**: Then add another `parent` to your output but check if there is a parent. You might want to do the following: create your well formatted code in your entries Twig template and set `{% set object = entry %}` and when you have the correct format you copy it in your cp.  
> **username_0**: Ok, Got it working, not sure it's the best to be doing it as it only allows up to 4 levels, so it might be worth rewriting to enable it to handle any level, but I'm pretty sure 4 levels is more than enough for my needs. `{{ object.parent.parent.parent.excludeFromUri == 0 ? object.parent.parent.parent.slug~'/'  }}{{ object.parent.parent.excludeFromUri == 0 ? object.parent.parent.slug~'/'  }}{{ object.parent.excludeFromUri == 0 ? object.parent.slug~'/'  }}{{ object.slug }}` - bit of beast!  
> **username_1**: You might want to think about coding a little bit more defensive cause I guess most of your entries can't be resaved because of the exceptions that are thrown  
> **username_0**: Yea, just wanted to test it's possible then optimise.  
> **username_1**: To clean up your code: you could loop your entries ancestors via reverse filter https://twig.symfony.com/doc/2.x/filters/reverse.html and do what you did above  
> **username_0**: `{% if object.level > 1 %}     {% for parent in object.getAncestors()  %}{{ parent.excludeFromUri != 1 ? parent.slug~'/' }}{% endfor %} {% endif %}{{ object.slug }}` - seems to do the job  
> **username_1**: Seems good well done glad you got sorted  
> **username_0**: Cheers, thanks for the nudge in the right direction. Was hitting a bit of a blank!  

---

