Newlines bij replacement
Ik ben zelf een beetje aan het knutselen met een zelfgemaakte template engine. Jaja, Smarty blablabla is beter blablabla. Ik weet het, maar ik wil zelf ook een beetje wat presteren, dus lijkt mij dit wel een leuk iets (en is niet zo echt moeilijk).
Nu mijn vraag:
Wanneer ik dit uit voer:
Dan verwijdert ie braaf dit soort commentaar:
Het probleem is echter dat ik i.p.v. niks als replacement heb, juist allemaal \n\r's heb.
Ik krijg dan dit:
Als ik dan een regex maak om die \n\r's te replacen met niks, dan is het wel oke, maar dat gaat dan over de hele .tpl file, dus alles staat achter elkaar en dat wil ik niet.
Dus mijn vraag is, hoe kan ik multi-line comments vervangen door niks en dat er dan daadwerkelijk NIKS meer staat; ook geen newlines?
Nu mijn vraag:
Wanneer ik dit uit voer:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// knip knip, wat irrelevante code blabla
private function _comments($content)
{
$pattern[0] = '/\{\*(.*)\*!\}/s';
$pattern[1] = '/\{\*(.*)\*\}/s';
$pattern[2] = '/#(.*)#/';
$replacement = '';
$this->template = preg_replace($pattern,$replacement,$content);
return trim($this->template);
}
// knip knip, wat irrelevante code blabla
?>
// knip knip, wat irrelevante code blabla
private function _comments($content)
{
$pattern[0] = '/\{\*(.*)\*!\}/s';
$pattern[1] = '/\{\*(.*)\*\}/s';
$pattern[2] = '/#(.*)#/';
$replacement = '';
$this->template = preg_replace($pattern,$replacement,$content);
return trim($this->template);
}
// knip knip, wat irrelevante code blabla
?>
Dan verwijdert ie braaf dit soort commentaar:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
<body>
{* Multiline comments,
so multiple lines including -
enters
*!}
{* Other method for comments,
also allowing multilines. *}
# Single line comment #
<h3>Hello $name!</h3>
{* Multiline comments,
so multiple lines including -
enters
*!}
{* Other method for comments,
also allowing multilines. *}
# Single line comment #
<h3>Hello $name!</h3>
Het probleem is echter dat ik i.p.v. niks als replacement heb, juist allemaal \n\r's heb.
Ik krijg dan dit:
Als ik dan een regex maak om die \n\r's te replacen met niks, dan is het wel oke, maar dat gaat dan over de hele .tpl file, dus alles staat achter elkaar en dat wil ik niet.
Dus mijn vraag is, hoe kan ik multi-line comments vervangen door niks en dat er dan daadwerkelijk NIKS meer staat; ook geen newlines?
Gewijzigd op 01/01/1970 01:00:00 door GaMer B
Gesponsorde koppelingen:
Voor Windows is het \r\n :D
Je kunt beter even alle vreemde enters vervangen door Linux enters.
Nu kun je veel makelijker werken met enters. Ook scheelt het wat aan ruimte als er in de tekst ook Windows enters stonden.
edit:
Je enters knoppen trouwens precies. Na een blok met kommentaar zou je ook een eventuele enter kunnen verwijderen.
'/\{\*(.*)\*!\}\n?/s'
edit2:
De bovenste regex noemen ze een greedy regex. Dit betekent dat dit "{*...*}...{* ...*}" helemaal gezien wordt als kommentaar.
En je wil dat het gezien moet worden als twee blokken kommentaar. Dat doe je zo:
'/\{\*(.*?)\*!\}\n?/s'
Je kunt beter even alle vreemde enters vervangen door Linux enters.
Nu kun je veel makelijker werken met enters. Ook scheelt het wat aan ruimte als er in de tekst ook Windows enters stonden.
edit:
Je enters knoppen trouwens precies. Na een blok met kommentaar zou je ook een eventuele enter kunnen verwijderen.
'/\{\*(.*)\*!\}\n?/s'
edit2:
De bovenste regex noemen ze een greedy regex. Dit betekent dat dit "{*...*}...{* ...*}" helemaal gezien wordt als kommentaar.
En je wil dat het gezien moet worden als twee blokken kommentaar. Dat doe je zo:
'/\{\*(.*?)\*!\}\n?/s'
Gewijzigd op 01/01/1970 01:00:00 door Martijn B
Ik heb de regex aangepast.
Maar als ik:
toepas, dan blijft het gewoon zo.
De .tpl file:
De output:
Zoals je ziet, zijn de newlines nog steeds aanwezig.
Maar als ik:
toepas, dan blijft het gewoon zo.
De .tpl file:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<html>
<head>
<title>Made by $name</title>
{css}
body
{
font-family: Verdana;
}
{!css}
</head>
<body>
{* Multiline comments,
so multiple lines including -
enters
*!}
# Single line comment #
# Another one #
<h3>Hello $name!</h3>
<p>The time is: $datetime</p>
<?php echo 'PHP'; ?><br />
{js}
document.write('Hello from JavaScript');
{!js}
{include: test.php}
</body>
</html>
<head>
<title>Made by $name</title>
{css}
body
{
font-family: Verdana;
}
{!css}
</head>
<body>
{* Multiline comments,
so multiple lines including -
enters
*!}
# Single line comment #
# Another one #
<h3>Hello $name!</h3>
<p>The time is: $datetime</p>
<?php echo 'PHP'; ?><br />
{js}
document.write('Hello from JavaScript');
{!js}
{include: test.php}
</body>
</html>
De output:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<html>
<head>
<title>Made by Flip van Rijn</title>
<style type="text/css">
body
{
font-family: Verdana;
}
</style>
</head>
<body>
<h3>Hello Flip van Rijn!</h3>
<p>The time is: 13/11/07</p>
PHP<br />
<script type="text/javascript">
document.write('Hello from JavaScript');
</script>
<br />Test file included.<br />0<br />
1<br />
2<br />
3<br />
4<br />
5<br />
6<br />
7<br />
8<br />
9<br />
10<br />
</body>
</html>
<head>
<title>Made by Flip van Rijn</title>
<style type="text/css">
body
{
font-family: Verdana;
}
</style>
</head>
<body>
<h3>Hello Flip van Rijn!</h3>
<p>The time is: 13/11/07</p>
PHP<br />
<script type="text/javascript">
document.write('Hello from JavaScript');
</script>
<br />Test file included.<br />0<br />
1<br />
2<br />
3<br />
4<br />
5<br />
6<br />
7<br />
8<br />
9<br />
10<br />
</body>
</html>
Zoals je ziet, zijn de newlines nog steeds aanwezig.
Het ziet er naar uit dat de laatste enter niet gepakt word.
Hoe ziet je code er nu uit?
edit:
Hier wat code uit mijn template engine:
Dit zou toch moeten werken...
Hoe ziet je code er nu uit?
edit:
Hier wat code uit mijn template engine:
Code (php)
1
2
3
4
5
6
7
2
3
4
5
6
7
<?
# Vervang Windows and Macintosh enters met Unix-style enters
$sContent = str_replace(array("\r\n", "\r"), "\n", $sContent);
# Verwijder template kommentaar
$sContent = preg_replace('/\{ *\*.*?\* *\}\n?/s', null, $sContent);
?>
# Vervang Windows and Macintosh enters met Unix-style enters
$sContent = str_replace(array("\r\n", "\r"), "\n", $sContent);
# Verwijder template kommentaar
$sContent = preg_replace('/\{ *\*.*?\* *\}\n?/s', null, $sContent);
?>
Dit zou toch moeten werken...
Gewijzigd op 01/01/1970 01:00:00 door Martijn B
In het kort:
lib/QTemplates.class.php:
Index.php:
lib/QTemplates.class.php:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
class Template
{
public $template;
/**
* @Description Nothing yet
* @Param Nothing yet
*/
function __constructor()
{
}
/**
* @Description Load a template file into the Template parser
* @Param $filepath: requires the file which needs to be parsed
*/
function load($filename)
{
$this->template = file_get_contents('templates/'.$filename);
}
/**
* @Description Returns the parsed template and optionally executes PHP code in the template
* @Param N/a
*/
function publish()
{
# Parser modules
$this->_comments($this->template); // Remove comments
$this->_cssStyle($this->template); // Add CSS style to template
$this->_javascript($this->template); // Add javascript to template
$this->_includeFile($this->template); // Include file in template
}
/**
* Parser modules
*/
private function _comments($content)
{
$pattern[0] = '/\{\*(.*?)\*!\}\n?/s';
$pattern[1] = '/#(.*?)#\n?/';
$replacement = '';
$this->template = preg_replace($pattern,$replacement,$content);
$this->template = str_replace(array("\r\n", "\r"), "\n", $this->template);
return trim($this->template);
}
// En nog een paar private functions
?>
class Template
{
public $template;
/**
* @Description Nothing yet
* @Param Nothing yet
*/
function __constructor()
{
}
/**
* @Description Load a template file into the Template parser
* @Param $filepath: requires the file which needs to be parsed
*/
function load($filename)
{
$this->template = file_get_contents('templates/'.$filename);
}
/**
* @Description Returns the parsed template and optionally executes PHP code in the template
* @Param N/a
*/
function publish()
{
# Parser modules
$this->_comments($this->template); // Remove comments
$this->_cssStyle($this->template); // Add CSS style to template
$this->_javascript($this->template); // Add javascript to template
$this->_includeFile($this->template); // Include file in template
}
/**
* Parser modules
*/
private function _comments($content)
{
$pattern[0] = '/\{\*(.*?)\*!\}\n?/s';
$pattern[1] = '/#(.*?)#\n?/';
$replacement = '';
$this->template = preg_replace($pattern,$replacement,$content);
$this->template = str_replace(array("\r\n", "\r"), "\n", $this->template);
return trim($this->template);
}
// En nog een paar private functions
?>
Index.php:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
/**
* @author Flip van Rijn
* @copyright 2007
* @Filename index.php
* @Description An example page to use the QTemplates.class.php class
*/
require 'lib/QTemplates.class.php';
$template = new Template;
$template->load('example.tpl');
$template->assign('name', 'Flip van Rijn');
$template->assign('company', 'Voirea');
$template->assign('datetime', date('d/m/y'));
$template->publish();
?>
/**
* @author Flip van Rijn
* @copyright 2007
* @Filename index.php
* @Description An example page to use the QTemplates.class.php class
*/
require 'lib/QTemplates.class.php';
$template = new Template;
$template->load('example.tpl');
$template->assign('name', 'Flip van Rijn');
$template->assign('company', 'Voirea');
$template->assign('datetime', date('d/m/y'));
$template->publish();
?>
Edit:
Code fix
Gewijzigd op 01/01/1970 01:00:00 door GaMer B
Zou ik in de methode load doen.
edit:
function __constructor() ???
Moet worden
function __construct()
toch?
Gewijzigd op 01/01/1970 01:00:00 door Martijn B
Ah, kijk aan :) Hij doet het. Dankjewel.
Edit:
Nu je het zegt... klopt inderdaad. Het moet __construct() zijn.
Quote:
edit:
function __constructor() ???
Moet worden
function __construct()
toch?
function __constructor() ???
Moet worden
function __construct()
toch?
Nu je het zegt... klopt inderdaad. Het moet __construct() zijn.
Gewijzigd op 01/01/1970 01:00:00 door GaMer B



