login  Naam:   Wachtwoord: 
Registreer je!
 Forum

[Opgelost] ereg

Offline haytjes - 13/04/2005 21:54 (laatste wijziging 15/04/2005 22:47)
Avatar van haytjesGouden medailleGouden medaille

JS gevorderde
hoi iedereen,

ik heb verstaan dat je met ereg het volgende kan

Citaat:
$template = "
<table>
<tr>
[block=tr]
<td>
blalalaal
</td>
</tr>
[/block=tr]
</table>";


en 'k zou dus willen dat hij die block eruit haalt in een andere variable zet.

dus

Citaat:
$template = "
<table>
<tr>
[block=tr]
</table>";

$block['tr'] = '
[block=tr]
<td>
blalalaal
</td>
</tr>
[/block=tr]';


bedankt en groetjes op voorhand

3 antwoorden

Gesponsorde links
Offline EliTe - 14/04/2005 13:36 (laatste wijziging 14/04/2005 13:36)
Avatar van EliTe Onbekend Ik snap niet exact wat je bedoelt. Misschien staat het in Deze tutorial?
Offline haytjes - 15/04/2005 22:46
Avatar van haytjes Gouden medailleGouden medaille

JS gevorderde
sorry voor dit laat reageren,

maar 't is iest ingewikkeld, waarbij je dus met verschillende soorten werk en daarmee heb ik mijn begin post wat kort gehouden en heb ik zelf wat verder gezocht.

Dank aan "Smarty", heb bij hen afgekeken.
'k ben eigenlijk ook een template engine aan het maken.
Maar alleen met blocken.
Volgens mij kan een block en een variable alles aan.
En daamee wou ik smary zelf ook niet nemen.
Te veel.

toch bedankt, vooral 'EliTe' dan;-)

groetjes
Offline Fenrir - 15/04/2005 22:56 (laatste wijziging 15/04/2005 22:58)
Avatar van Fenrir PHP expert Ik heb zelf ook eens een TemplateEngine gemaakt. Het werkt met blocks, maar het wordt gecompileerd als bij Smarty.

