I have a list of links in a text area which I want to show up in a menu. The menu needs to show the 5 most recent links from the link page. I was thinking about the snippet ’ListIndexer’, but that wil only show recent pages which you created and not text. How can I achieve to show the recent text/links which I added in a content area?
No one? I want something similiar to the ditto snippet, but instead of documents I want to show links that are in a piece of text
you can try this:
<?php
/*
* call example:
* <ul>
* [!text2items? &tpl=`myitemchunk`&source=`mysourceTV`&display=`all`!]
* </ul>
*
* content example:
* FIELDNAMES:link||linktext
* www.link1.com||linktext1
* www.link2.com||linktext2
* www.link3.com||linktext3
* www.link4.com||linktext4
*
* chunk example:
* <li><a href="http://[+link+]">[+linktext+]</a></li>
*
* or:
* <li><a href="http://[+0+]">[+1+]</a></li>
* (this will work also without the first line in chunk: 'FIELDNAMES:.......')
*
* author: Bruno Perner - [email protected]
*
*/
$display = isset ($display)?$display:'3';
$sourceTV = isset ($source)?$source:'content';
$docid = isset ($docid)?$docid:null;
$tv = $modx->getTemplateVarOutput($sourceTV,$docid);
$text = $tv[$sourceTV];
$text = str_replace('\r', '', $text);
$text = preg_replace('/(?<!>)\n/', "#break#", $text);
$items = explode('#break#', $text);
$output = '';
if (isset($tpl)){
if (count($items) > 0)
{
$iteration = 0;
$fieldnames=array();
if (substr($items[0],0,11)=='FIELDNAMES:'){
$fieldnames=explode(':',$items[0]);
$fieldnames=explode('||',trim($fieldnames[1]));
unset($items[0]);
}
foreach ($items as $item)
{
if ($display !=='all'&&$iteration == $display) break;
$item=explode('||',trim($item));
$item['iteration']=$iteration;
if (count($fieldnames>0)){
foreach ($fieldnames as $key=>$fieldname){
$item[$fieldname]=$item[$key];
}
}
$output.=$modx->parseChunk($tpl,$item,'[+','+]');
$iteration++;
}
}
}
else{
$output= '<pre>'.print_r($items,true).'</pre>';
}
return $output;
?>
In my html I get
<li><a href="http://"></a></li>
But not the links that are in my TV. Which settings should I put in my TV?
use textarea-tv with example-content:
FIELDNAMES:link||linktext
www.link1.com||linktext1
www.link2.com||linktext2
www.link3.com||linktext3
www.link4.com||linktext4
mmh, doesn’t work.
Now I get this. But now I don’t have spaces after the `
Array
(
[0] =>
[!links?&tpl=`link`&source=`mysourceTV`&display=`all`!]
[1] =>
[2] =>
)
ok, had a little bug and have fixed it now. Can test it, if it works now.