MODx – Include Page Content Snippet

In MODx I added an “advertisement” page for a new project to allow people using the manager to change the adverts displayed throughout the site. I wanted to include that page in a similar fasion to PHPs include and require functions. I found a static method of doing it via the MODx “extras” repository so decided to extend it to allow a custom page ID to be used wherever.

To use the snippet login to the MODx manager, click the Elements Tab then Manage Elements. Go to the Snippets tab then click New Snippet. Call the snippet IncludePage, paste the code below then save.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
/*
::::::::::::::::::::::::::::::::::::::::
 Snippet name: IncudePage
 Short Desc: Include a MODx page content
 Version: 1.0
 Author: Daniel Gibbs (danielgibbs.net)
 Date: 5th November 2009
::::::::::::::::::::::::::::::::::::::::
Example Usage:
    [[IncludePage? &pageID=`12`]]
::::::::::::::::::::::::::::::::::::::::
*/
 
# check if inside manager
if ($m = $modx->insideManager())  {
	return ''; # don't go any further when inside manager
}
 
if (!isset ($pageID))  {
	return "<p>Error: No page ID specified, can't include content...</p>";
}
else {
	$content = $modx->getTemplateVarOutput('content', $pageID);
	return $content['content'];
}
?>

Example Usage:

1
 [[IncludePage? &pageID=`12`]]

This should be self explanatory but… setting pageID to 12 will include page 12. Page id’s are displayed in the document tree to the left of the manager next to the page title; i.e. Advertise (12).

2 Responses to “MODx – Include Page Content Snippet”

  1. Hi there,

    fantastic snippet, was looking for something just like this.

    just one mention, in the comments of your script you name it “Snippet name: IncudePage”, it’s missing the “l”, this isn’t super important except if one is lazy and copy/pastes that as the snippet’s name and finds the snippet doesn’t work if they call “IncludePage”

  2. Heh

    Thanks for pointing out the typo Lance.

Leave a Reply


Website Designed and maintained by Daniel Gibbs - Powered by WordPress - Copyright 2009