1

I'm using modx revolution. I'd like to create a chunk called layout that calls other chucks example

Head header nav body footer

then in my template do something like //open layout tag[[$layout]] [[$layout]]//close layout tag. then inside of the the open close tags append my [[*content]]. this would allow me to reuse my layout template over and over again without having to replicate it in the templates. First question, is it possible, second what kind of syntax would be needed to achieve this goal? I'm rather new to modx and know it's possible with other frameworks, so any help would be appreciated. Thanks.

Sample concept done in Apache Tapestry framework, obviously different syntax, but should give you the general idea of what I'm looking for.

components/Chunks used.

Layout Header Nav Footer

Inside of layout

<html>
    <t:Header/>
    <t:Nav/>
    <t:Body/>
    <t:Footer/>
</html>

Inside of Index/Template

<t:Layout>
        template body content goes here ex. [[*content]]
</t:Layout>

Hope this helps to clarify.

Fuzzical Logic
  • 12,947
  • 2
  • 30
  • 58
Code Junkie
  • 7,602
  • 26
  • 79
  • 141

4 Answers4

2

Your post is not very clear and I think you haven't really taken much time to read up on how MODx works before looking for help.

That aside, I think what you want to do is create different templates, structured more or less like this:

[[$header]]
[[$nav]]

<div id="content">
    <h1>[[*pagetitle]]</h1>
    [[*content]]
</div>

[[$footer]]

That might do for your home page, then for internal pages where the layout is a bit different you can create one or more new templates for each layout:

[[$header]]
[[$nav]]

<div id="content">
    <h1>[[*pagetitle]]</h1>
    [[*content]]
</div>

[[$sidebar-chunk]]

[[$footer]]

You can even show different layouts using a single template something like this:

[[$header]]
[[$nav]]

<div id="content">
    <h1>[[*pagetitle]]</h1>
    [[*content]]
</div>

[[*parent:is=`6`:then=`
    [[$recent-articles]]
`:else=`
    [[$sidebar-chunk]]
`]]

[[$footer]]

That should get you started, but you'll soon realise there are multiple ways to do everything in MODx.

okyanet
  • 3,106
  • 1
  • 22
  • 16
  • Thanks, but this doesn't answer my question. I currently have the application setup like this thus far, however I don't feel it's the most efficient way to do this since I'm repeating header, nav, footer and any other redundant chunk in within all templates. What I'm looking to do is create a layout chunk and add something like [[*content]] to the body portion of the chunk. Within my template surround my body with open close layout chunk tags and inject my template body into the body of the chunk. This would prevent the use of redundant code. See my sample above. – Code Junkie Mar 02 '12 at 14:28
  • 2
    You can't 'open' and 'close' tags in MODx. I don't believe adding the header, nav and footer tags makes templates unmanageable or overly redundant either. You could potentially inject your header and footer using a plugin, or build a simple snippet to switch the 'layout' chunk depending on the current resource location. But you'll be working against the system and things will become unmanageable real quick. Also, users won't be able to easily select a template when creating a new resource. How many templates do you currently have? – okyanet Mar 02 '12 at 16:04
  • There isn't many templates within this particular project, it's just a behavior I've become a custom to and wanted to know if it was feasible with modx. If the option doesn't exist, we just won't worry about it. Thanks for getting back to me though. – Code Junkie Mar 02 '12 at 17:16
2

You can put your [[*content]] where-ever you want, even inside another chunk, if that's what you mean.

So your [[$layout]] chunk could just be this:

[[$header]]
[[$menu]]
<div id="content">
  <h1>[[*pagetitle]]</h1>
  [[*content]]
</div>

If you want to make some minor changes in a chunk on a template-basis you could also do something like this in the template:

[[$layout? &customContentBits=`
    <h1>[[*pagetitle]]</h1>
    [[*content]]
`]]

and your layout chunk could then be something like this:

[[$header]]
[[$menu]]
<div id="content">
  [[+customContentBits]]
</div>

That's a placeholder ([[+customContentBits]]) which is set by adding the &customContentBits in the chunk call.

Explained that a tad more with a different use case on my blog some time ago too: http://www.markhamstra.com/modx-blog/2010/10/introducing-element-properties-and-output-modifier/

Mark Hamstra
  • 692
  • 1
  • 4
  • 16
0

What you are asking can absolutely be done. In fact, on my website, I even have the same template/chunk combo providing multiple layouts by passing a template variable as a chunk modifier. But anyhow, let's keep things simple.

A quick note on your question., ModX doesn't use start tags and end tags, natively. It's best to stop thinking that way. Instead just place things where you want to place them. Resource variables can go in any chunk, as each resource is unique.


Create your Chunks:

First, start with the simple ones. Create your Header, Footer, and Navigation. Next, create your Body. Inside the Body, make sure to include your [[*content]] (no... it doesn't have to go into the Template. Finally, create your Layout with the following code:

[[$header]]
[[$navigation]]
[[$body]]
[[$footer]]

Create your Template:

Your template can now be as simple as [[$layout]]. You're done.


Note

While you can do this with ModX, understand that the power of ModX is that you can have multiple templates and chunks depending on the type of content you have. Singularizing everything like this really takes away a major advantage of using the platform.

Community
  • 1
  • 1
Fuzzical Logic
  • 12,947
  • 2
  • 30
  • 58
0

Mark Hamstra more or less gave you the answer, but just to clarify: Any snippet, chunk or output of some sort in Modx can take parameters. Chunks and snippets especially can make use of these params easily. From what i understand you want to have all your templates call [[$layout]] and nothing else. The layout chunk in turn looks like

[[$header]]
[[$navigation]]
[[$body]]
[[$footer]]

On this you simply build and add your params, nesting them down from the top like

[[$layout? &useNavigation=`1`]]

(And continue passing the param in your layout chunk)

[[$header]]
[[+useNavigation:is=`1`:then=`[[$navigation]]`]]
[[$body]]
[[$footer]]

Another way of accomplishing the same behaviour would be to use a single template to which you have connected a series of template variables that decide how the template looks like. You might have template variable called useNavigation of checkbox type. If you check this through the resource editor it will be passed to your $layout chunk directly without having to add params into the $layout chunk call.

[[$layout]]

(Just call layout normally and add the TV checks to the layout chunk directly. 
Note the difference between calling a TV and a placeholder, + vs *)

[[$header]]
[[*useNavigation:is=`1`:then=`[[$navigation]]`]]
[[$body]]
[[$footer]]