Parent class and template management

0 views
Skip to first unread message

nickW+

unread,
Aug 29, 2010, 2:41:06 PM8/29/10
to Professional PHP Developers
Hello All,

So i'm curious about two things. The first items is more about
standardization....

I'm working on a completely different project from normal right now
and i'm working with Open Realty. I'm creating a custom plugin and I
need to render search results from a template.

So the first question is about using "parent::func_name();"

I have setup two classes within the plugin. class1 and class2. class2
extends class1. Is it acceptable to call "parent::"? It's a great way
for me to organize my code for multiple content usages. Is there
another way to call the parent class?

The second question is about templates and what is the best way to
handle them?

I have to build an array one way or another with all the values in the
template. So I have two options (to my knowledge but i'm sure there
are more methods):

Have a function return the HTML template and then run a str_replace
using array inputs for search and replace.

- or -

Pass the variables to the HTML template and insert them directly with
<?=$array['value']?>, and take advantage of the buffer in this case.

Thanks in advance.

Nick

Robert Gonzalez

unread,
Aug 29, 2010, 7:34:51 PM8/29/10
to professi...@googlegroups.com
Using parent:: calls are totally acceptable, but if you are using a
lot of them you may want to question your design architecture.

As for templating, I usually use a View class that receives variables
and values from a controller of sorts, the when the controller is
ready to render it calls the view objects render() method which starts
the output buffer, extracts all the set vars, requires the template
then returns the output buffer (the parsed template) to the
controller.

> --
> This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.
>
> For information or project assistance please visit :
> http://www.360psg.com
>
> You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
> To post to this group, send email to Professi...@googlegroups.com
> To unsubscribe from this group, send email to Professional-P...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/Professional-PHP

nickW+

unread,
Aug 30, 2010, 12:21:24 AM8/30/10
to Professional PHP Developers
Hey thanks for the reply Robert.

No I don't use it a lot. I'm putting stuff in the parent class that
other extended classes will use too like a function to convert a
string to a url, etc. Kinda like a toolset and I might have 5 calls in
my exteded class to the parent. Is there any other way to call it?

Regarding the template:

I understand the View class, object all that but how do you replace
the variables in the buffer? Do you use str_replace or another method?

Basically here are the two ways i'm thinking:

Version 1:

function results_template(){
return '<div id="go">{silly_var}</div>';
}

function replace_template_tags(){
$tags['{silly_var}'] = 'Hello World';

$search = array_keys($tags);
$replace = array_values($tags);

$template = $this->results_template();

$template = str_replace($search, $replace, $template);
}

Version 2:

function results_template($tags){
return '<div id="go">'.$tags['silly_var'].'</div>';
}

function replace_template_tags(){
$tags['silly_var'] = 'Hello World';
$template = $this->results_template($tags);
}

Both results_template functions use an extremely simply html return
for example only. The version I have implements ob_start, etc.

On Aug 29, 5:34 pm, Robert Gonzalez
<robert.anthony.gonza...@gmail.com> wrote:
> Using parent:: calls are totally acceptable, but if you are using a
> lot of them you may want to question your design architecture.
>
> As for templating, I usually use a View class that receives variables
> and values from a controller of sorts, the when the controller is
> ready to render it calls the view objects render() method which starts
> the output buffer, extracts all the set vars, requires the template
> then returns the output buffer (the parsed template) to the
> controller.
>

Subir Jolly

unread,
Aug 30, 2010, 12:26:02 AM8/30/10
to professi...@googlegroups.com

I prefer using this. Parent is equally good too

<robert.anthony.gonza...@gmail.com> wrote:
> Using parent:: calls are totally acceptable, but if you...

> On Sunday, August 29, 2010, nickW+ <n...@weeklyplus.com> wrote:
> > Hello All,
>

> > So i'm curiou...

> > For more options, visit this group athttp://groups.google.com/group/Professional-PHP

>
>

--
This group is managed and maintained by the development staff at 360 PSG. An enterprise app...

nickW+

unread,
Aug 30, 2010, 12:48:10 AM8/30/10
to Professional PHP Developers
> I prefer using this. Parent is equally good too

I use this for all my within class declarations, I never tried to do
it for the parent.

For consistency this will certainly make my code much more uniform as
the parent:: stuck out like a sore thumb.

I figured there was some way to do it with an object just didn't
imagine it would be that, I feel dumb now lol.

Thanks again.


On Aug 29, 10:26 pm, Subir Jolly <subirjo...@gmail.com> wrote:

Subir Jolly

unread,
Aug 30, 2010, 1:01:10 AM8/30/10
to professi...@googlegroups.com

Np at all

On Aug 30, 2010 12:48 AM, "nickW+" <ni...@weeklyplus.com> wrote:

> I prefer using this. Parent is equally good too

I use this for all my within class declarations, I never tried to do
it for the parent.

For consistency this will certainly make my code much more uniform as
the parent:: stuck out like a sore thumb.

I figured there was some way to do it with an object just didn't
imagine it would be that, I feel dumb now lol.

