PHP templating challenge (optimizing front-end templates)

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-03-08T02:28:34Z Indexed on 2010/03/08 3:17 UTC
Read the original article Hit count: 323

Filed under:
|
|
|

Hey all,

I'm trying to do some templating optimizations and I'm wondering if it is possible to do something like this:

function table_with_lowercase($data) {
    $out = '<table>';
    for ($i=0; $i < 3; $i++) { 
        $out .= '<tr><td>';
        $out .= strtolower($data);
        $out .= '</td></tr>';
    }
    $out .= "</table>";

    return $out;
}   

NOTE: You do not know what $data is when you run this function.

Results in:

<table>
    <tr><td><?php echo strtolower($data) ?></td></tr>
    <tr><td><?php echo strtolower($data) ?></td></tr>
    <tr><td><?php echo strtolower($data) ?></td></tr>
</table>

General Case: Anything that can be evaluated (compiled) will be. Any time there is an unknown variable, the variable and the functions enclosing it, will be output in a string format.

Here's one more example:

function capitalize($str) {
    return ucwords(strtolower($str));
}

If $str is "HI ALL" then the output is:

  • Hi All

If $str is unknown then the output is:

  • <?php echo ucwords(strtolower($str)); ?>

In this case it would be easier to just call the function (ie. <?php echo capitalize($str) ?> ), but the example before would allow you to precompile your PHP to make it more efficient

© Stack Overflow or respective owner

PHP templating challenge (optimizing front-end templates)

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-03-08T02:28:34Z Indexed on 2010/03/08 3:30 UTC
Read the original article Hit count: 323

Filed under:
|
|
|

Hey all,

I'm trying to do some templating optimizations and I'm wondering if it is possible to do something like this:

function table_with_lowercase($data) {
    $out = '<table>';
    for ($i=0; $i < 3; $i++) { 
        $out .= '<tr><td>';
        $out .= strtolower($data);
        $out .= '</td></tr>';
    }
    $out .= "</table>";

    return $out;
}   

NOTE: You do not know what $data is when you run this function.

Results in:

<table>
    <tr><td><?php echo strtolower($data) ?></td></tr>
    <tr><td><?php echo strtolower($data) ?></td></tr>
    <tr><td><?php echo strtolower($data) ?></td></tr>
</table>

General Case: Anything that can be evaluated (compiled) will be. Any time there is an unknown variable, the variable and the functions enclosing it, will be output in a string format.

Here's one more example:

function capitalize($str) {
    return ucwords(strtolower($str));
}

If $str is "HI ALL" then the output is:

  • Hi All

If $str is unknown then the output is:

  • <?php echo ucwords(strtolower($str)); ?>

In this case it would be easier to just call the function (ie. <?php echo capitalize($str) ?> ), but the example before would allow you to precompile your PHP to make it more efficient

© Stack Overflow or respective owner

Related posts about php

Related posts about templating