Commit | Line | Data |
---|---|---|
cd8826ea S |
1 | <?php |
2 | /** | |
3 | * Template prefiler plugin which allows inserting code dynamically upon the contents | |
4 | * of 'content'. | |
5 | * | |
6 | * Usage: | |
7 | * {hascontent} | |
8 | * <ul> | |
9 | * {content} | |
10 | * {if $foo}<li>bar</li>{/if} | |
11 | * {/content} | |
12 | * </ul> | |
13 | * {hascontentelse} | |
14 | * <p>baz</p> | |
15 | * {/hascontent} | |
16 | * | |
17 | * @author Alexander Ebert | |
18 | * @copyright 2001-2014 WoltLab GmbH | |
19 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
20 | * @package com.woltlab.wcf | |
21 | * @subpackage system.template.plugin | |
22 | * @category Community Framework | |
23 | */ | |
24 | ||
25 | function smarty_prefilter_hascontent($source, &$smarty) { | |
26 | $ldq = preg_quote($smarty->left_delimiter, '~'); | |
27 | $rdq = preg_quote($smarty->right_delimiter, '~'); | |
28 | ||
29 | $source = preg_replace_callback("~{$ldq}hascontent( assign='(?P<assign>.*)')?{$rdq}(?P<before>.*){$ldq}content{$rdq}(?P<content>.*){$ldq}\/content{$rdq}(?P<after>.*)({$ldq}hascontentelse{$rdq}(?P<else>.*))?{$ldq}\/hascontent{$rdq}~sU", function ($matches) { | |
30 | $beforeContent = $matches['before']; | |
31 | $content = $matches['content']; | |
32 | $afterContent = $matches['after']; | |
33 | $elseContent = (isset($matches['else'])) ? $matches['else'] : ''; | |
34 | $assignContent = (isset($matches['assign']) && !empty($matches['assign'])) ? $matches['assign'] : ''; | |
35 | $variable = 'hascontent_' . sha1(time()); | |
36 | ||
37 | $newContent = '{capture assign='.$variable.'}'.$content.'{/capture}'."\n"; | |
38 | $newContent .= '{assign var='.$variable.' value=$'.$variable.'|trim}'."\n"; | |
39 | ||
40 | if ($assignContent) $newContent .= '{capture assign='.$assignContent.'}'."\n"; | |
41 | $newContent .= '{if $'.$variable.'}'.$beforeContent.'{$'.$variable.'}'."\n".$afterContent; | |
42 | ||
43 | if (!empty($elseContent)) { | |
44 | $newContent .= '{else}'.$elseContent."\n"; | |
45 | } | |
46 | ||
47 | $newContent .= '{/if}'."\n"; | |
48 | ||
49 | if ($assignContent) $newContent .= "{/capture}\n{\$".$assignContent."}\n"; | |
50 | ||
51 | return $newContent; | |
52 | }, $source); | |
53 | ||
54 | return $source; | |
55 | } |