Hi,
I want to include certain content only on pages which are in certain folders on my website.
Specifically, I want to include the AuthorTimestamp snippet after the page's main heading, but only on pages which are within folders "news/????" (where ? ? ? ? = [0-9][0-9][0-9][0-9], ie, a folder name which corresponds to a year), as it is useful/essential to include the publication date in news articles.
I'm trying to work out what the simplest way to do this would be. The previous version of the website had several different page templates for different purposes, which became somewhat awkward to manage, as contributors adding news items had to remember to make several settings when adding news items. The new version of the website will have only one template for all but certain very specialised pages (which contributors won't edit).
I'm wondering whether the easiest way to do this would be to have a Template Variable that contributors can activate, when the page is a news article, which will contain the snippet call as its output (but they will have to remember to do this), or whether there is some way of having the page template automatically output the snippet call if the URI matches (and output nothing extra, if not), or even some other possible solution that I have not thought of..?
Thanks for any advice!
[Edited, as the 4 ?s in a row became an undesired smiley otherwise!]
Please don't PM me unless it's absolutely essential: if a technical question is worth asking, it's worth asking in public, so that others can share their experience, and so that all can learn from the answers.
You could try this, a wrapper snippet for the author timestamp:
Create a new snippet with the code below, named whatever you want.
Replace your AuthorTimestamp snippet call with your new snippet name.
Modify the $params values accordingly.
// add document ids of news folders
$news_folders = array( 3, 5 );
$out ='';
// get the ultimate parent of the current document
$uparent = $modx->runSnippet( 'UltimateParent' );
// check if the current document ultimateparent is found in the $news_folders array
// if so run the AuthorTimestamp snippet and return output
if( in_array( $uparent, $news_folders ) ){
$params = array(
'dateFormat' => '%d-%b-%y %H:%M',
'usedDate' => 'createdon',
'tpl' => 'By <strong>[+Author+]</strong> on [+Date+] '
);
$out = $modx->runSnippet( 'AuthorTimestamp', $params );
}
return $out;
Here's a snippet based on the AuthorTimestamp code, with folder and chunk support:
params:
&news_folders=`2,5`
&dateFormat=`%d-%b-%y %H:%M`
&usedDate=`createdon`
&tpl_chunk=`ChunkName`
&tpl=`By <strong>[+Author+]</strong> on [+Date+] `
// add document ids of news folders
$news_folders = isset( $news_folders ) ? explode( ',', $news_folders ) : array( 5, 2 );
// format for the Date
$dateFormat = isset( $dateFormat ) ? $dateFormat : '%d-%b-%y %H:%M';
// Date field to use (ex: createon, editeon, pub_date, etc...)
$usedDate = isset( $usedDate ) ? $usedDate : 'createdon';
// Template for the output.
// added chunk support
// &tpl_chunk=`Valid_Chunk_Name`
// &tpl=`By <strong>[+Author+]</strong> on [+Date+] `
$tpl = isset( $tpl_chunk ) ? $modx->getChunk( $tpl_chunk ) : ( isset( $tpl ) ? $tpl : '' );
$tpl = $tpl !='' ? $tpl : 'By <strong>[+Author+]</strong> on [+Date+] ';
$out ='';
// get the ultimate parent of the current document
$uparent = $modx->runSnippet( 'UltimateParent', array( 'top' => 1 ) );
if( !in_array( $modx->documentObject['id'], $news_folders ) && in_array( $uparent, $news_folders ) ){
// Ensure proper charset is used
if( $modx->config['modx_charset']=='UTF-8' ) {
$createdon = utf8_encode( strftime( $dateFormat, $modx->documentObject[$usedDate] ) );
}else{
$createdon = strftime( $dateFormat, $modx->documentObject[$usedDate] );
}
$author = $modx->db->getValue( $modx->db->select( 'fullname', $modx->getFullTableName( 'user_attributes' ), 'id = "'.$modx->documentObject['createdby'].'"' ) );
if( $author ){
$replace = array(
'[+Author+]' => $author,
'[+Date+]' => $createdon
);
$out = str_replace( array_keys( $replace ), array_values( $replace ), $tpl );
}
}
return $out;
[ed. note: breezer last edited this post 13 years ago.]