Page 2 of 2

Re: Help with Template Mess

Posted: Mon Dec 05, 2011 6:44 am
by juliobear
Thanks so much for your reply. I just don't understand why I need to use preg_replace() (I also don't understand why he used multiple slashes like that), as I don't see what should be replaced? Or, this means, I still lack a firm grasp on how the code works, I guess.


You're right, I want to read in a file and display it on the .tpl page. I tried something like "$contents=include("filecontent") below the above code, and then placed {$contents} like the original developer did on the display page (the .tpl), but of course it only displayed the text as typed , i.e "{$contents}". Is that why I need to use preg_replace()? Well, I'm still not sure how I should go in replacing the file contents, or with what. You've brought me closer though, hopefully we could figure it out, as I'm lagging... :mrgreen:

Re: Help with Template Mess

Posted: Mon Dec 05, 2011 10:05 am
by pickle
Look up what preg_replace() does & you'll see it replaces matches to the regex. The multiple slashes are there because "{" and "}" are special characters that need to be escaped.

Re: Help with Template Mess

Posted: Thu Dec 15, 2011 6:27 pm
by juliobear
Ok, you helped solve my problem. It took looking into preg_replace, and then it was easy.


It's just however, I cannot still understand why the original coder used so many backslashes :banghead: . I knew about its role as escaping characters already, so it's not that.

But if we look at the original code var $re_title = "/\{\\\$title\}/" it seems that the backslash that's escaping the $ sign is escaped by an extraneous backslash preceding it. Yet the code works. Shouldn't it have been "/\{\$title\}/" instead? What are the 3 backslashes after the 1st bracket for?

From what I read the first backslash (after { ) should escape the second, which should escape the 3rd, which would leave the dollar sign unescaped.

Re: Help with Template Mess

Posted: Fri Dec 16, 2011 9:58 am
by pickle
Ya, it's messed up. It would have been much easier to just use single quotes. Then $title won't be evaluated & wouldn't need to be escaped.

Re: Help with Template Mess

Posted: Tue Dec 20, 2011 5:13 am
by juliobear
Yes, it's messed up, but you know what? It works. I tried using only single backslashes, and the code failed! When I copied previous programmers regular expression syntax, I don't know why, but then it suddenly worked. It's almost as if a syntax error yields the correct outcome.

Well I can't figure out what all those backslashes are for. In regular expression, backslashes are used to escape the characters they precede right? So again, you'd think all those backslashes would escape each other, leaving the brackets themselves un-escaped. Unless I'm missing something. I've never before seen this situation where you need 3 backslashes in a row for a regular expression to work. The backslashes are not in the template file that the Preg_Replace() replaces, so why are they used? Yet my preg_replace() didn't work using this syntax "/\{\$title\}/". It'd be cool if someone could explain why.

Re: Help with Template Mess

Posted: Tue Dec 20, 2011 9:40 am
by pickle
Try putting it in single quotes like I said. Running a preg_match() with that pattern in single quotes worked fine for me.

Re: Help with Template Mess

Posted: Wed Dec 28, 2011 3:16 am
by juliobear
By single quotes I assume you meant ' ' as opposed to " "...


I'll try that, just because I'd prefer to the code I put in to make sense to me;)


Again, got to say thanks to everyone that's helped, since as a result I did manage to solve the initial problem.