De code (lang?):

  1. <?php
  2. /**
  3. * FTemplate class
  4. * Author: Fenrir
  5. * ToDo: Description Finished? Order
  6. * -------------------------------------------------------------------------------------------------
  7. * | - Change recurse to while($var = $block['recurse']->next()) [ ] ++ |
  8. * | - Include files [ ] +++ |
  9. * | - If's [ ] ++ |
  10. * | - Foreach (for recurse) [ ] ++ |
  11. * | - Limit recurse: [RECURSE: DEEPER_LEVEL : 3] [+] - |
  12. * | - Blocks [+] - |
  13. * | - Trees (recurse) [+] - |
  14. * | - Cache [ ] ++ |
  15. * | - Compile [+] - |
  16. * -------------------------------------------------------------------------------------------------
  17. * Copyright by Fenrir 2005
  18. */
  19.  
  20. class FT_Node
  21. {
  22.  
  23. /**
  24. * An array that holds the variables for this node
  25. */
  26. var $vars = array();
  27.  
  28. /**
  29. * An array that holds the children for this node
  30. */
  31. var $items = array();
  32.  
  33. /**
  34. * Constructor
  35. * Sets the variables for this node that are in $vars
  36. */
  37. function FT_Node($vars = array())
  38. {
  39. $this->vars = $vars;
  40. }
  41.  
  42. /**
  43. * Adds a new child-node to this node
  44. */
  45. function &addItem(&$item)
  46. {
  47. $this->items[] = &$item;
  48. return $this->items[count($this->items) - 1];
  49. }
  50.  
  51. function toArray()
  52. {
  53. $arr = array();
  54. $arr['VARS'] = $this->vars;
  55. $arr['CHILDREN'] = array();
  56.  
  57. foreach($this->items as $item)
  58. {
  59. $arr['CHILDREN'][] = $item->toArray();
  60. }
  61.  
  62. return $arr;
  63. }
  64.  
  65. }
  66.  
  67. class FT_Recurse
  68. {
  69. /**
  70. * An array that holds the children for this recurse
  71. */
  72. var $items = array();
  73.  
  74. function FT_Node()
  75. {
  76. }
  77.  
  78. function &addItem(&$item)
  79. {
  80. $this->items[] = &$item;
  81. return $this->items[count($this->items) - 1];
  82. }
  83.  
  84. function toArray()
  85. {
  86. $arr = array();
  87.  
  88. foreach($this->items as $item)
  89. {
  90. $arr[] = $item->toArray();
  91. }
  92.  
  93. return $arr;
  94. }
  95. }
  96.  
  97. function recurse($array, $set_begin, $set_end, $item_begin, $item_end, $maxdepth = 99, $currdepth = 0)
  98. {
  99. if($currdepth < $maxdepth)
  100. {
  101. echo $set_begin;
  102.  
  103. foreach($array as $item)
  104. {
  105.  
  106. $item_begin2 = $item_begin;
  107. $item_end2 = $item_end;
  108.  
  109. foreach($item['VARS'] as $var => $content)
  110. {
  111. $item_begin2 = str_replace('{recurse_var: '.$var.'}', $content, $item_begin2);
  112. $item_end2 = str_replace('{recurse_var: '.$var.'}', $content, $item_end2);
  113. }
  114.  
  115. $item_begin2 = preg_replace('/\{recurse_var: ([a-zA-Z0-9]*)\}/', '', $item_begin2);
  116. $item_end2 = preg_replace('/\{recurse_var: ([a-zA-Z0-9]*)\}/', '', $item_end2);
  117.  
  118. echo $item_begin2;
  119.  
  120. if(!empty($item['CHILDREN']))
  121. {
  122. recurse($item['CHILDREN'], $set_begin, $set_end, $item_begin, $item_end, $maxdepth, $currdepth + 1);
  123. }
  124.  
  125. echo $item_end2;
  126.  
  127. }
  128.  
  129. echo $set_end;
  130. }
  131. }
  132.  
  133. function recurse_arg($str)
  134. {
  135. $str = str_replace("\\", "\\\\", $str);
  136. $str = str_replace('"', '\"', $str);
  137. $str = preg_replace('/\[([a-zA-Z0-9]*)\]/', '" . @\$constants["\\1"] . "', $str);
  138. $str = preg_replace('/\{([a-zA-Z0-9]*)\}/', '{recurse_var: \\1}', $str);
  139.  
  140. return $str;
  141. }
  142.  
  143. class FTemplate
  144. {
  145.  
  146. # The non-compiled templatefile
  147. var $TemplateFile = '';
  148.  
  149. # The cached (compiled) templatefile
  150. var $CompiledTemplateFile = '';
  151.  
  152. # The cached config
  153. var $CompiledTemplateConfigFile = '';
  154.  
  155. # The cache-dir
  156. var $CacheDir = 'cache/';
  157.  
  158. # The contents of the non-compiled template
  159. var $Template = '';
  160.  
  161. # The templatedata (blocks and vars)
  162. var $TemplateData = array(
  163. 'BLOCKS' => array(),
  164. 'VARS' => array()
  165. );
  166.  
  167. # The constants
  168. var $constants = array();
  169.  
  170. # The contents of the compiled template
  171. var $CompiledTemplate = '';
  172.  
  173. # The parsed template
  174. var $ParsedTemplate = '';
  175.  
  176. # Is the template compiled yet?
  177. var $Compiled = false;
  178.  
  179. # Is the template parsed yet?
  180. var $Parsed = false;
  181.  
  182. # The options array
  183. var $Options = array(
  184. 'forcecompile' => false
  185. );
  186.  
  187. # The blocks
  188. var $blocks = array();
  189.  
  190. # The current block
  191. var $currBlock = array();
  192.  
  193.  
  194.  
  195. # FTemplate($Template):
  196. # Constructor
  197. # $Template contains the path to the templatefile
  198.  
  199. function FTemplate($Template, $options = array())
  200. {
  201. $this->currBlock = &$this->TemplateData;
  202.  
  203. foreach($options as $Opt => $OptVal)
  204. {
  205. $this->Options[$Opt] = $OptVal;
  206. }
  207.  
  208. if(is_file($Template))
  209. {
  210. $this->TemplateFile = $Template;
  211. $this->CompiledTemplateFile = md5($Template) . '_TEMLPATE.ctpl';
  212. $this->CompiledTemplateConfigFile = md5($Template) . '_CONFIG.ctpl';
  213.  
  214. //echo 'TEMPLATE MOD TIME: ' . filemtime($this->TemplateFile) . '<br />';
  215. //echo 'CTEMPLATE MOD TIME: ' . filemtime($this->CacheDir . $this->CompiledTemplateFile) . '<br />';
  216.  
  217. if(!is_file($this->CacheDir . $this->CompiledTemplateConfigFile) ||
  218. filemtime($this->CacheDir . $this->CompiledTemplateConfigFile) < filemtime($this->TemplateFile) ||
  219. !is_file($this->CacheDir . $this->CompiledTemplateFile) ||
  220. filemtime($this->CacheDir . $this->CompiledTemplateFile) < filemtime($this->TemplateFile) ||
  221. $this->Options['forcecompile'])
  222. {
  223. #The template isn't compiled yet, compile it now
  224. $this->Template = file_get_contents($this->TemplateFile);
  225. $this->Compile();
  226. $this->CacheCompiled();
  227. }else
  228. {
  229. # The template is already compiled
  230. $this->CompiledTemplate = file_get_contents($this->CacheDir . $this->CompiledTemplateFile);
  231. $this->blocks = unserialize(file_get_contents($this->CacheDir . $this->CompiledTemplateConfigFile));
  232. $this->Compiled = true;
  233. }
  234. }else
  235. {
  236. $this->Error('The Templatefile doesn\'t exist!');
  237. }
  238. }
  239.  
  240. function Compile()
  241. {
  242. if(!empty($this->Template))
  243. {
  244. $Template = str_replace('<?', '&lt?', str_replace('?>', '?&gt', $this->Template));
  245.  
  246. preg_match_all('#\[START RECURSE : ([A-Za-z0-9_\-]+)\]
  247. ((?s).*)
  248. \[RECURSE : START_ITEM\]((?s).*)\[RECURSE : DEEPER_LEVEL : ([0-9]+)\]((?s).*)\[RECURSE : END_ITEM\]
  249. ((?s).*)
  250. \[END RECURSE : \\1\]#i', $Template, $matches);
  251.  
  252. $search = $matches[0];
  253.  
  254. $replace = array();
  255.  
  256. $count = count($matches[0]);
  257. for($i = 0; $i < $count; $i++)
  258. {
  259. $replace[] = '<?php @recurse($block["VARS"]["' . $matches[1][$i] . '"], "'
  260. . recurse_arg($matches[2][$i]) . '", "'
  261. . recurse_arg($matches[6][$i]) . '", "'
  262. . recurse_arg($matches[3][$i]) . '", "'
  263. . recurse_arg($matches[5][$i]) . '", ' .
  264. $matches[4][$i] . '); ?>';
  265. }
  266.  
  267. $Template = str_replace($search, $replace, $Template);
  268.  
  269.  
  270.  
  271. # The Variables
  272. $Template = preg_replace('/\{([a-zA-Z0-9]*)\}/', '<?=@\$block["VARS"]["\\1"]?>', $Template);
  273.  
  274. # The constants
  275. $Template = preg_replace('/\[([a-zA-Z0-9]*)\]/', '<?=@\$constants["\\1"]?>', $Template);
  276.  
  277.  
  278. # The templatearray
  279. $tplarray = array();
  280.  
  281. preg_match_all('/\[(START|END) BLOCK : ([A-Za-z0-9_\-]+)\]/', $Template, $parts);
  282.  
  283. # Array to hold the blocks that are currently open
  284. $openBlocks = array();
  285.  
  286. # Array that will be stored in the conpiledtemplate_config file
  287. $blocks = array();
  288.  
  289. # Validate the blocks
  290. $count = count($parts[0]);
  291. for($i = 0; $i < $count; ++$i)
  292. {
  293. $blockName = $parts[2][$i];
  294. $blockType = $parts[1][$i];
  295.  
  296. if($blockType == 'START')
  297. {
  298. if(!in_array($blockName, $openBlocks))
  299. {
  300.  
  301. $blocks[$blockName] = $openBlocks;
  302.  
  303. $openBlocks[] = $blockName;
  304. }else
  305. {
  306. $this->Error('There are two or more blocks with the same name!');
  307. }
  308. }else
  309. {
  310. if($openBlocks[count($openBlocks) - 1] == $blockName)
  311. {
  312. array_pop($openBlocks);
  313. }else
  314. {
  315. $this->Error('The blocks are not properly nested!');
  316. }
  317. }
  318. }
  319.  
  320. $this->blocks = $blocks;
  321.  
  322. if(count($openBlocks) != 0)
  323. {
  324. $this->Error('There are blocks that are not closed!', 2);
  325. }
  326.  
  327. # Compile the template
  328.  
  329. # The array with the tag-strings
  330. $search = array();
  331.  
  332. # The array with the php-code strings
  333. $replace = array();
  334.  
  335. $count = count($parts[0]);
  336. for($i = 0; $i < $count; ++$i)
  337. {
  338. $blockName = $parts[2][$i];
  339. $blockType = $parts[1][$i];
  340. $blockTag = $parts[0][$i];
  341.  
  342. $search[] = $blockTag;
  343.  
  344. if($blockType == 'START')
  345. {
  346.  
  347. $replace[] = '<?php
  348. if(isset($block["BLOCKS"]["' . $blockName . '"])):
  349. foreach($block["BLOCKS"]["' . $blockName . '"] as $block):
  350. ?>';
  351. }else
  352. {
  353. $replace[] = '<?php
  354. endforeach;
  355. endif;
  356. ?>';
  357. }
  358. }
  359.  
  360. $Template = str_replace($search, $replace, $Template);
  361.  
  362. $this->CompiledTemplate = $Template;
  363. $this->Compiled = true;
  364. $this->Config = serialize($blocks);
  365.  
  366. }else
  367. {
  368. $this->Error('The Template isn\'t set!');
  369. }
  370. }
  371.  
  372. function CacheCompiled()
  373. {
  374. if($this->Compiled)
  375. {
  376. $h = fopen($this->CacheDir . $this->CompiledTemplateFile, 'w');
  377.  
  378. fwrite($h, $this->CompiledTemplate);
  379. fclose($h);
  380.  
  381. $h = fopen($this->CacheDir . $this->CompiledTemplateConfigFile, 'w');
  382. fwrite($h, $this->Config);
  383. fclose($h);
  384. }else
  385. {
  386. $this->Error('The Template isn\'t compiled yet!');
  387. }
  388. }
  389.  
  390. function GetTemplate()
  391. {
  392. if(empty($this->Template))
  393. {
  394. $this->Error('The Template isn\'t set!');
  395. }else
  396. {
  397. return $this->Template;
  398. }
  399. }
  400.  
  401. function GetCompiledTemplate()
  402. {
  403. if(empty($this->CompiledTemplate))
  404. {
  405. $this->Error('The Compiled Template isn\'t set!');
  406. }else
  407. {
  408. return $this->CompiledTemplate;
  409. }
  410. }
  411.  
  412. function GetTemplateData()
  413. {
  414. return $this->blocks;
  415. }
  416.  
  417. function newBlock($block)
  418. {
  419.  
  420. $pblock = &$this->TemplateData;
  421.  
  422. if(isset($this->blocks[$block]))
  423. {
  424. $blockparents = $this->blocks[$block];
  425. }else
  426. {
  427. $this->Error('The block <i>"' . $block . '"</i> doesn\'t exist in the template <i>"' . $this->TemplateFile . '"</i>.');
  428. }
  429.  
  430. foreach($blockparents as $pname)
  431. {
  432. if(!isset($pblock['BLOCKS'][$pname]))
  433. {
  434. $pblock['BLOCKS'][$pname] = array();
  435. }
  436.  
  437. $pblock = &$pblock['BLOCKS'][$pname][count($pblock['BLOCKS'][$pname]) - 1];
  438. }
  439.  
  440. if(!isset($pblock['BLOCKS'][$block]))
  441. {
  442. $pblock['BLOCKS'][$block] = array();
  443. }
  444.  
  445. $pblock = &$pblock['BLOCKS'][$block][count($pblock['BLOCKS'][$block])];
  446.  
  447. $this->currBlock = &$pblock;
  448.  
  449. }
  450.  
  451. function Assign($var, $content)
  452. {
  453. $this->currBlock['VARS'][$var] = $content;
  454. }
  455.  
  456. function AssignConstant($var, $content)
  457. {
  458. $this->constants[$var] = $content;
  459. }
  460.  
  461. function AssignTree($var, $content)
  462. {
  463. $this->currBlock['VARS'][$var] = $content->toArray();
  464. }
  465.  
  466. function Parse()
  467. {
  468.  
  469. if($this->Compiled)
  470. {
  471. ob_start();
  472. $block = &$this->TemplateData;
  473. $constants = &$this->constants;
  474. include($this->CacheDir . $this->CompiledTemplateFile);
  475. $this->ParsedTemplate = ob_get_contents();
  476. ob_end_clean();
  477. }else
  478. {
  479. $this->Error('The template isn\'t compiled yet!');
  480. }
  481.  
  482. $this->Parsed = true;
  483. }
  484.  
  485. function PrintToScreen()
  486. {
  487. if($this->Parsed)
  488. {
  489. echo $this->ParsedTemplate;
  490. }else
  491. {
  492. if($this->Compiled)
  493. {
  494. $block = &$this->TemplateData;
  495. $constants = &$this->constants;
  496. include($this->CacheDir . $this->CompiledTemplateFile);
  497. }else
  498. {
  499. $this->Error('The template isn\'t compiled yet!');
  500. }
  501. }
  502. }
  503.  
  504. function GetParsedTemplate()
  505. {
  506. if($this->Parsed)
  507. {
  508. return $this->ParsedTemplate;
  509. }else
  510. {
  511. $this->Parse();
  512. return $this->ParsedTemplate;
  513. }
  514. }
  515.  
  516. function Error($msg, $eLevel = 1)
  517. {
  518. if($eLevel == 2)
  519. {
  520. die($msg);
  521. }else
  522. {
  523. die($msg);
  524. }
  525. }
  526. }
  527.  
  528. /**
  529. * Copyright by Fenrir 2005
  530. */
  531. ?>


Template:
  1. <html>
  2. <head>
  3. <title>{title}</title>
  4. </head>
  5. <body>
  6.  
  7. Constant Imagedir: [ImageDir] (This is set in ALL blocks and subblocks!)<br />
  8.  
  9. [START BLOCK : Chapter]
  10. <h2>{title}</h2>
  11. [START BLOCK : Paragraph]
  12. <h4>{title}</h4>
  13. <p>{content}</p>
  14. [END BLOCK : Paragraph]
  15. [END BLOCK : Chapter]
  16.  
  17. [START RECURSE : Tree]
  18. <ul>
  19. [RECURSE : START_ITEM]
  20. <li>
  21. <h4>{title}</h4>
  22. [RECURSE : DEEPER_LEVEL : 999]
  23. </li>
  24. [RECURSE : END_ITEM]
  25. </ul>
  26. [END RECURSE : Tree]
  27.  
  28. </body>
  29. </html>

PHP:
  1. <?php
  2.  
  3. include('includes/FTemplate.class.php');
  4.  
  5. # Create a new template-object
  6. $tpl = new FTemplate('templates/Template.tpl', array('forcecompile' => true));
  7.  
  8. # Set the page-title
  9. $tpl->assign('title', 'FTemplate');
  10.  
  11. # Set the image-dir
  12. $tpl->assignConstant('ImageDir', '/images/');
  13.  
  14. # Create a new chapter
  15. $tpl->newBlock('Chapter');
  16. $tpl->assign('title', 'Chapter 1');
  17.  
  18. # Create a new paragraph in chapter 1
  19. $tpl->newBlock('Paragraph');
  20. $tpl->assign('title', 'Paragraph 1');
  21. $tpl->assign('content', 'Lorem ipsum, dolor sit amet...');
  22.  
  23. # Create a new paragraph in chapter 1
  24. $tpl->newBlock('Paragraph');
  25. $tpl->assign('title', 'Paragraph 2');
  26. $tpl->assign('content', 'Lorem ipsum, dolor sit amet...');
  27.  
  28. # Create a new chapter
  29. $tpl->newBlock('Chapter');
  30. $tpl->assign('title', 'Chapter 2');
  31.  
  32. # Create a new paragraph in chapter 2
  33. $tpl->newBlock('Paragraph');
  34. $tpl->assign('title', 'Paragraph 1');
  35. $tpl->assign('content', 'Lorem ipsum, dolor sit amet...');
  36.  
  37. # Create a new paragraph in chapter 2
  38. $tpl->newBlock('Paragraph');
  39. $tpl->assign('title', 'Paragraph 2');
  40. $tpl->assign('content', 'Lorem ipsum, dolor sit amet...');
  41.  
  42. # Create a new tree
  43. $tree = new FT_Recurse();
  44.  
  45. # Create three nodes
  46. $node1 = new FT_Node(array('title' => 'Node1'));
  47. $node2 = new FT_Node(array('title' => 'Node2'));
  48. $node3 = new FT_Node(array('title' => 'Node3'));
  49.  
  50. # Create three subnodes for node 1
  51. $sub11 = new FT_Node(array('title' => 'SubNode1'));
  52. $sub12 = new FT_Node(array('title' => 'SubNode2'));
  53. $sub13 = new FT_Node(array('title' => 'SubNode3'));
  54.  
  55. # Create two subnodes for subnode 2
  56. $sub121 = new FT_Node(array('title' => 'SubSubNode1'));
  57. $sub122 = new FT_Node(array('title' => 'SubSubNode2'));
  58. $sub123 = new FT_Node(array('title' => 'SubSubNode3'));
  59.  
  60. # Assign the subsubnodes to subnode 2
  61. $sub12->addItem($sub121);
  62. $sub12->addItem($sub122);
  63. $sub12->addItem($sub123);
  64.  
  65. # Assign the subnode to node 1
  66. $node1->addItem($sub11);
  67. $node1->addItem($sub12);
  68. $node1->addItem($sub13);
  69.  
  70. # Assign the nodes/subnodes to the tree
  71. $tree->addItem($node1);
  72. $tree->addItem($node2);
  73. $tree->addItem($node3);
  74.  
  75. # Assign the tree to the template
  76. $tpl->assignTree('Tree', $tree);
  77.  
  78. # Output the template
  79. $tpl->PrintToScreen();
  80.  
  81. ?>
Gesponsorde links
Dit onderwerp is gesloten.
Actieve forumberichten
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.233s