Page 1 of 1

Split header and rest of content from content(); tplfunc

Posted: Wed May 14, 2014 5:04 pm
by learnandcode
Hi
Is there a chance to get those two as separate functions. This time I need get headers in other place than rest of entry.
Or just some function to return header and I will disable show header in content.

Re: Split header and rest of content from content(); tplfunc

Posted: Wed May 14, 2014 5:37 pm
by cmb
Hm, not sure if that could work as expected. You have to consider that content() does not only return the content of a page, but additionally potentially arbitrary output (plugins can manipulate the global $o, and at least for the plugin administration this is the usual technique).

However, all in all I agree, that CMSimple_XH should offer some generally useful functions regarding splitting content in header and "body". There are several routines that need this functionality (in the core and in plugins).

How to get a page heading can be seen in XH_saveEditorContents (cmsimple/admfuncs.php). Basically:

Code: Select all

function getHeading($text)
{
    global $cf;

    $hot = '<h[1-' . $cf['menu']['levels'] . '][^>]*>';
    $hct = '<\/h[1-' . $cf['menu']['levels'] . ']>'; // TODO: use $1 ?
    if (preg_match("/$hot(.+?)$hct/isu", $text, $matches)) {
        return $matches[1];
    } else {
        return false;
    }
} 

Re: Split header and rest of content from content(); tplfunc

Posted: Wed May 14, 2014 5:46 pm
by learnandcode
So should I use

Code: Select all

<?php echo getHeading(content());?> 
and append youre code to adminfuncs? If yes I have error but I will work on it later.
Thanks anyway.

Re: Split header and rest of content from content(); tplfunc

Posted: Wed May 14, 2014 5:51 pm
by cmb
Basically, yes. I would put the new function into cmsimple/userfuncs.php, however, so it will easily survive an update.
learnandcode wrote:If yes I have error but I will work on it later.
I have just fixed one bug: $cf wasn't "declared" as global. There might be others--I have not tested the code.

Re: Split header and rest of content from content(); tplfunc

Posted: Wed May 14, 2014 7:47 pm
by learnandcode
Thanks working like a charm.
You forgot the last bracket (for those that only copy and paste)

Re: Split header and rest of content from content(); tplfunc

Posted: Wed May 14, 2014 8:23 pm
by cmb
learnandcode wrote:Thanks working like a charm.
Fine. :)
learnandcode wrote:You forgot the last bracket
Oops! Fixed above now.

Re: Split header and rest of content from content(); tplfunc

Posted: Thu May 15, 2014 10:16 am
by learnandcode
Well. I have an issue.
This function working only when i leave heading in content. If I use blank alternative heading I receive void from your function. Any idea?
I cannot leave two h1 cause of crawlers (seo).

Re: Split header and rest of content from content(); tplfunc

Posted: Thu May 15, 2014 11:18 am
by cmb
learnandcode wrote:This function working only when i leave heading in content. If I use blank alternative heading I receive void from your function. Any idea?
If you set an alternative heading, the heading is not part of content(), and so getHeading() returns false. You have to leave the heading in (i.e. don't use an empty alternative heading), and use a similar function to remove the heading from content():

Code: Select all

function removeHeading($text)
{
    global $cf;

    $hot = '<h[1-' . $cf['menu']['levels'] . '][^>]*>';
    $hct = '<\/h[1-' . $cf['menu']['levels'] . ']>'; // TODO: use $1 ?
    return preg_replace("/$hot(.+?)$hct/isu", '', $text);
}
Call it in the template this way:

Code: Select all

<?php echo removeHeading(content());?>
If there are issues wrt. calling content() twice (performance or otherwise), you can cache the results of content() when you first call it and reuse the cached content when you call it the second time, e.g.:

Code: Select all

<?php echo getHeading($cachedContent = content());?>
<!-- further template markup -->
<?php echo removeHeading($cachedContent);?>
learnandcode wrote:I cannot leave two h1 cause of crawlers (seo).
I'm not sure if that's really an issue (see http://www.youtube.com/watch?v=GIn5qJKU8VM).