1

My entry template looks for the category of the entry

{% for category in entry.productCategory %}
      {{ category.uri }}
{% endfor %}

This will output products/[category slug] as I intended. There is only one value in the loop.

As soon as I wrap the category URI in HTML like this:

{% for category in entry.productCategory %}
     <a href="{{ category.uri }}">view</a>
{% endfor %}

The URI doubles: eg the output is repeated so the template renders {{ category.uri }} as

products/[category slug]/products/[category slug]

Am I missing something in the loop / or Twig behaviour?

  • That is extremely odd behaviour - there is no reason why the HTML would make any difference. What happens if you add one line with HTML and one without within the same loop? Also, what are your category URL formats set to in the CP? – Luke Pearce Aug 2 '16 at 12:00
  • Same thing as above – joomkit Aug 2 '16 at 15:52
1

Using {{category.uri}} is likely just appending to the page's URL. You can use either {{category.url}} or {{siteUrl~category.uri}} to get the full URL.

| improve this answer | |
1

Looked into this some more: I think this was due to my local development multiconfig environment having a similar url to the live and the order in which they appear in the config seemed to be responsible for creating some repeated URL segments. I changed the local URL ( MAMP) to something different and {{ category.url }} works fine

| 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.