modx icon
5th
Nov 09

MODx – Include Page Content Snippet

Posted Thursday 5th November 2009
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).

Download for Evolution

MODx Revolution

As pointed about by a viewer via email this doesn’t work in MODx Revolution. This is because getTemplateVarOutput has been deprecated in the latest MODx. The updated script below (be warned, not thoroughly tested) will work for Revolution.

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: IncludePage
 Short Desc: Include a MODx (Revolution) resource content
 Version: 0.1
 Author: Daniel Gibbs (danielgibbs.net)
 Date: 20th Jan 2011
 NOT TESTED - Should work though
::::::::::::::::::::::::::::::::::::::::
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><strong>Error: No page ID specified for [[IncludePage]]</strong></p>";
}
else {
	$res = $modx->getObject('modResource', (int)$pageID);
	return ($res->get('content'));
}

Download for Revolution

Bookmark or share this page:

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon

Related posts:

  1. Determing Login State (MODx)
  2. Changing MODx Managers Document Tree View Nodes
  3. PHP URL Rewriting Without Apache and mod_rewrite
  4. GIMP Graphical Content Divider


MSN Contact: contact [at] danielgibbs.net



6 Comments

  1. Lance 19 December 2009 9:38 am

    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. Gibbs 21 December 2009 10:07 am

    Heh

    Thanks for pointing out the typo Lance.

  3. eRage website 28 July 2010 3:55 pm

    Hey, it’s a nice tip.. but isn’t that what the GetField snippet is for, or am I missing something here?

  4. Jonathan 5 August 2010 1:43 pm

    I cant get this thing working, where am I supposed to place the:
    [[IncludePage? &pageID=`PAGE_ID_NUMBER`]] ?

  5. Gibbs 13 August 2010 10:16 am

    Hi Jonathan,

    You can place it pretty much anywhere. Usually in a pages content or in a template though.

  6. Gibbs 22 January 2011 6:30 pm

    Updated for MODx Revolution. Still very basic but thanks go to Patrick for emailing that it didn’t work under Revolution.

    http://sudo.co/code/modx/includepage-revolution.modx

TrackBack URL

Leave a comment