Page 1 of 1
[Solved]How to access custom page content block value within UDT?
Posted: Sat Nov 06, 2010 11:28 pm
by neil-cmsms
I've added a custom content block via my template {content block="my page data"} so that it now appears in my pages, as expected, when they are being edited - I enter data for "my page data" on each page.
I am writing a UDT that traverses the page hierarchy from the page it's on, and I want to get the value of the custom content block data. This must be easy to do but I have not been able to find the right object method or function I need online and it doesn't seem too obvious. The code I want to use is like like:
Code: Select all
$thisPage = $gCms->variables['content_id'];
$currentNode = &$manager->sureGetNodeById($thisPage);
while( isset($currentNode) && ($level=$currentNode->getLevel()) >= 0 )
{
$pData = $currentNode->GetPropertyValue['my_page_data']; // This method does not seem to work
if(strlen($pData)) {
if(strlen($data)) $data.= ",";
$data.= $pData ;
}
$currentNode =& $currentNode->getParentNode();
}
Can anyone help?
Thanks,
Neil
Re: How to access custom page content block value within UDT?
Posted: Sun Nov 07, 2010 3:42 am
by neil-cmsms
I also tried the following snippet to get pData and that did not seem to work either:
Code: Select all
$smarty_data = "{content block='my_page_data'}";
$smarty->_compile_source('temporary template', $smarty_data, $_compiled );
@ob_start();
$smarty->_eval('?>' . $_compiled);
$pData = @ob_get_contents();
@ob_end_clean();
Am I being stupid and missing something obvious here?

Thanks,
Neil
Re: How to access custom page content block value within UDT?
Posted: Sun Nov 07, 2010 11:18 am
by Peciura
Try it
Code: Select all
/**
* gets page content block
*
* @param string $params['block'] (mandaroty). content clock.
* @param string $params['page'] (mandatory). page alias or id.
* @param string $params['eval'] evaluate content.
*
* @return string content of a block.
*/
$result = FALSE;
if (!empty($params['page']) && !empty($params['block'])){
$contentops = cmsms()->GetContentOperations();
$smarty = cmsms()->GetSmarty();
$block = ( empty($params['block']))? 'content_en' : $params['block'];
$page_id = $contentops->GetPageIDFromAlias($params['page']);
if( !empty($page_id) ){
$content = $contentops->LoadContentFromId($page_id, TRUE);
if( is_object($content) ) {
$result = $content->GetPropertyValue($block);
}
}
}
if (!empty($result) && !empty($params['eval'])){
$smarty->_compile_source('temporary template', $result, $_compiled);
@ob_start();
$smarty->_eval('?>' . $_compiled);
$result = @ob_get_contents();
@ob_end_clean();
}
return $result;
Code: Select all
{get_content_block page='71' block='Image_1_Alt_lt' eval='1'}
Re: How to access custom page content block value within UDT?
Posted: Sun Nov 07, 2010 11:06 pm
by neil-cmsms
Excellent, thanks!
I got it to work using the content object mentioned in the above code. The last section seems redundant though - the $result is correct without doing the $smarty->_compile_source part. Am I missing some case where this would not work without that code? It's interesting that the $result is the same whether that code is there or not.
Thanks,
Neil
Re: How to access custom page content block value within UDT?
Posted: Mon Nov 08, 2010 9:00 am
by Peciura
It is required if you have smarty in content block, e.g you have {sitename}. By default parameter 'eval' is not set as a result smarty is not processed.
Re: How to access custom page content block value within UDT?
Posted: Mon Nov 08, 2010 4:06 pm
by neil-cmsms
Ah, yes. The example usage call had eval set to 1 so I figured this was required in general (and I suppose it is, actually). My initial target was simple text - it was when I tried to process main content blocks that I found its real purpose.
Many thanks for the help!
Neil