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

About the template and stylesheet - and changing the menu
Post Reply
learnandcode
Posts: 13
Joined: Sat Jan 07, 2012 8:37 pm
Location: Poland
Contact:

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

Post by learnandcode » Wed May 14, 2014 5:04 pm

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.

cmb
Posts: 13300
Joined: Tue Jun 21, 2011 11:04 am
Location: Mü-Sa, RLP, DE
Contact:

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

Post by cmb » Wed May 14, 2014 5:37 pm

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;
    }
} 
Last edited by cmb on Wed May 14, 2014 8:22 pm, edited 2 times in total.
Reason: fixed bugs in code
Christoph M. Becker – Plugins for CMSimple_XH

learnandcode
Posts: 13
Joined: Sat Jan 07, 2012 8:37 pm
Location: Poland
Contact:

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

Post by learnandcode » Wed May 14, 2014 5:46 pm

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.

cmb
Posts: 13300
Joined: Tue Jun 21, 2011 11:04 am
Location: Mü-Sa, RLP, DE
Contact:

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

Post by cmb » Wed May 14, 2014 5:51 pm

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.
Christoph M. Becker – Plugins for CMSimple_XH

learnandcode
Posts: 13
Joined: Sat Jan 07, 2012 8:37 pm
Location: Poland
Contact:

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

Post by learnandcode » Wed May 14, 2014 7:47 pm

Thanks working like a charm.
You forgot the last bracket (for those that only copy and paste)

cmb
Posts: 13300
Joined: Tue Jun 21, 2011 11:04 am
Location: Mü-Sa, RLP, DE
Contact:

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

Post by cmb » Wed May 14, 2014 8:23 pm

learnandcode wrote:Thanks working like a charm.
Fine. :)
learnandcode wrote:You forgot the last bracket
Oops! Fixed above now.
Christoph M. Becker – Plugins for CMSimple_XH

learnandcode
Posts: 13
Joined: Sat Jan 07, 2012 8:37 pm
Location: Poland
Contact:

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

Post by learnandcode » Thu May 15, 2014 10:16 am

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).

cmb
Posts: 13300
Joined: Tue Jun 21, 2011 11:04 am
Location: Mü-Sa, RLP, DE
Contact:

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

Post by cmb » Thu May 15, 2014 11:18 am

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).
Christoph M. Becker – Plugins for CMSimple_XH

Post Reply