...

View Full Version : Using double brackets to extract info



masterofollies
02-27-2010, 06:49 PM
I am trying to do comments on my profiles where people can leave a short comment, however it would be easy except that my page is setup in a template, so it uses arrays. So doing this {{name}} would show the persons name it pulls it from another file where it's broken down into code.

Now with this template it will only extract info from the users table. I want to extract from the comments table. What can I put in the other file so that I can use {{comments}} in my template file?

masterofollies
02-28-2010, 05:47 PM
*bump* been 24 hours

MattF
02-28-2010, 06:20 PM
*bump* been 24 hours

24 hours and pyschic abilities are still in short supply. You've not mentioned which software you're using or given any hint, (or example), as to what the existing code looks like.

masterofollies
02-28-2010, 06:34 PM
Software? there is no software, it's PHP.

library.php

if ($items == true)
{
$userrow['itemslist'] .= '<a href="index.php?x=item:' . $itemsrow['id'] . '"><font color=black>' . $itemsrow['name'] . ' (' . $unique[$itemsrow['id']] . ')</font></a><br />';
}

template.php

Items: {{itemslist}}

MattF
02-28-2010, 06:46 PM
Software? there is no software, it's PHP.

PHP is a programming language. You write a PHP script and you create a programme, i.e: software. That snippet of code was about as much use as a chocolate fireguard, btw.

With regards to your question, you code in the format relevant for your template engine.

kbluhm
02-28-2010, 07:42 PM
http://www.php.net/str_replace

masterofollies
02-28-2010, 08:12 PM
PHP is a programming language. You write a PHP script and you create a programme, i.e: software. That snippet of code was about as much use as a chocolate fireguard, btw.

With regards to your question, you code in the format relevant for your template engine.

Software would be an exe file, I-tunes, MS Word, Dreamweaver, I find that as software. PHP is a programming language.

Right and for my template it's setup to read from the users table. I need help figuring out how to add a different table into it, or how I can get it to read from the template.

At the beginning of the profile it reads

$template = <<<PROFILE

and at the end of the file it shows

PROFILE;

Inigoesdr
02-28-2010, 08:40 PM
Software would be an exe file, I-tunes, MS Word, Dreamweaver, I find that as software. PHP is a programming language.
Strictly speaking, PHP is both a piece of software an a programming language. But that is completely off-topic so both of you please stop arguing about it.

Right and for my template it's setup to read from the users table. I need help figuring out how to add a different table into it, or how I can get it to read from the template.
We would have to look at a lot more of your site's code to be able to tell you specifically where to add the new tag, but if you check to see where it is being replaced you can probably figure it out yourself. I believe kbluhm was trying to give you a subtle hint with his post. :thumbsup:

MattF
02-28-2010, 08:57 PM
Strictly speaking, PHP is both a piece of software an a programming language. But that is completely off-topic so both of you please stop arguing about it.

It's not off-topic. If he either posted the code for one of the scripts where this process takes place, or mentioned which CMS, (or similar), *software* he's using, then we'd all find it a bit easier to have the faintest idea regarding the question.

masterofollies
02-28-2010, 09:05 PM
There is no CMS, this is hand coded.

I've looked and looked at the script to find it, but I cannot find the spot where it converts it, or a template. I know these scripts inside and out. Part of it was coded by someone else, which is why I am trying to find it.

str_replace doesn't work in this.

Inigoesdr
02-28-2010, 09:12 PM
str_replace doesn't work in this.
In principal it's exactly what you're looking for. You have described a token parser that takes the content of your page and replaces certain items:

So doing this {{name}} would show the persons name it pulls it from another file where it's broken down into code.

Now with this template it will only extract info from the users table. I want to extract from the comments table. What can I put in the other file so that I can use {{comments}} in my template file?
So, -somewhere- you're doing a str/preg/etc. replace of some sort. And you haven't posted any code that shows us how you're doing that, so we can't really help you with that, can we?

kbluhm
02-28-2010, 09:13 PM
I believe kbluhm was trying to give you a subtle hint with his post. :thumbsup:
Bingo :)


str_replace doesn't work in this.
Doesn't it?


foreach ( $userrow as $key => $value )
{
$template = str_replace( '{{' . $key . '}}', $value, $template );
}

MattF
02-28-2010, 09:14 PM
There is no CMS, this is hand coded.

I've looked and looked at the script to find it, but I cannot find the spot where it converts it, or a template. I know these scripts inside and out. Part of it was coded by someone else, which is why I am trying to find it.

You'll have a template class or suchlike. Do a grep for '$template)' and find out which file does the processing.



str_replace doesn't work in this.

The template engine will most likely have removed any unused template tags. Without some full code to look at though, there's not really that much anyone can suggest.

masterofollies
02-28-2010, 09:41 PM
I found this. It does use a str_replace, but the PHP manual examples had nothing to do with what I need, so I didn't see it was helpful in this case.


function gettemplate($templatename)
{ // SQL query for the template.

$filename = "templates/" . $templatename . ".php";
include_once($filename);
return $template;
}

function parsetemplate($template, $array)
{ // Replace template with proper content.
$array = (array) $array;
foreach ($array as $a => $b)
{
$template = str_replace("{{{$a}}}", $b, $template);
}
return $template;
}

Inigoesdr
02-28-2010, 09:46 PM
Good, now search for where parsetemplate() is called, and find out where the second argument($array) is defined. After you do that all you have to do is add your new entry to that array before parsetemplate() is called for each comment.

masterofollies
02-28-2010, 10:14 PM
I am guessing this?


$page = parsetemplate($template, $finalarray);

kbluhm
03-01-2010, 01:52 AM
http://www.php.net/str_replace


str_replace doesn't work in this.





foreach ( $userrow as $key => $value )
{
$template = str_replace( '{{' . $key . '}}', $value, $template );
}



I found this. It does use a str_replace [...]



/* fast forward */

function parsetemplate($template, $array)
{ // Replace template with proper content.
$array = (array) $array;
foreach ($array as $a => $b)
{
$template = str_replace("{{{$a}}}", $b, $template);
}
return $template;
}

Interesting...

masterofollies
03-01-2010, 04:11 AM
Try reading my reply two replies ago about it. No reason to get smart about it.

Inigoesdr
03-02-2010, 03:20 AM
I am guessing this?


$page = parsetemplate($template, $finalarray);

Probably, if that is the only place it is called. Again, if I understand your intention as to display comments and use a template for that, the easiest way is going to be to make a template for the comments and generate the comment HTML, then add it to the page. Here is a pseudo-example:

// pretend $comments is a multi-dimensional array of your comments
$comment_HTML = '';
foreach($comments as $comment)
$comment_HTML .= parsetemplate('comment.tpl', $comment);

$finalarray['comments'] = $comment_HTML;
$page = parsetemplate('mypage.tpl', $finalarray); // mypage.tpl would have a "comments" token: {{{comments}}}
//in it where you would want all of the comments
That is the most help I can give you for this task. If you are still having issues I suggest you post a project in the projects forum and try to have someone make the change for you so you can see how it is done.

masterofollies
03-02-2010, 04:14 AM
Hmm so I put that in the library file, and then make a page called comment.tpl, but what is mypage.tpl for? I need the {{comments}} to be in the same template file as the other stuff.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum