2

I have created a Structure page 'Products' with a URL of '/products' so that I can write a description etc, but then I would also like to pull in the Products into the template. I'm a little unsure on how best to achieve this.

The Sections are created as follows;

Page
Handle: page
Type: Structure
URL Format: {slug}
Entry Template: page/_entry

Product
Handle: product
Type: Channel
URL Format: products/{slug}
Entry Template: products/_product

In ExpressionEngine I would have pulled in a Channel entries tag for the page and then another entries tag for the products.

1

There are a few ways to deal with this, but it really depends on how you have the rest of your sections set up. If the structure is exclusively products with only a single root level item called "products" then I would probably suggest storing content for the products overview page somewhere else. This will greatly simplify your products structure (you could even change it to a 'channel' if there is no other reason for the hierarchy). Where you decide to store the product index/overview info is up to you. You could, for example:

  • create a single called "products" (with uri '/products'), or
  • add it to the "pages" structure as "products" (with a custom Entry Type "products"), or
  • create a template "products/_index.html" with description fields, etc. stored in a global set called "productsDescription" (or whatever you like).

Whichever way you choose the product index page will still be "/products/" and the product detail pages will still be "/products/product-slug/".

If you do decide to place the products overview in the "pages" section, for example, you can just check for the entry type using a conditional or switch statement (within the 'page/_entry.html' template), like so:

{% if entry.type == "products" %}
    {# display products index page template here #}
{% endif %}

However you decide to do it, you can load the actual products like so (as Marion suggested above):

{% for product in craft.entries.section('product') %}
    {{ product.title }}
    ...
{% endfor %}
  • As per his description, he already has the products in a channel section "product" and wants the index to be an entry of his "page" structure section. But that's what you suggested in the lower part of your answer anyways :) – carlcs Jan 20 '15 at 8:25
  • Thanks Douglas. Just wanted to make sure that using a single template for all of my pages didn't have overhead with a conditional to display products for those pages that wouldn't show the products. – doubleplusgood Jan 20 '15 at 11:13
  • @doubleplusgood don't worry about performance with Twig code like conditionals and includes. Did you read this Doc article: "How can I give each Entry Type its own template"? – carlcs Jan 20 '15 at 15:15
  • @doubleplusgood I agree with carlcs. The overhead is insignificant. I would design the system in whatever makes most sense from a organizational, development and user perspective. – Douglas McDonald Jan 20 '15 at 16:51
1

If your channel is products, you list them like this on their index page (which is presumably your '/products' page)

{% for p in craft.entries.section('products') %}
  {# p is our product. display it however #}
  {{p.title}}
{% endfor %}
  • Thanks Marion. I guess what I would also like is to pull in the structure page content for 'Products' into the same template but my Page section/channel uses the page/_entry.html template rather than products/index.html – doubleplusgood Jan 19 '15 at 21:24

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.