Help with Template Mess

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

juliobear
Forum Newbie
Posts: 14
Joined: Tue Nov 22, 2011 6:37 pm

Re: Help with Template Mess

Post 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:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Help with Template Mess

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
juliobear
Forum Newbie
Posts: 14
Joined: Tue Nov 22, 2011 6:37 pm

Re: Help with Template Mess

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Help with Template Mess

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
juliobear
Forum Newbie
Posts: 14
Joined: Tue Nov 22, 2011 6:37 pm

Re: Help with Template Mess

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Help with Template Mess

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
juliobear
Forum Newbie
Posts: 14
Joined: Tue Nov 22, 2011 6:37 pm

Re: Help with Template Mess

Post 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.
Post Reply