Fragments vs standard WP templating


In the process of creating a reusable structure for my future Cwicly websites, I’m testing fragment based architecture to see if it is more efficient than WP standard templates.

So, first, what I want to achieve (most of the time) is this kind of standard HTML markup:

header tag
main tag
- article tag
-- header tag
--- h1 tag
-- post content
-- social share, CTA, comments, etc.
- sidebar div
footer tag

With WP templates, I need to create a default template for each type of page (post, page, archive, 404, etc.) and add my main tag div and recreate the page structure inside each.

It is not that hard, but there is redondant stuff, like the main tag div or adding the page header template part (and potentially more with CTA sections, common archive loops, etc.).

With fragments, I’ve come to this setup: only single.php WP template with main tag div wrapping a unique “content” fragment that deals with everything.

Note that I have to set visibility conditions for each content part in fragments, whereas with default WP template I don’t need that since each template already handles those conditions.

So, my questions:

  • is this the right way to do this in Cwicly?
  • is it future proof regarding Cwicly’s templating strategy? No future changes in all this fragment mechanism?
  • are there flaws in the long term I cannot detect now? (like in one year, I need to add something somehere with some condition and I cannot do it with fragments)

Because with standard WP templates, I know where I’m going (at least I’m going the WP way), and even though I need to repeat a few gestures, I don’t have to mind all the visibility conditions combinations, growing with complex sites.

Besides, reordering template parts in a fragment is not possible, that’s why I wonder if I actually use it properly :wink:
Like, I inserted my page header after the page content, and now I can’t move it before, so I need to recreate the whole structure, which is not acceptable if it is working like this.

Another difference is that WP templating allows to see and edit each template part inside a template editor: for instance you can see and directly alter global header or footer or page header if you’re editing an archive template.
I find this very handy (note that you can hide them if they are in the way with the Cwicly eye icon).

Finally, I think I can reproduce most of the fragment system with template parts and insert them in my default templates layouts with conditions if needed, so I can’t decide which way to go!

What is your experience with this?

Well, after trying in vain to organize things with fragments, I ended up redesigning all with templates and it’s much clearer.
Also you benefit from the visual previews in template page, which is pretty nice.

Then, I watched the video about fragments again, and then I think I understood something: fragments are not meant to be used to build or organize everything in a single template or multiple templates, but could rather be considered as WP hooks that can be used to inject stuff globally with full condition management.

So you can add fragments to your pages/templates at specific locations, and then activate the visibility of the associated template parts according to time, user role, etc…

I guess the idea here is that you can group template parts thematically and manage visibility more easily than editing each template or page where those template parts are used, as well as add new template parts at given locations without opening actual templates.

Main use case for me would be a site with multiple dynamic topbars/headers/banners/ctas/popups.
Could be useful for member sites too, with private sections, displaying profile/download pages or any data according to privilege…

Feel free to correct me or add your own use cases or examples :wink:


Perfectly summed up, couldn’t have put it better myself.

A fragment can hold a multitude of different template parts depending on the query, but occupy a specific position in a template (and multiple templates).

Good point about reordering the template parts :+1:

1 Like

The above discussion shows me, that it would be helpful to have a fully fledged, fully funcitonal and structured best practice site example one can download/ or log into its dashboard and examine it, e.g. with respect to template, template parts and global parts structure and use, where custom templates and template parts are used instead of standard ones, the use of fragments, where what content is put directly into a specific template vs. having it placed in a regular page/ post etc.

1 Like