Thanks for the new events in TP3.2!
Here’s a little plugin that allows you to modify the document template at runtime. I’ve no idea if this could be usefull to others but I post it anyway. It might help some to understand the power of plugins!
I noticed that there’s a (quite lengthy) snippet for etomite that does the same thing (cut&paste the link):
EDIT: The author (jaredc thanks!) of the snippet kindly pointed out (in this thread) that this snippet does actually something else. It’s some sort of the same but not at runtime.
www.etomite.org/browsesnippets.html?int_snipid=38&lst_category=10
This is what I use to render a page with a ’print this page’ template. I’ve added a bit of javascript to the template that fires the printpage dialog of your browser.
See also this thread:
http://modxcms.com/forums/index.php/topic,753.0.html
/*
* Written by: Theo Ketsinga
* Created: 8/2005
* For: MODx cms (modxcms.com)
* Name: ChangeTemplate
* Description: Enables template modification at runtime by parameter
* Events: OnLoadWebDocument
*
* Put this text into a new plugin and set the System Event
* 'OnLoadWebDocument'. You can now override the template for a
* page by adding the parameter &template=<template_id> to a page.
* Look into the table modx_site_templates for the template id to
* use. You could also change the SQL query below a little.
* Change 'id' into 'templatename' and your parameter understands
* the name of the template. And you should then remove the line
* $modx->documentObject['template']=$overrideTemplate;
* Anyway, if there's a need for this I'll post a modified plugin.
*/
if (isset($_REQUEST['template'])) {
$overrideTemplate=$_REQUEST['template'];
if (is_numeric($overrideTemplate)) {
$modx->documentObject['template']=$overrideTemplate;
$sql = "SELECT * FROM ".$this->getFullTableName("site_templates")." WHERE ".$modx->getFullTableName("site_templates").".id = '".$overrideTemplate."';";
$result = $modx->dbQuery($sql);
$rowCount = $this->recordCount($result);
if($rowCount>1) {
$this->messageQuit("Incorrect number of templates returned from database", $sql);
} elseif($rowCount==1) {
$row = $this->fetchRow($result);
$modx->documentContent = $row['content'];
}
}
}
EDIT: Added ’is_numeric’ check.