Page 1 of 1
Any other ways to get page contents in UDT?
Posted: Tue Aug 21, 2012 7:13 pm
by eden
This really makes my heart bleed:
Code: Select all
global $gCms;
$manager =& $gCms->GetHierarchyManager();
$thisPage = $gCms->variables['page_name'];
$currentNode = &$manager->sureGetNodeByAlias($thisPage);
$contentops =& $gCms->GetContentOperations();
$nodes = $currentNode->getChildren();
if ($currentNode->hasChildren()) {
foreach ($nodes as $node) {
$content= $node->getContent();
$requestUrl = 'http://localhost/index.php?p='.$content->alias();
@ob_start();
echo file_get_contents($requestUrl);
$contentx = @ob_get_contents();
@ob_end_clean();
//do what you want...
echo $contentx;
}
}
Is there really no other way to get hold of page contents
with template parsed in?
Re: Any other ways to get page contents in UDT?
Posted: Tue Aug 21, 2012 7:26 pm
by Dr.CSS
Maybe you could explain a little more fully what it is you are trying to acomplish...?
Re: Any other ways to get page contents in UDT?
Posted: Wed Aug 22, 2012 7:16 am
by eden
Sure. I want to make a site with horizontal layout with variable number of 'pages' in each page. Each 'page' can also have 1 to 4 columns in several layout options.
My first idea was to do it the right way. So I made a page with a content block for each pagelet but then I realized that I need a layout for individual pagelets as I can't hand code columns in the editor. Because I'm not the only one in our club that is going to edit our web site so with layout in the wysiwyg editor I'd be constantly repairing things like 'I just pressed del and everything went white'.
So my next idea was to split each pagelet to 4 content blocks but that would get really messy really soon. And there would be lots of strangeness in guessing the right layout to use based on some fifth oneline content block. This would mean that a 10 pagelet page would need 50 content blocks.
So the most elegant solution would be to have a page with child pages. Each child page represents one pagelet with a template that can be selected as usually. When user is browsing the site a horizontal layout is formed by pulling parent page with all its children into a long row. This means that I don't want just content_en from the children but a completely or at least partially parsed page.
Now the code above actually works but I feel that the
http:\\ part unnecessarily slows the thing down and makes it kind of ugly.
So is there a RenderPageById() or something...
Re: Any other ways to get page contents in UDT?
Posted: Wed Aug 22, 2012 7:45 am
by Jos
You might want to take a look at the CGSimpleSmarty module
Re: Any other ways to get page contents in UDT?
Posted: Wed Aug 29, 2012 11:26 pm
by eden
Jos wrote:You might want to take a look at the CGSimpleSmarty module
I did already but that gives only access to content blocks.
I've tried a dozen ways but it seems that cmsms isn't really up to it you would need to be able to do:
Code: Select all
$gCms1=cmsms(); //original
$gCms2=cmsms('second'); //second
So that templates ect. wouldnt get mixed. Another common problem in my many attempts is that smarty dies when there are multiple {content} blocks:
//main template:
<div>page1 {content} {content block='2'}</div>
<div>page2 {page_content alias='pageone'}</div>
<div>page3 {page_content alias='pagetwo'}</div>
//secondary template (for pageone and pagetwo):
<div>Column 1 {content}</div>
<div>Column 1 {content block='column2'}</div>
//becomes essentially this when page is parsed:
<div>page1 {content} {content block='2'}</div>
<div>page2 <div>Column 1 {content}</div>
<div>Column 1 {content block='column2'}</div></div>
<div>page3 <div>Column 1 {content}</div>
<div>Column 1 {content block='column2'}</div></div>
//no matter how much you try to avoid it as I haven't found a way to have several smarty/cmsms instances running.
Anyway I got it to work. It's not pretty and it's not ready, but it works and it's faster than the url method.
{page_content alias=''} tag gets the raw template from the db and converts all {content} tags to {subcontent pageindex='x' block='x'} tags and then template is parsed with smarty. Subcontent tag gets the correct content block and parses it. All included blocks act as a part of primary template which has it's pros and cons (title etc. tags are mostly unusable, but urls work perfectly as they point to parent page).
If someone uses this remember that it's your job to make sure not to get stuck on a loop
Code: Select all
function smarty_function_page_content($params, &$template)
{
$gCms = cmsms();
$smarty=$gCms->GetSmarty();
$hm = $gCms->GetHierarchyManager();
if(empty($params['id']))
{
if(empty($params['alias']))
{
if(empty($params['position'])) { echo 'Missing arguments'; return null; }
else {
$position = $params['position'];
$page= $hm->getNodeByHierarchy($position);
}
}
else
{
$alias = $params['alias'];
$page=$hm->getNodeByAlias($params['alias']);
}
}
else
{
$id = $params['id'];
$page=$hm->getNodeById($params['id']);
}
if(is_null($page))
{
echo 'Node missing'; return null;
}
//edit later
$cnt= $page->getContent();
if(is_null($cnt))
{
echo 'No content'; return null;
}
$alias=$cnt->Alias();
$contentops=$gCms->GetContentOperations();
$contentobj = $contentops->LoadContentFromAlias($alias,true);
if( !is_object($contentobj) )
{
echo 'No content object';
return null;
}
if( !$contentobj->IsViewable() )
{
return null;
}
if( $contentobj->Secure() && (! isset($_SERVER['HTTPS']) || empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off') )
{
echo 'Content is marked as secure and this is not a secure page';
return null;
}
$db = $gCms->GetDb();
$q = "SELECT template_content FROM ".cms_db_prefix()."templates WHERE template_id=".$contentobj->TemplateId();
$dbresult = $db->Execute( $q );
if( !$dbresult )
{echo 'DB error: '. $db->ErrorMsg()."<br/>";}
$data = $dbresult->FetchRow();
//Let's replace all content blocks with subcontent blocks. Note: currently messes with content_image tags!
$templ=str_replace('{content', '{subcontent pageindex='.$contentobj->Id()."", $data['template_content']);
$html=$smarty->fetch('string:'.$templ) ;
echo $html;
}
function smarty_function_subcontent($params, &$template)
{
if(empty($params['block']))
{$block='content_en';}
else
{$block=$params['block'];}
if(empty($params['pageindex']))
{echo 'missing arguments';}
else
{$pageindex=$params['pageindex'];}
$gCms = cmsms();
$db = $gCms->GetDb();
// Get a content prop, this could be improved by getting all content blocks for a page at once and by using caching
$q = "SELECT content FROM ".cms_db_prefix()."content_props
WHERE content_id =".((int)$pageindex)." AND prop_name = '".$block."' LIMIT 1";
$dbresult = $db->Execute( $q );
if( !$dbresult )
{
echo 'DB error: '. $db->ErrorMsg()."<br/>";
}
$data = $dbresult->FetchRow();
$cont=$data['content'];
$smarty = &$gCms->GetSmarty();
$smarty->display('string:'.$cont);
}
Usage:
Code: Select all
//primary template
<div>page1 {content} {content block='2'}</div>
<div>page2 {page_content alias='pageone'}</div>
<div>page3 {page_content alias='pagetwo'}</div>
//secondary template (for pageone and pagetwo but could be different too...):
<div>Column 1 {content}</div>
<div>Column 1 {content block='column2'}</div>
//becomes essentially this when page is parsed:
<div>page1 page one content block1 page one content block2</div>
<div>page2 <div>Column 1 page two content block1</div>
<div>Column 2 page two content block2</div></div>
<div>page3 <div>Column 1 page3 content block1</div>
<div>Column 2 page3 content block2</div></div>
Re: Any other ways to get page contents in UDT?
Posted: Thu Aug 30, 2012 9:44 pm
by fredp
Hi,
Here's a thought regarding your Smarty issues:
If you're using CMSMS 1.11+, then you might investigate new Smarty 3.x features to see if they can help. For example, the
createTemplate() and
createData() methods look interesting...
Excerpts from http://www.smarty.net wrote:Template Objects:
You can now make template objects and execute them independently. Example:
// create template object with its private variable scope
$tpl = $smarty->createTemplate('index.tpl');
// assign variable to template scope
$tpl->assign('foo','bar');
// display the template
$tpl->display();
Data Objects:
Data objects are used to create scopes for assigned variables. They can be used to have controll which variables are seen by which templates.
// create data object with its private variable scope
$data = $smarty->createData();
// assign variable to data scope
$data->assign('foo','bar');
// create template object which will use variables from data object
$tpl = $smarty->createTemplate('index.tpl',$data);
// display the template
$tpl->display();
I haven't had a chance to play with any of this yet, but it looks like these new features might prove useful.
Hope this helps,
fred p