views:

52

answers:

2

If the title hasn't scared you away yet, read on. I'm working on an ExpressionEngine website and I'm editing the member templates file. All templates that relate to member interaction are stored as functions inside of a class in one single file.

Each of these functions is a simple here document, but many of them print code with paths and terms that I don't care to use. For instance, this website will refer to logged in users as "clients" instead of "members."

Anyway, I'm looking for a way to abstract these values so that I can change them easily for this and future projects. Right now, I'm able to print variables inside of here documents by defining them within each function. I would much prefer to define these values in the top of the file, before the class is defined, but I can't get the here documents to recognize these values.

Here's an abridged example file:

<?php
/* I wish to define variables once in this area */
$globaluserterm = "client";

class profile_theme {

//----------------------------------------
//  Member Page Outer
//----------------------------------------
function member_page()
{
$userterm = "client";
return <<<EOF
<div id="{$userterm}-content">
    <h1>{$userterm} Account</h1> (Note: This DOES work)
    <h1>{$globaluserterm} Account</h1> (Note: This doesn't work)
    {include:member_manager}
</div>
EOF;
}
/* END */

//-------------------------------------
//  Full Proile with menu
//-------------------------------------
function full_profile()
{
$userterm = "client";
return <<< EOF
<div id="{$userterm}-full-profile">
    {include:content}
</div>
EOF;
}
/* END */


}
// END CLASS
?>
A: 

I don't entirely understand what you are up to but I think what you are looking for is a template engine like Smarty. (See this question for leaner alternatives.)

In Smarty, you would prepare a template file:

<div id="{$userterm}-content">
    <h1>{$userterm} Account</h1> (Note: This DOES work)
    <h1>{$globaluserterm} Account</h1> (Note: This doesn't work)
    {include:member_manager} /* Don't know how to do this in smarty */
</div>

and pre-populate the variable values:

$Smarty = new Smarty();
// ... set up caching etc. if needed  ...

$Smarty->assign("userterm", $value1);
$Smarty->assign("globaluserterm", $value2);
..... 

if this is not what you're looking for, can you explain in more detail what you want to do?

Pekka
Alternatively, he can just use PHP (as it's a templating language) via includes, forgoing the Smarty overhead, i.e., `<h1><?php echo $vars['userterm'] ?> Account</h1>`
webbiedave
A: 

If you have only one class per file, maybe it is already sufficient to define the values at class level? E.g.

class profile_theme {
    private $globaluserterm = "client";
    //....
}

and in the function:

return <<<EOF
<div id="{$userterm}-content">
    <h1>{$userterm} Account</h1> (Note: This DOES work)
    <h1>{$this->globaluserterm} Account</h1> (Note: This doesn't work)
    {include:member_manager}
</div>
EOF;
Felix Kling
This did it. I don't fully understand how classes work, and I had tried incorrect ways of referencing the private $globaluserterm variable before posting here. Thanks, Felix!
Bitmanic