Nov 20, 2008, 05:47 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
Search via SMF or Google: modx forums all of modxcms.com web
  MODxCMS.com   Forums   Help Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: [Snippet] getdoc  (Read 7370 times)
0 Members and 1 Guest are viewing this topic.
Mark
Coding Team
*
Posts: 3,247


Ditto Developer


WWW
« on: Dec 04, 2005, 12:31 PM »

This snippet gets a specific field (any of these http://modxcms.com/the-document-object.html) for an individual document.

Example call that will return the content of the first document (id 1):

Code:
[[getdoc? &id=1 &field=`content`]]

Snippet code:

Code:
$id = isset($id)? $id: $modx->documentObject['id'];

$field = isset($field)? $field : "pagetitle";

$result = $modx->getDocument($id,$field);

return $result[$field];

« Last Edit: Jan 02, 2006, 07:14 PM by Mark » Logged

zi
MODx Special Forces /
Administrator
*
Posts: 3,003


May Peace Be On You


WWW
« Reply #1 on: Dec 04, 2005, 12:34 PM »

Very nice little snippet !

Thanks for sharing.

regards,

zi
Logged

“Internet Explorer’s CSS rendering: WYSIWTF”. — someone genius
--------------------------------------------------
banzai
Testers
*
Posts: 857


MODx Italia


WWW
« Reply #2 on: Dec 04, 2005, 12:51 PM »

great useful snippet! thanks!
Logged

MODx Websites Showcase 
Add your site to www.modx.it!
-----------------------------------------------------
FREE MODx Templates
www.tattoocms.it
-----------------------------------------------------
VIDEO: whats new in MODx 0.9.5   | VIDEO: MODx PHP Application Framework  |  bubuna.com - Web & Multimedia Design
rthrash
Foundation
*
Posts: 9,510



WWW
« Reply #3 on: Dec 04, 2005, 03:40 PM »

Does it get the raw code or the parsed output?
Logged

MODx is a framework that allows web professionals to turn over sites to end-users for daily maintenance without worrying. Community participation and questions are encouraged, especially when you help us help you, read the wiki, and review snippet parameters – even if you have to look at the source. Searching the forums helps, too.
Ryan Thrash
MODx Co-Founder
Principal @ Collabpad
work productively.
work intelligently.
work together.
Mark
Coding Team
*
Posts: 3,247


Ditto Developer


WWW
« Reply #4 on: Dec 04, 2005, 03:55 PM »

Parsed output.

Sorry folks I copied and pasted the wrong version!

I updated the first post with the correct version!
Logged

davidm
Marketing & Design Team
*
Posts: 6,733


The best way to predict the future is to invent it


WWW
« Reply #5 on: Dec 05, 2005, 04:08 AM »

Thanks Mark for this much needed snippet, I can think of a few uses for it Smiley
Logged

.: david-molliere.net : Pour un web libre, moderne et ouvert! :: Forums modxcms.fr : Participez à l'élaboration du site MODx francophone ! :.
! Nouveau !  En live, ne manquez pas les news de modxcms.fr sur Twitter   ! Nouveau !

MODx est l'outil idéal pour les developpeurs et webdesigners qui cherchent un framework de gestion de contenu hautement flexible et performant, tout en étant simple d'accès pour les utilisateurs finaux.

Config : Apache 2.2.8 - MySQL 5.0.45 - PHP 5.2.6 | Debian 4.0 (Etch)

Réalisations sous MODx : nodeo.net | pargade-notaires.fr | soleil.info | gican.asso.fr | michelez-notaires.com | amadom.gerondicap.com | jocelyne-violet.net
rthrash
Foundation
*
Posts: 9,510



WWW
« Reply #6 on: Dec 05, 2005, 08:28 AM »

Both versions would be useful actually (parsed and raw unparsed). Smiley
Logged

MODx is a framework that allows web professionals to turn over sites to end-users for daily maintenance without worrying. Community participation and questions are encouraged, especially when you help us help you, read the wiki, and review snippet parameters – even if you have to look at the source. Searching the forums helps, too.
Ryan Thrash
MODx Co-Founder
Principal @ Collabpad
work productively.
work intelligently.
work together.
luke.stokes
Support Subscriber
*
Posts: 54



« Reply #7 on: Mar 01, 2006, 05:00 PM »

Slight modification to look for a template variable:
Code:
// Determine the target document, defaulting to self if nothing specified
$id = (!isset($id)) ? $modx->documentObject['id'] : "$id";
// Document field to get
$field = (!isset($field)) ? 'pagetitle' : "$field";

$array = $modx->getDocument($id);
$output = $array[$field] ;
if (!$output) {
    // try to get a template variable
    $TVarray = $modx->getTemplateVar($field, "*", $id);
    $output = $TVarray["value"];
    if (!$output) {
         $output = $array['pagetitle'];
    }
}
return $output;
Logged
dogas
Jr. Member
*
Posts: 10

I love MODx!


« Reply #8 on: Mar 02, 2006, 01:32 PM »

Great snippet, although for some documents I request I'm getting no output when I request the content, although the "source retrieved from cache" gives me the following:
Code:
<?php die('Unauthorized access.');


Any help would be greatly appreciated.  Thanks.
Logged
luke.stokes
Support Subscriber
*
Posts: 54



« Reply #9 on: Mar 02, 2006, 02:01 PM »

Hmm... does it work fine without the modification I made? I just put in a quick hack, I didn't do any security testing. It may be that the template variable you're requesting has specific document permissions on it which would cause the getTemplateVar call to fail. I'd try taking a look at the "some documents" to find a pattern and see if you are requesting info from pages that have security on them.

Hope that helps.
Logged
dogas
Jr. Member
*
Posts: 10

I love MODx!


« Reply #10 on: Mar 02, 2006, 02:14 PM »

I'm using both versions of your code.  Both of which return a blank output.  I think the "unauthorized access" message is because of a caching issue, which I turned off for the test page I am using.

Is there any other way to debug what might be going on?  Does MODx log output anywhere?
Logged
luke.stokes
Support Subscriber
*
Posts: 54



« Reply #11 on: Mar 02, 2006, 03:14 PM »

Well, there's the "Event log viewer" under the Administration tab and the Audit trail. Are you calling the snippet cached or unchached ([[getDoc]] or [!getDoc!]). I need learn more about the differences between the two, but I've always been told to use [! !] which does not cache.

Sorry I couldn't be more helpful.
Logged
dogas
Jr. Member
*
Posts: 10

I love MODx!


« Reply #12 on: Mar 02, 2006, 03:33 PM »

Well, I've sort of figured it out.  It seems for me the getdoc function works correctly if the page does not use a tempate that has template variables.  If i try to get a page the is based on a template that has template variables, it spits out a blank page.

Oh well, back to the drawing board.  Thanks again for your help!
Logged
luke.stokes
Support Subscriber
*
Posts: 54



« Reply #13 on: Mar 03, 2006, 01:00 AM »

Well, you found a pattern, but I don't think that's the problem. All template variables are associated with templates. So in order to use the getDoc to grab a template variable, by definition the page you are looking for has to have a template (with template variables).

I'd look again at your template variables and the document security you have set for them. That would explain the access error you got.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP

Copyright © 2005-2008 MODxCMS, All rights reserved. Contact Us
Styles by ziworks.com

Powered by SMF 1.1.4 | SMF © 2005, Simple Machines LLC

Valid XHTML 1.0! Valid CSS!