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>