0

Modx and Blogging part 7: Creating template chunks


09
Oct


Modx, Templating, Chunk, Articles

In taking this deliberate, but round-about way of developing my blog site we have delved very deeply into one page (the blog summary or main page) and largely ignored the others. We are now at the point where we can start to take what we have learnt and more easily apply it to the other pages. But first, we'll be identifying blocks of template code and be creating numerous chunks for elements that repeat on multiple pages, which will significantly help in streamlining the creation and integration of the remaining blog pages.

I have not been writing these posts from a beginner's perspective, but I realise that beginners to Modx may well be reading this. Where appropriate, I have been re-directing readers to an excellent series of tutorials on Revolution by Mary over on Coding Pad. If you are uncertain about chunks and how to create them then this tutorial of Mary's should clarify things. Feel free to abandon me and come back when you are ready.

Also, it is worth repeating that most of you will not be using the same template (Caprice) as me, nor will you necessarily agree as to the best way to use it within the Articles framework. That is as it should be - mine is but one way, not the way.

Chunks and templates

There are a number of elements that repeat on every page of a website:

  • header
  • footer
  • navigation
  • sidebars
  • logo

Based on the templating work done in parts 5 and 6, we will review the templates already modified and identify code to turn into chunks to be used on the remaining Articles templates as well as other pages in the site. Take the following as an example only - I fully expect some of what I do now will evolve and change as I proceed further with my site development.

So having identified chunks of the ArticleTemplate code that may or may not repeat in other templates, I created the following chunks:

archives


comments

[[+commentsenabled:is=`1`:then=`

`]]

footer




Note that I have yet to determine the final layout of my footer. This is effectively a placeholder for when I do.

jscript.all







This is the javascript that is likely to be needed on each page. Other scripts may need to be added to each page

latest


link











logo


meta


Only one line at the moment - I'll be adding to this later.

nav


search


social


Also incomplete at this time - a placeholder.

tags


Having created these chunks, I can now remove the code from the ArticlesContainerTemplate and replace it with the appropriate chunks. This results in the following code:

ArticlesContainerTemplate

<!DOCTYPE HTML>
<html lang="en-US">
<head>
  [[$rg.meta]]
  Argy's Blog
  [[$rg.link]]
  [[$rg.jscript.all]]
</head>
<body>

Argy's Blog

I'm a technical writer, developer and DBA with wide-ranging skills and interests that I enjoy writing about. I hope you find something of interest here...
[[*content]]
[[$rg.latest]] [[$rg.tags]] [[$rg.comments]] [[$rg.archives]]
[[$rg.footer]]
</body> </html>

Even better, I can now start to use these chunks in the other templates that I need to convert: ArticleTemplate and SearchResults - as well as non-Articles pages such as my Home page. In very short order, I was able to modify both these templates to take on the look and feel of the Caprice template - knowing that each of the chunks I used were not only well tested, but also, as the site evolves and changes, any changes to a chunk would automatically be reflected throughout the site. This is what the SearchResults template now looks like:

SearchResults

<!DOCTYPE HTML>
<html lang="en-US">
<head>
  [[$rg.meta]]
  Argy's Blog
  [[$rg.link]]
  [[$rg.jscript.all]]
</head>
<body>

Search results

[[!SimpleSearch? &depth=`50` &ids=`2` &idType=`parents` &hideMenu=`1` &highlightTag=`strong` &tpl=`rg.ArticlesSearch`]]
[[$rg.footer]]
</body> </html>

I won't be listing the ArticleTemplate just yet, as I realised when I began working on it that the Comments sections (creating and listing) that is driven by the Quip plug-in needed to be worked upon. That will be the subject of Part 8.



Add a Comment