Welcome Guest, Not a member yet? Register   Sign In
Template Parser Class Improvement
#11

[eluser]adamp1[/eluser]
That looks quite a good feature as you said if you want to offer more features for a user. The only little problem I can see is you haven't used $this->l_delim & $this->r_delim for the brackets. Could cause problems if people decide to change it from the default.

For Example:
Code:
function conditionals($template, $show_errors)
{
  if (preg_match_all('#'.$this->l_delim.'if (.+)'.$this->r_delim.'(.+)'.$this->l_delim.'/if'.$this->r_delim.'#sU', $template, $conditionals, PREG_SET_ORDER))
  {
    ...
  }
  ...
}

One other thing with the following bit.
Code:
// Do they us to hide the errors?
if (!preg_match_all('#{if (.+)}(.+){/if}#sU', $template, $conditionals, PREG_SET_ORDER) AND !$show_errors)
{
   ...
}
If some {if statements} are not replaced then the other variables which are not replaced won't be deleted? Even if show errors is FALSE.

Apart from that seems good, I like the show errors bit, would be good for debuging.
#12

[eluser]isaiahdw[/eluser]
Thanks for taking a look at it adamp1!

I've made some updates, per your suggestions, you can find them here: http://www.iitechs.com/MY_Parser.phps

I'm having one problem with it now, it seems like the line below deletes all the output - not just the stuff it should. Do we need this line?

Code:
$template = preg_replace ("(".$this->l_delim.$value.$this->r_delim."((?!".$this->l_delim.$value.$this->r_delim.")|.|[\r\n])*".$this->l_delim."/".$value.$this->r_delim.")", "", $template);
#13

[eluser]adamp1[/eluser]
What that line does is as follows.

It looks for all variable pair tags which have not been used and deletes all the content between them. For example:
Code:
{code}
  Line: {line}
{/code}
If the above was not parsed then it would remove everything between the {code}..{/code}. This is needed since otherwise if the user implements recurring content like above but dosn't set any data for it then only the variable tags will be removed and not inner text. For example:

Say we have the same code as above, but we remove the line you mention and make the following str_replace accept the '/' char as well. Then the following would occur.
Code:
Line:

As you can see all tags would be removed but that 'Line:' should also not be there since it was part of the {code} tags.

Im taking it that the reason nothing is outputted in your solution is that your conditional method does not remove the {if} tags around the content and is relying on mine to do this. But mine assumes that any tags left at this point are not wanted and removes all.

The only way to solve this is either make your conditional method remove the {if} tags before passing output back OR make my replace statement far more complex. Personally I would go with the first. It then separates the two different options so people arn't forced to have both.
#14

[eluser]isaiahdw[/eluser]
Ah, yeah I didn't think about that.

I don't think the problem is with the {if} part, because I tested it without your function and it works.

Try using this line:
Code:
$template = preg_replace('#'.$this->l_delim.$value.$this->r_delim.'(.+)'.$this->l_delim.'/'.$value.$this->r_delim.'#sU', "", $template);

I think that fixes the problem. I've updated the code again here: http://www.iitechs.com/MY_Parser.phps

Regular expressions still confuse me, so let me know if this update makes any sense.

Thanks!
#15

[eluser]adamp1[/eluser]
Sorry but what's the '#' and the '#sU', my regex editor doesn't recognise these as valid symbols. If these have a meaning I don't know about then I could be wrong but.

The problem with that expression is it won't match the following
Code:
{code}
Line: {test}
{/code}

More code

{code}
Line: {test}
{/code}

The . symbol doesn't match every char, it won't match newlines so you need a bit more code for that (.|[\r\n])+. Then you have a new problem. With this change it will be greedy and match the maximum, so now it will consume the 'More Code' line, but we don't want this. So that's why mine has
Code:
((?!".$this->l_delim.$value.$this->r_delim.")|.|[\r\n])*
in the middle. What this says is match any char/newline BUT don't be greedy, match the smallest possible string you can. So this means the 'More code' line doesn't get matched. It's a bit late atm so I'm off to bed.

Will have a look at the problem again tomorrow ish. Btw if you want some help with regex download this Regex Buddy 3.0. It lets you test your expressions on the fly and save them if need be.

I think I have an idea how to fix the problem, but will see tomorrow.
#16

[eluser]isaiahdw[/eluser]
The "#"'s are just delimiters, you could really use anything you want. The 'sU' are modifiers, 's' tells the dot to match everything including newlines (like the [\r\n] would do in your example) and the 'U' tells it not to be greedy (like the ?) in your example. I often find it's easier to use flags and keep the rest of the expression nice and simple. You can read about more modifiers here: http://us3.php.net/manual/en/reference.p...ifiers.php

Thanks for the link to Regex Buddy, it makes testing much easier!

I updated the file with the latest fixes from the CI SVN ("Fixed a bug in the parser class where numeric data was ignored."). Let me know if you find problems with it.
#17

[eluser]adamp1[/eluser]
Thanks for the details, you learn something new everyday. I will give it a go and see what happens.

EDIT:
Well I gave it a run and everything seems to work just fine. Well done to you.
#18

[eluser]isaiahdw[/eluser]
Glad to hear it works!
#19

[eluser]r.tahara[/eluser]
when I open the http://www.iitechs.com/MY_Parser.phps it return like this:

Not Found

The requested URL /MY_Parser.phps was not found on this server.


I can not try the parser...
#20

[eluser]isaiahdw[/eluser]
Sorry about that r.tahara, I've fixed the link now.




Theme © iAndrew 2016 - Forum software by © MyBB