Thanks again.


On Aug 29, 10:26 pm, Subir Jolly <subirjo...@gmail.com> wrote:


>
> On Aug 30, 2010 12:21 AM, "nickW+" <n...@weeklyplus.com> wrote:
>

> Hey thanks for the reply Ro...

This group is managed and maintained by the development staff at 360 PSG. An enterprise application ...

Jack Timmons

unread,
Aug 30, 2010, 6:54:46 AM8/30/10
to professi...@googlegroups.com
On Sun, Aug 29, 2010 at 11:21 PM, nickW+ <ni...@weeklyplus.com> wrote:
> Hey thanks for the reply Robert.
>
> No I don't use it a lot. I'm putting stuff in the parent class that
> other extended classes will use too like a function to convert a
> string to a url, etc. Kinda like a toolset and I might have 5 calls in
> my exteded class to the parent. Is there any other way to call it?

If I understand your explanation right, you're on the right track.

> I understand the View class, object all that but how do you replace
> the variables in the buffer? Do you use str_replace or another method?

All of this assumes you're managing data output correctly, so by the
time you get here, there's no headers, etc that needed to be sent.
(Maybe cookies).

- While your script runs, set up the variables to give to the view
and place it somewhere in an array.
- Call the special function to import the view
- Extract the variables
- Turn on output buffering
- Include the template file
- Use ob_get_clean or ob_get_contents to store the newly parsed include
- You're on your way.

>
> Basically here are the two ways i'm thinking:
>
> Version 1:
>
> function results_template(){
>        return '<div id="go">{silly_var}</div>';
> }
>
> function replace_template_tags(){
>        $tags['{silly_var}'] = 'Hello World';
>
>        $search = array_keys($tags);
>        $replace = array_values($tags);
>
>        $template = $this->results_template();
>
>        $template = str_replace($search, $replace, $template);
> }
>
> Version 2:
>
> function results_template($tags){
>        return '<div id="go">'.$tags['silly_var'].'</div>';
> }
>
> function replace_template_tags(){
>        $tags['silly_var'] = 'Hello World';
>        $template = $this->results_template($tags);
> }
>
> Both results_template functions use an extremely simply html return
> for example only. The version I have implements ob_start, etc.


--
Jack Timmons
@_Codeacula

nickW+

unread,
Aug 30, 2010, 3:38:24 PM8/30/10
to Professional PHP Developers
Would you happen to have a link to and article about "managing data
output correctly"?

Or maybe an example of what you mean with the object and replacing
vars. I didn't think that it was possible to replace vars inside a
string unless you replace the tag with the value using str_replace or
something of sorts.

On Aug 30, 4:54 am, Jack Timmons <codeac...@codeacula.com> wrote:

Ovidiu Alexa

unread,
Aug 31, 2010, 2:40:44 AM8/31/10
to professi...@googlegroups.com
Hi,

click here for more info http://www.source-code.biz/MiniTemplator/

the point is this: I want to separate the PHP code FROM the HTML code as
much as possible. the only way I found is to have this php class read a
html template and output a string according to my instructions.

so, in any case. in html you will only want to write-in variables and
make loops, right? right. so you can make your coding in html, read a
html template, then output everything into a variable or you can also
write it on a file.

And the final question is this: did anyone ever worked with php
templates? and only if you did, please reply me with what template
engines you have been working with. thank's a lot. no, really... thanks.

Cheers!

nickW+

unread,
Aug 31, 2010, 4:35:32 PM8/31/10
to Professional PHP Developers
In essence what my code does is exactly what they do in MiniTemplator.
They create an array of variables. Then they replace all the tags that
match variables in the template.

In both examples I provided, they both honor the first part which is
creating the array. I was looking for suggestions on what is the best
way to include variables in the template.

Now if we are talking about separation then sure the template with
tags which are replaced is best since the template can be stored in a
DB or File or whatever.

Another way is option 2 which allows you to pass the vars to a
template held in a function which then inserts the array values where
needed in the template directly through <?php echo $varName; ?> and
avoids the need for the str_replace function to be used but I really
have no idea how much better it is. I realize this doesn't separate
the code but in a plugin scenario this really isn't a big deal but I
don't mind doing option 1 if str_replace won't really be a lot of
overhead.

Mind you in creating the array with tags and values, then passing the
search, replace as arrays I managed to get the time to load the site
down to 1s from 2s. Originally I had a str_replace('{var_name}','My
Var',$template); for each and every value I wanted replaced. This was
before learning that str_replace is array friendly.

Jack I still don't know why I shouldn't use <?=$varName?>. I did look
it up and some people hinted that PHP6 won't have it others say other
reasons but no one gives a very clear reason. Whats the reason?

Thanks for your help everyone!

On Aug 31, 12:40 am, Ovidiu Alexa <ovidiu.al...@gmail.com> wrote:
>   Hi,
>
> click here for more infohttp://www.source-code.biz/MiniTemplator/
Reply all
Reply to author
Forward
0 new messages