<![CDATA[ Snippet/Output Modifier get the value of H1 tag and and output to placeholder - My Forums]]> https://forums.modx.com/thread/?thread=95687 <![CDATA[Snippet/Output Modifier get the value of H1 tag and and output to placeholder]]> https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder?page=2#dis-post-517926

  1. Get the value of a H1 tag from the [[*content]]
  2. Remove the H1 tag including its value
  3. Output the value to a placeholder

I know we can get any value from a tv or the pagetitle field, but for this project the H1 has to be inside the [[*content]].

Any help would be much appreciated.]]>
mdehaan Jan 06, 2015, 03:46 AM https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder?page=2#dis-post-517926
<![CDATA[Re: Snippet/Output Modifier get the value of H1 tag and and output to placeholder]]> https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder?page=2#dis-post-517953
I'm using an unpublished resource with the content structure I need, duplicating it for each recipe. I haven't gotten Content Blocks under control yet, but once I do I won't need to duplicate an already structured resource. Content Blocks will provide the sections I need - intro, question, answer, discussion.]]>
sottwell Jan 06, 2015, 07:42 AM https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder?page=2#dis-post-517953
<![CDATA[Re: Snippet/Output Modifier get the value of H1 tag and and output to placeholder]]> https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder?page=2#dis-post-517951 Quote from: sottwell at Jan 06, 2015, 01:25 PM
Thank you for the excellent recipe, mdehaan and Bruno! http://modxcookbook.com/basics/resources/extract-part-of-content.html

I can go to the page from your link.
But I can't find it trough your navigation.
Maybe I'm not looking at the right place, or maybe you aren't done adding/writing....]]>
mdehaan Jan 06, 2015, 07:29 AM https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder?page=2#dis-post-517951
<![CDATA[Re: Snippet/Output Modifier get the value of H1 tag and and output to placeholder]]> https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder?page=2#dis-post-517950 http://modxcookbook.com/basics/resources/extract-part-of-content.html]]> sottwell Jan 06, 2015, 07:25 AM https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder?page=2#dis-post-517950 <![CDATA[Re: Snippet/Output Modifier get the value of H1 tag and and output to placeholder]]> https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder?page=2#dis-post-517940 mdehaan Jan 06, 2015, 05:19 AM https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder?page=2#dis-post-517940 <![CDATA[Re: Snippet/Output Modifier get the value of H1 tag and and output to placeholder]]> https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder#dis-post-517939 Quote from: Bruno17 at Jan 06, 2015, 11:05 AM
snippet 'extractH1fromContent':

<!--?php

$html = $modx--->resource->get('content');
$tag = 'h1';

preg_match_all('|<h[^>]+>(.*)</h[^>]+>|iU', $html, $headings);

foreach ($headings as $heading){
    $fullnode = isset($heading[0]) ? $heading[0] : '';
    $text = isset($heading[1]) ? $heading[1] : '';    
    if (strstr($fullnode,'h1')){
        $modx->setPlaceholder('h1.fullnode',$fullnode);
        $modx->setPlaceholder('h1.text',$text);
        $html = str_replace($fullnode,'',$html);
        break;    
    }
}

$modx->setPlaceholder('cleancontent',$html);


[[extractH1fromContent]]
[[+cleancontent]]
[[+h1.fullnode]]

WOW Bruno.

This works exactly like I imagined it would.
Thanx!
Can I make a donation somewhere?]]>
mdehaan Jan 06, 2015, 05:17 AM https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder#dis-post-517939
<![CDATA[Re: Snippet/Output Modifier get the value of H1 tag and and output to placeholder]]> https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder#dis-post-517938 Quote from: mdehaan at Jan 06, 2015, 10:04 AM
Quote from: sottwell at Jan 06, 2015, 09:53 AM
You could use a plugin to parse the content and get the <h1> DOM element and its content, providing there is only one H1 tag in the content. Then you could set the value of a placeholder with it.

To be honest, I don't see how this is going to work well. What, exactly, are you doing? If you need a dynamic value, use a TV. That's what they are for.</h1>

What I'm trying to achieve is to have the end user use the H1 tag from the WYSIWYG editor from the main content field.
This is easier to understand for them.

The problem is with the frontend.
Sometimes we want to have the H1 tag inside an other DIV than the one the other page content is in.

I know it would be much easier to use a TV for this, but would this be a bad idea or hard to achieve?

>>What I'm trying to achieve is to have the end user use the H1 tag from the WYSIWYG editor from the main content field.

- Here you wish the "visible" page title (not the <title></title> content) to be set in WYSIWYG editor. And so to be a part of [[*content]]?

What do you mean then by "Remove the H1 tag including its value"? If the user types in the text into the editor field and then sets it to be the H1 header - then the same user may remove it or change it into a paragraph or else.

>>Sometimes we want to have the H1 tag inside an other DIV than the one the other page content is in.

- The I would do it something like this (I am more into Evolution, so the example is in the Evolution style):

1) Create a TV (and assign it to the corresponding template):

Name: h1
Input Type: Check box
Input Option Values: Add H1=={{h1-content}}

Put [*h1*] in its place in a template (or in a chunk of a template)

2) Create another TV (and assign it to the corresponding template):

Name: h1-content-tv
Input Type: Text

3) Create a chunk

Name: h1-content
Chunk code: <h1>[*h1-content-tv*]</h1>

- So, there are two TV fields below editor on each page with a given template. The user enters a text into the "h1-content" field and checks it at "Add a H1" to have this H1 title displayed in its place on a page.]]>
viener Jan 06, 2015, 05:10 AM https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder#dis-post-517938
<![CDATA[Re: Snippet/Output Modifier get the value of H1 tag and and output to placeholder (Best Answer)]]> https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder#dis-post-517937
<?php

$html = $modx->resource->get('content');
$tag = 'h1';

preg_match_all('|<h[^>]+>(.*)</h[^>]+>|iU', $html, $headings);

foreach ($headings as $heading){
    $fullnode = isset($heading[0]) ? $heading[0] : '';
    $text = isset($heading[1]) ? $heading[1] : '';    
    if (strstr($fullnode,'h1')){
        $modx->setPlaceholder('h1.fullnode',$fullnode);
        $modx->setPlaceholder('h1.text',$text);
        $html = str_replace($fullnode,'',$html);
        break;    
    }
}

$modx->setPlaceholder('cleancontent',$html);


[[extractH1fromContent]]
[[+cleancontent]]
[[+h1.fullnode]]
]]>
Bruno17 Jan 06, 2015, 05:05 AM https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder#dis-post-517937
<![CDATA[Re: Snippet/Output Modifier get the value of H1 tag and and output to placeholder]]> https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder#dis-post-517936 http://modx.com/extras/package/getimagesfromhtml]]> mdehaan Jan 06, 2015, 04:50 AM https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder#dis-post-517936 <![CDATA[Re: Snippet/Output Modifier get the value of H1 tag and and output to placeholder]]> https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder#dis-post-517935 Quote from: Bruno17 at Jan 06, 2015, 10:08 AM
some start-points for a snippet:

http://kaspars.net/blog/web-development/regex-extract-headings-h1-h2-h3-from-html

and search for 'strip_selected_tags'
here:
http://php.net/manual/de/function.strip-tags.php

I'll look into that and try with my very limited PHP knowledge smiley]]>
mdehaan Jan 06, 2015, 04:27 AM https://forums.modx.com/thread/95687/snippet-output-modifier-get-the-value-of-h1-tag-and-and-output-to-placeholder#dis-post-517935